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.

4
  • What do you mean, “does not work either”? \SaveVerb definitely works here. What exact code are you using and what error are you getting? Jan 12, 2011 at 15:33
  • @Konrad I added the code that reproduced the error for me. Jan 13, 2011 at 8:53
  • thanks for the code, and also for posting your solution. Jan 13, 2011 at 8:56
  • 3
    [Same question as How to put \verb command inside of \textbf{} block? so I flag -- however since this one is older it's also okay to close the other way. Vote count is the same though]
    – user202729
    Apr 10, 2022 at 9:23

3 Answers 3

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}}
5
  • 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). Jan 12, 2011 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, 2011 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. Jan 23, 2011 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, 2013 at 16:45
  • @BenVoigt This should probably a new question on the main site.
    – mafp
    Apr 4, 2013 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.

4
  • 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, 2011 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. Jan 13, 2011 at 8:58
  • 2
    @LeoLiu \texttt{a\_b} works fine for me... May 8, 2013 at 13:47
  • 1
    @GerhardBurger how convenient when you have 4 or 5 _ in the word...
    – Welgriv
    Jan 18, 2022 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}
1
  • 2
    If used you need to place \SaveVerb before the list of figures. Jul 28, 2011 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .