[Tex/LaTex] 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:

\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.

Best Answer

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