# Verbatim Environment in Figure Caption

verbatim environment is not working in figure caption. Checked How to put \verb command inside of \textbf{} block? and Verbatim inside a command is not working.

\documentclass[11pt, double space]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{color}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[english]{babel}
\usepackage[autostyle, english = american]{csquotes}
\MakeOuterQuote{"}
\usepackage{cprotect}

\begin{document}
\begin{figure}[!h]
\caption{Sample 1 \cprotect\begin{verbatim} \tau(A) = e^{\chi_0A/(KD_p(K+A))} \end{verbatim} per hour.}
\label{fig:singlecell_chemo}
\end{figure}

\end{document}


I have tried cprotect package also. Any help.

You can also use the verbatimbox package. In this case, you put the verbatim material into a verbbox before invoking figure, and then recall it with \theverbbox inside the figure.

\documentclass[11pt, double space]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{color}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[english]{babel}
\usepackage[autostyle, english = american]{csquotes}
\MakeOuterQuote{"}
\usepackage{verbatimbox}

\begin{document}
\begin{verbbox}\tau(A) = e^{\chi_0A/(KD_p(K+A))} \end{verbbox}

\begin{figure}[!h]
\caption{Sample 1 \theverbbox per hour.}
\label{fig:singlecell_chemo}
\end{figure}

\end{document}


p.s. version 3.01 of verbatimbox just hit the streets. It finally has "real" documentation.

You must use \cprotect before \caption.

\documentclass[11pt, double space]{article}
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{cprotect}
\begin{document}
\begin{figure}[!ht]
\cprotect\caption{Sample 1 \verb+\tau(A) = e^{\chi_0A/(KD_p(K+A))}+  per hour.}
\label{fig:singlecell_chemo}
\end{figure}

\end{document}


BTW: Don't use the environment center inside figure. When should we use \begin{center} instead of \centering?

\cprotect needs to be specified before \caption:

\cprotect\caption{Sample 1 \verb|\tau(A) = e^{\chi_0A/(KD_p(K+A))}| per hour.}


Then \cprotect puts the argument of \caption inside a file and uses the file loading command as safer argument for \caption.

Also I have used \verb for use of verbatim inside a text line.

Using \cprotect or other methods is a good solution for large material.

In case just a control sequence name is needed in a moving argument there's an easier way:

\caption{Example of usage of the \texttt{\string\psbox} command}


Not documented in the LaTeX manual, but easier than using complex strategies.

If the \listoffigures is needed, then a more complex approach is needed:

\documentclass[11pt]{article}

\newcommand{\captionstring}[1]{\noexpand\noexpand\noexpand\string\string#1}

\begin{document}
\listoffigures

\begin{figure}[htp]
\caption{Sample 1 \texttt{\captionstring\psbox}.}
\end{figure}

\end{document}

• "Moving argument"? \string will not survive, when the caption is moved to the .lof file. – Heiko Oberdiek Aug 14 '15 at 16:48