4

I want to use verbatim in a caption of a figure. I have tried the cprotect package but it seems to work only with \caption and not with \captionof. Is there a workaround?

\documentclass{scrbook}
\usepackage[ngerman]{babel}
\usepackage[latin1]{inputenc}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{capt-of}

\begin{document}

\verb|works|

\afterpage{%
    \begin{landscape}
      \parbox[c][\textwidth][s]{\linewidth}{%
        \vfill
        \centering
        \mbox{+++++++ a huge figure ++++++++}
        \captionof{figure}{Syntaxbaum \verb|doesn't works|}
        \vfill
      }
    \end{landscape}
}
\end{document}
user4811
  • 4,185
  • I don't suppose \texttt{doesn't works} would do the trick? – John Wickerson Jun 05 '13 at 12:08
  • 1
    \verb is useful for material that includes TeX's special characters; if it's not the case, use \texttt and the result will be what's needed. – egreg Jun 05 '13 at 12:28
  • Thanks for your answer but I need the verbatim environment. – user4811 Jun 05 '13 at 13:01
  • This can also be solved by nesting \cprotect as mentioned in its documentation, or use my cprotectinside package (although the question is closed, and the target question can already be easily solved with cprotect) – user202729 Jun 26 '22 at 12:51

1 Answers1

4

Create the verbbox before the figure and then recall it therein.

\documentclass{scrbook}
\usepackage[ngerman]{babel}
\usepackage[latin1]{inputenc}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{capt-of}
\usepackage{verbatimbox}

\begin{document}

\verb|works|

\begin{verbbox}
now it works
\end{verbbox}
\afterpage{%
    \begin{landscape}
      \parbox[c][\textwidth][s]{\linewidth}{%
        \vfill
        \centering
        \mbox{+++++++ a huge figure ++++++++}
        \captionof{figure}{Syntaxbaum \theverbbox}
        \vfill
      }
    \end{landscape}
}
\end{document}

enter image description here