[Tex/LaTex] verb inside lstlisting caption


I try to do this:

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

But it gives me something like:

Undefined control sequence ... some more

How to I include an linline verb inside my caption?

Best Answer

\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


and the example would become


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

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