Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
@JLDiaz Thanks for the formating edit! –  Pjanc Matuzl Nov 30 '12 at 12:25
    
@Pjanc_Matuzl You are welcome! –  JLDiaz Nov 30 '12 at 12:31
add comment

1 Answer

up vote 2 down vote accepted

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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