[Tex/LaTex] How to include verbatim in a figure caption

captionsfloatsverbatim

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.

Best Answer

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}}
Related Question