# How to include verbatim in a figure caption?

I would like to include verbatim in a caption of a figure. I only want part of it to be verbatim, not the entire caption. The simple approach does not work because verbatim is not allowed in arguments of other commands, as this answer explains. I tried using \SaveVerb and \UseVerb, but this does not work either. Is there a way to achieve this?

The code I used was the following:

\SaveVerb{term}|test|
\begin{figure}
\caption{This is a \UseVerb{term}.}
\end{figure}

This did not compile (I will update the error message when I get home, don't have access to it right now). However, I already fixed this, as I explain in my own answer to this question.

-
What do you mean, “does not work either”? \SaveVerb definitely works here. What exact code are you using and what error are you getting? –  Konrad Rudolph Jan 12 '11 at 15:33
@Konrad I added the code that reproduced the error for me. –  Björn Pollex Jan 13 '11 at 8:53
thanks for the code, and also for posting your solution. –  Konrad Rudolph Jan 13 '11 at 8:56

A new package cprotect(released no more than 2 weeks ago), solve this problem prefectly. And it is much easier to use.

\usepackage{cprotect}

\cprotect\caption{blah \verb|#$%^&| blah...} http://www.ctan.org/tex-archive/help/Catalogue/entries/cprotect.html There are still several other solutions. As is referred, \SaveVerb and \UseVerb from fancyvrb is also useful. And you can even do it mannually all by yourself: % in preamble \newsavebox\verbbox % in document env. \begin{lrbox}{\verbbox} \verb|@#$%#%|
\end{lrbox}
\caption{\usebox{\verbbox}}
-
thanks. You also need to "protect" the figure environment containing the caption using \cprotEnv. --- A point to note is that optional arguments are not yet supported by \cprotect. They will be in the next release (soon). –  Bruno Le Floch Jan 12 '11 at 18:13
@Burno: Thanks for your comment, your new package is very useful. In figure env, \cprotect\caption does works for me, without \cprotEnv\begin{figure}. I'm not sure when to use \cprotEnv. –  Leo Liu Jan 13 '11 at 4:28
The documentation is not good, I have to admit. And you are right about the \cprotEnv being useful only in some border cases: basically, each time the environment is not a true environment, i.e., gobbles its argument before doing something with it. A typical example is the align environment (although \cprotEnv does not seem to play well with that), and every environment define using the environ package. Not many applications, but it was my initial motivation (for a private package), so I left it. –  Bruno Le Floch Jan 23 '11 at 17:10
Many thanks, Brono. –  Leo Liu Jan 24 '11 at 13:13
\cprotect made \verb|...| work great, but now I have ^^E^^L on the end of my caption (\cprotect makes it appear whether I use \verb or not). Any ideas on what I'm doing wrong? –  Ben Voigt Jan 2 '13 at 16:45
show 1 more comment

Ok, the simple solution was to use \protect before \UseVerb:

\SaveVerb{term}|test|
\begin{figure}
\caption{This is a \protect\UseVerb{term}.}
\end{figure}
-
If used you need to place \SaveVerb before the list of figures. –  Martin Scharrer Jul 28 '11 at 9:55

Everything can also be done with \texttt, not so short as with verb, but it is possible. A backslash is \textbackslash.

-
Indeed. Sometimes it's a bit awesome, however. One must use \texttt{a\symbol{95}b} to get a_b correctly, \texttt{a\\_b} is not correct. –  Leo Liu Jan 12 '11 at 14:27
Thanks for this answer. I finally decided to go with it, as it actually fits my intend better (I just want some special identifiers to stand out from the text). The advantage of using \texttt is that it also wraps. –  Björn Pollex Jan 13 '11 at 8:58
@LeoLiu \texttt{a\_b} works fine for me... –  Gerhard Burger May 8 '13 at 13:47