4

cprotect can be used to use verbatim in a macro definition according to the package specification. Just like everything else in the world that doesn't provide an example it doesn't work in real life and therefore I want/have to do that job myself. Why does

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{cprotect}
\usepackage{fancyvrb}

\begin{document}
\newcommand{\bla}{Like this one: \verb-!@#$%^&*()_+-.}
% same in TeX
%\def\bla{\verb+Yes!+}
\cprotect\bla
\end{document}

not work (error message is ! Forbidden control sequence found while scanning use of \@argdef.? Using a verbatim environment doesn't work (or does, we'll see...).

6

\cprotect\bla allows to have \verb in the argument to \bla, but since \bla has no argument, nothing is done.

You have to \cprotect the definition of \bla:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{cprotect}
\usepackage{fancyvrb}

\begin{document}

\cprotect{\newcommand{\bla}}{Like this one: \verb-!@#$%^&*()_+-.}

\bla
\end{document}

See the trick explained at the end of section 1 in the manual.

enter image description here

  • Great! Thanks a lot! The formulation "just write \cprotect\cs or \cprotect{\cs} instead of \cs" is ridiculous and even with the prepending half sentence "if you want {<arg1> } to contain verbatim". It upsets me when so much brilliance and effort is put into the package and then documentation basically abandoned. – Karl Richter Dec 1 '14 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.