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 try to do this:

\begin{lstlisting}[language=Perl,caption={text {\verb+myverb+} some more text}]
...
\end{lstlisting}

But it gives me something like:

Undefined control sequence ... some more

How to I include an linline verb inside my caption?

share|improve this question
2  
Welcome to TeX.SX! Do you really need \verb because what you denote with myverb has special characters in it? If it hasn't, then \texttt{myverb} does the same. –  egreg Sep 12 '12 at 10:45
    
Okay, that works and looks the same. I would really prefer to use verb if possible at all, because I like to grep through my tex files and I now won't collect all verbs with this. But that is a minor setback. If there is no real solution, please post your comment as an answer, so I can accept it. –  javex Sep 12 '12 at 10:53
add comment

2 Answers

up vote 4 down vote accepted

\verb is a very peculiar command in LaTeX and this peculiarity consists in the fact that it may not appear in the argument of another command. In your example it is in the optional argument to \begin{lstlisting} and this is disallowed (because of very good reasons).

Actually \verb is necessary only when characters special for (La)TeX are needed: # $ % ^ & { } _ \. When none of these character is needed, one can use \texttt{myverb material} with the same result as that given by \verb.

However, this doesn't solve the problem of grepping through your files for finding all \verb. You can do

\protected\def\psverb#1{\def\innerpsverb##1#1{\texttt{##1}}

and the example would become

\documentclass{article}
\usepackage{listings}
\protected\def\psverb#1{\def\innerpsverb##1#1{\texttt{##1}}\innerpsverb}

\begin{document}
\begin{lstlisting}[language=Perl,caption={text {\psverb+myverb+} some more text}]
...
\end{lstlisting}
\end{document}

Grepping for \verb or \psverb should be quite easy.

share|improve this answer
    
Perfect solution and thanks for the explanation. –  javex Sep 12 '12 at 11:26
    
{\psverb!S#Var!}: You can't use 'macro parameter character #' in horizontal mode. I have to escape hash characters, like so now compared to Verb!...!: {\psverb!S\#Var!} –  Emit Taste Nov 5 '13 at 22:48
    
@EmitTaste Yes, that's what you need to do. –  egreg Nov 5 '13 at 23:03
add comment

Another option not requiring any new commands is to use \lstinline:

\documentclass{article}
\usepackage{listings}

\begin{document}

\begin{lstlisting}[language=Perl,caption={text {\lstinline[basicstyle=\ttfamily]+myverb+} some more text}]
...
\end{lstlisting}

\end{document}
share|improve this answer
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.