[Tex/LaTex] Include a lstlistings keyword in lstlistings caption

inline()keywordslistings

I'm writing a small summary for my fellow students about VB in LaTeX. I have to cover the basic loops and found the \lstlistnigs package to be the best for writing colored code. I have already added some keywords that are not included directly in the package.
I have also defined a short verb for inline listing.
Now, what I would like to do, is to include a VB keyword in a listing's caption with the help of a inline listing.

\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}  
If [cond] Then  
    [code]  
ElseIf [cond] Then  
    [code]  
Else  
    [code]  
End If  
\end{lstlisting}  

\end{document}

I hope I have described the problem thoroughly and will be grateful for any tips!

Cheers

Best Answer

See "Section 5.1 Listings inside arguments". You can use \lstinline and need to add an additional backslash before the backslash, the braces and the space.

\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={%
  \lstinline|If| loop with \lstinline|Else\ If| and \lstinline|Else|%
},label=ifloop]
If [cond] Then
    [code]
ElseIf [cond] Then
    [code]
Else
    [code]
End If
\end{lstlisting}
\end{document}

Result