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:

\caption{This is a \UseVerb{term}.}

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.


\cprotect\caption{blah \verb|#$%^&| blah...}


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
% in document env.
  • 1
    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
  • 1
    \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
  • @BenVoigt This should probably a new question on the main site. – mafp Apr 4 '13 at 14:42

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
  • 1
    @LeoLiu \texttt{a\_b} works fine for me... – Gerhard Burger May 8 '13 at 13:47

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

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

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.