# Should I use \lstinline for the language keywords embedded in text?

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}[caption={SQL},label={lst:sql}]
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}


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 the listings.

The reason for that is that the default for \lstinline as per the documentation is that \lstinline

works like \verb but respects the active language and style. These listings use flexible columns unless requested differently in the optional argument

So, you need to change the column specification with the optional parameter:

\lstinline[columns=fixed]{declare}


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:

\documentclass[11pt,letterpaper]{book}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}

\lstset{
frameround=fttt,
language=SQL,
numbers=left,
breaklines=true,
keywordstyle=\color{blue}\bfseries,
basicstyle=\ttfamily\color{red},
numberstyle=\color{black}
}
\lstMakeShortInline[columns=fixed]|

\begin{document}

\begin{lstlisting}[caption={SQL},label={lst:sql}]
declare @t table(
id int
)
\end{lstlisting}

The above Listing \ref{lst:sql} demonstrates how to use |declare| statement to create a |table| variable.

\end{document}


## Code: Without \basicstyle:

\documentclass[11pt,letterpaper]{book}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}

\lstset{frameround=fttt,language=SQL,numbers=left,breaklines=true}

\lstMakeShortInline[columns=fixed]|

\begin{lstlisting}[caption={SQL},label={lst:sql}]
declare @t table(
id int
)
\end{lstlisting}

\noindent Using \verb+\lstinlne+:\par
The above Listing \ref{lst:sql} demonstrates how to use \lstinline[columns=fixed]{declare} statement to create a \lstinline{table} variable.

\noindent Using \verb+|+:\par
The above Listing \ref{lst:sql} demonstrates how to use |declare| statement to create a \lstinline{table} variable.

\end{document}

• Or define a shorthand for it with \lstMakeShortInline. Oct 10, 2014 at 6:56
• @Jubobs It is interesting that you used a different format of \lstinline than I saw in the listings manual. They said on p.4 that it should be used as follows: \lstinline!var i:integer;!' What am I missing?
– ajeh
Oct 10, 2014 at 13:01
• @ajeh Did you mean to address that last comment to Peter Grill instead of to me? Oct 10, 2014 at 13:07
• That is correct.
– ajeh
Oct 10, 2014 at 13:22
• For some reason that syntax does not seem to work for me. When I added 3 more parameters to \lstset` they broke the listings. I am now getting undefined control sequence on each of them. When I go back to my setup it works. Without your 3 new parameters the inline listings render in the same regular text font.
– ajeh
Oct 10, 2014 at 13:37