24

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}

enter image description here

ajeh
  • 2,572

1 Answers1

38

Yes, I would recommend you use \lstinline so that the same style is applied:

enter image description here

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|:

enter image description here


If you don't want to specify the \basicstyle you get:

enter image description here


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}

Peter Grill
  • 223,288
  • 1
    Or define a shorthand for it with \lstMakeShortInline. – jub0bs Oct 10 '14 at 06: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 '14 at 13:01
  • @ajeh Did you mean to address that last comment to Peter Grill instead of to me? – jub0bs Oct 10 '14 at 13:07
  • That is correct. – ajeh Oct 10 '14 at 13:22
  • 1
    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 '14 at 13:37
  • I missed that you included xcolor package. That's why the colors were killing me. Now I see that the fonts of the listings are completely different. So it seems that w/o changing the listing basicstyle font the inline listings will not render in the same font and style as the block listings with this approach. Am I missing anything else? – ajeh Oct 10 '14 at 14:29
  • @ajeh: Sorry, I should have pointed out that I added xcolor so that it was easier to see that the inline listings and the environment maintained consistent formatting. Yes, I believe that if you don't set \basicstyle then the text which are not keywords will take on the font of the surrounding text. Keywords are different in that the language=SQL must be defining keywordstyle=\bfseries. But that language file is not defining a \basicstyle. – Peter Grill Oct 10 '14 at 18:16
  • How can I preserve the default block listing style, yet define basicstyle for the inline listings to work? Perhaps should I try a different language? – ajeh Oct 10 '14 at 20:11
  • @ajeh: I think that should be a separate question -- That way it is likely to be help to others as it'll be easier to find. – Peter Grill Oct 10 '14 at 20:26
  • No, it is still the same original question - how to format the inline comments same as the block comments. – ajeh Oct 10 '14 at 20:47
  • @ajeh: Sorry I misunderstood. So, are you saying that prefer to not specify \basicstyle and have the same output with listings enviroment and lstinline? – Peter Grill Oct 10 '14 at 21:29
  • @ajeh: Please see updated solution. – Peter Grill Oct 10 '14 at 21:43
  • Even though I do not understand how that works, it is interesting again. Perhaps the difficulty a non-English speaker such as myself is having reading an English manual written by a German speaker is understandable, maybe not, but I fail to make a connection between fixed column format as described by Heinz and Hoffmann on p. 21 and how it fixed basic style in your example. – ajeh Oct 14 '14 at 14:07
  • @ajeh: Yeah TeX can be confusing at times. I had to reread the documentation several times to get it, so don't feel bad. Also, I noticed a slight error in my previous version and have corrected it. You must specify [columns=fixed] for \lstinline in either case. – Peter Grill Oct 14 '14 at 22:36
  • @PeterGrill That worked for the most part, but still produced a nasty side effect: an extra blank space at the beginning of the line onto which the inline listing wraps. Virtually every inline listing that wraps onto the next line starts 1 blank space after the beginning of that line now. – ajeh Oct 24 '14 at 17:10
  • @ajeh: I can't seem to duplicate it. I'd recommend you post a new question. – Peter Grill Oct 24 '14 at 17:27