Text FormattingInclude colored code in listing's caption?

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
fcpjanc
Posts: 2
Joined: Fri Nov 30, 2012 1:47 pm

Include colored code in listing's caption?

Post by fcpjanc »

Hello, community,

I am trying to write a short summary of basic loop functions used in visual basic for my fellow students. I have found the listings package to color the keywords and have already defined all the parameters and additional keywords. I am also using the inline listings for faster and easier writing.

Now, I would like to include such inline listings in another listings caption, like so:

Code: Select all

\documentclass{book}

\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

\lstset{ language=[Visual]Basic,  
keywordstyle=\color{blue}, commentstyle=\color{ForestGreen}, stringstyle=\color{Maroon},
basicstyle=\ttfamily\normalsize,  
frame=lines, showspaces=false, showstringspaces=false,  
tabsize=3,
aboveskip=10pt,
belowskip=10pt,
lineskip=3pt,  
numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt  
morekeywords={Or, Loop, Until, To, As, Single, Module, Console, Double, ByVal}}

\lstMakeShortInline[basicstyle=\ttfamily\normalsize]{\|}

\begin{document}

Here, we can see an example of the |If| loop implementation with the |ElseIf| and |Else| statements.

\begin{lstlisting}[caption={|If| loop with |Else If| and |Else|},label=ifloop]  
If <cond> Then  
    <code>  
ElseIf <cond> Then  
    <code>  
Else  
    <code>  
End If  
\end{lstlisting}  

\end{document}
The error I am getting is:

Code: Select all

! Argument of \lst@temp has an extra }.
<inserted text> 
                \par 
l.24 ...p with |Else If| and |Else|},label=ifloop]
I hope I have described the problem thoroughly and will be very happy for any tips.

Cheers,
Stefan
Last edited by cgnieder on Fri Nov 30, 2012 2:46 pm, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Include colored code in listing's caption?

Post by cgnieder »

Verbatim commands like \verb, \lstinline and also shortcuts like | in your case can't be used in arguments of other macros. I'd suggest to define some command

Code: Select all

\newcommand\keyword[1]{\textcolor{blue}{\ttfamily#1}}
and use it like

Code: Select all

\begin{lstlisting}[caption={\keyword{If} loop with \keyword{Else If} and \keyword{Else}},label=ifloop]
...
\end{lstlisting}
Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Include colored code in listing's caption?

Post by cgnieder »

Ok, I have been proven wrong for this specific case. Please, next time read our board rules first which clearly state that you should provide a direct link if you make a crossposting.

Regards
site moderator & package author
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Include colored code in listing's caption?

Post by Stefan Kottwitz »

To have the solution also noted here, it can be done using \lstinline, as Heiko Oberdiek wrote in the linked topic. Some special characters such as braces need to be escaped by a preceding backslash, if required.

Code: Select all

\begin{lstlisting}[
  caption={\lstinline|If| loop with \lstinline|Else\ If| and \lstinline|Else|},
  label=ifloop]
listing.png
listing.png (9.5 KiB) Viewed 4012 times
Stefan
LaTeX.org admin
Post Reply