5

I am a newcomer to Latex and I am reading the listings pack­age doc­u­men­ta­tion(http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf). Page 6 has the following paragraph:

The styles use two different kinds of commands. \ttfamily and \bfseries both take no arguments but \underbar does; it underlines the following argument. In general, the very last command may read exactly one argument, namely some material the package typesets. There's one exception. The last command of basicstyle must not read any tokens-or you will get deep in trouble.

I have some questions concerning this paragraph. First, what does "the vary last command" refer to? At first sight I thought it refers to \underbar, but the phrase "In general" which appears at the beginning of the same sentence and the following sentences "There's one exception..." confuse me. Secondly, and similarly, what does "The last command of basicstyle" refer to? Could anyone explain the whole paragraph to me? Page 30 also has a similar sentence:

The last token of basic style must not read any following characters.

I would appreciate your help with this situation.

3 Answers 3

5

You can do, as the documentation says

keywordstyle=\color{black}\bfseries\underbar

or also other silly things such as

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\newcommand{\fancycomment}[1]{\textcolor{red}{\slshape#1}}
\newcommand{\fancystring}[1]{\textcolor{blue}{\itshape#1}}

\lstset{% general command to set parameter(s)
  basicstyle=\small, % print whole listing small
  keywordstyle=\color{black}\bfseries\underbar,
  identifierstyle=\textit,
  commentstyle=\fancycomment,
  stringstyle=\fancystring,
  showstringspaces=false,
}

\begin{document}

\begin{lstlisting}[language=Pascal]
for i:=maxint to 0 do
begin
  { do nothing }
end;
Write('Case insensitive ');
WritE('Pascal keywords.');
\end{lstlisting}

\end{document}

What happens is that a word to be treated as comment is passed as a braced argument after the commands in commentstyle and similarly for keywordstyle or stringstyle. So what happens behind the scenes is something like

\begingroup\color{black}\bfseries\underbar{for}\endgroup

(grouping is used to avoid settings leaking outside the intended scope); actually instead of for there would be a far more complex list of tokens, but this is irrelevant for this explanation. Without the \underbar command, we'd have

\begingroup\color{black}\bfseries{for}\endgroup

and the braces do nothing special. The use of \fancycomment and \fancystring is just by way of example, since

commentstyle=\color{red}\slshape,
stringstyle=\color{blue}\itshape,

would do the same more easily. You can think to better applications.

This does not happen for basicstyle: it would be absurd to take the whole processed listing and then to pass it as an argument for the macros in basicstyle.

Thus we're not allowed to do such games with basicstyle, that should only receive as value a list of declarations such as \small or \ttfamily.

It's a common error to believe that \small and the other font size changing commands take an argument: they don't. Similarly to \ttfamily, \small orders TeX to “typeset in a smaller font from now on”, limited by the standard scoping rules.

Not all one argument commands can be in a trailing position as the value for a style key: for instance, \MakeUppercase is not allowed.

3

That refers to the way you give styling parameters to listings. So what it means is just that you can use any styling command which does not take any parameter.

You must not use commands taking parameters if you use basicstyle (as in \lstset{basicstyle=}. In any other case you are allowed to use a command with only one parameter such as in \ttfamily\underbar. The "very last" command just refers that this one has to be the last one in the assignment.

The reason for that distinction is just that basicstyle is applied to all listing text, so a keyword for example would be styled as basicstyle+keywordstyle. And only the last one (here the last of keywordstyle) can process the argument.

3

basicstyle is the style of the overall font in a listing.

\documentclass{article}
\usepackage{listings}
\lstset{language=[LaTeX]TeX}
\begin{document}

%OK
\begin{lstlisting}[basicstyle=\bfseries,
                   keywordstyle=\underbar]
abc \ttfamily
\end{lstlisting}

%big troubles
%\begin{lstlisting}[basicstyle=\underbar]
%abc
%\end{lstlisting}
%

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .