30

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.

3 Answers3

28

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}}
Leo Liu
  • 77,365
  • 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 04: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 02 '13 at 16:45
  • @BenVoigt This should probably a new question on the main site. – mafp Apr 04 '13 at 14:42
18

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

lockstep
  • 250,273
  • 1
    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 08:58
  • 2
    @LeoLiu \texttt{a\_b} works fine for me... – Gerhard Burger May 08 '13 at 13:47
  • 1
    @GerhardBurger how convenient when you have 4 or 5 _ in the word... – Welgriv Jan 18 '22 at 16:09
10

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

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