When writing about programming languages, there are examples to format as listings. That works fine and produces beautiful results. Sometimes I want to mention the keywords used in the listing further in the text, like that declare
that I put in bold italic.
I tried \lstinline
but it seems to simply print its argument verbatim without applying the same text formatting as in the listing. Is there any other mechanism to typeset the keywords or short snippets within regular text, while obeying the same language formatting as specified in \lstset
?
\documentclass[11pt,letterpaper]{book}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\begin{document}
\lstset{frameround=fttt,language=SQL,numbers=left,breaklines=true}
\begin{lstlisting}
declare @t table(
id int
)
\end{lstlisting}
The above Listing \ref{lst:sql} demonstrates how to use \textbf{\textit{declare}} statement to create a table variable.
\end{document}
Best Answer
Yes, I would recommend you use
\lstinline
so that the same style is applied:Note that even though it appears the formatting is the same, if you look carefully you will notice that the spacing of the keywords in the
\lstinline
is slightly different than thelistings
.The reason for that is that the default for
\lstinline
as per the documentation is that\lstinline
So, you need to change the column specification with the optional parameter:
or use
\lstMakeShortInline[columns=fixed]|
to define a special char (in this case the vertical unix pipe) and simply use|declare|
:If you don't want to specify the
\basicstyle
you get:Summary:
[columns=fixed]
needs to be applied to the\lstinline
.Code: Specify
\basicstyle
:Code: Without
\basicstyle
: