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

  • It isn't really fair to say there isn't enough documentation, because basically the whole source code is documented; what you want is a brief overview how it works (so you can deduce what it does in unexpected cases). See my answer for that, or my other answer for a solution that allows arguments in the replacement text. – user202729 Dec 17 '21 at 01:41

2 Answers2

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

egreg
  • 1,121,712
  • 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 { } to contain verbatim". It upsets me when so much brilliance and effort is put into the package and then documentation basically abandoned. – Kalle Richter Dec 01 '14 at 17:37
0

Using my new cprotectinside package.

Declare the "special delimiter character" to be /, then use it to wrap around the verbatim-like commands (\verb). Read the documentation for more details.

%! TEX program = lualatex
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{cprotectinside}
\usepackage{fancyvrb}

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

user202729
  • 7,143