1

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}[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}

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

Cheers

  • @Pjanc_Matuzl You are welcome! – JLDiaz Nov 30 '12 at 12:31
2

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

  • Thank you for the solution. It works! I have, however, improved it to my needs (which are writing as little as possible) by defining a simple command: \newcommand{\code}[1]{\lstinline|#1|} – Pjanc Matuzl Dec 3 '12 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.