6

Item 9 of this answer to Documents with typical LaTeX errors says:

\verb and friends are delicate and should be handled with care. It deals with the advanced topic of category codes and is perhaps best described in the TeX FAQ entry Why doesn’t verbatim work within …?, together with appropriate solutions/alternatives. Most notably asking yourself the question whether using verbatim is actually necessary.

To be honest, most of my use for \verb is just a shortcut to make all the necessary catcode changes and switch to \ttfamily, which seems like it could be done with a 'simpler' macro. What makes \verb special?

Sean Allred
  • 27,421
  • 2
    \verb (or simpler variations thereof) is necessary when you want to typeset TeX's special characters without changing the code by adding markup (macros, \string, escape character and so on). – egreg Jul 13 '15 at 07:05
  • 2
    Would the downvoter care to explain? – Sean Allred Jul 13 '15 at 12:17

1 Answers1

6

There are several cases where \verb can be dispensed with.

Suppose you're writing the manual for a package and you want to typeset command or environment names; then a couple of definitions such as

\newcommand\cs[1]{\texttt{\string#1}}
\newcommand\env[1]{\texttt{\string\begin\string{#1\string}}}

will do. Example:

\documentclass{article}

\DeclareRobustCommand\cs[1]{\texttt{\string#1}}
\DeclareRobustCommand\env[1]{\texttt{\string\begin\string{#1\string}}}

\begin{document}

\section{\cs{\foo} and \env{baz}}

We describe the command \cs{\foo} and the environment \env{baz}.

\end{document}

enter image description here

As you can see, both \cs and \env can be used in a section title, but you'll soon discover that \cs doesn't really work in a title and a more complicated definition is needed:

\makeatletter
\newcommand\cs[1]{%
  \ifx\protect\@typeset@protect
    \texttt{\string#1}%
  \else
    \string\cs{\noexpand\string\string#1}%
  \fi
}
\makeatother

so that the .aux file will have the correct annotation

\@writefile{toc}{\contentsline {section}{\numberline {1}\cs{\foo} and \env  {baz}}{1}}

Often this is simplified by avoiding the backslash in the argument:

\DeclareRobustCommand\cs[1]{\texttt{\symbol{`\\}#1}}

but the input would be

We describe the command \cs{foo} and the environment \env{baz}.

This is the approach taken by ltugboat.cls which has

\DeclareRobustCommand{\cs}[1]{{\tt \char`\\#1}}
\DeclareRobustCommand{\tubbraced}[1]{\mbox{\texttt{\char`\{#1\char`\}}}}
\DeclareRobustCommand{\env}[1]{\cs{begin}\tubbraced{#1}}

(yes, with \tt, I hope they'll change this some day).

Package manuals also need other features for explaining the syntax of commands. So, for instance, one must do something like

\cs{\foo}\oarg{optarg}\marg{arg}

after defining \oarg and \marg in a suitable way. Substituting this with

\verb|\foo[optarg]{arg}|

would lose the emphasis on the “metavariables”. Entering and exiting from verbatim mode is clumsier

\verb|\foo[|optarg\verb|]{|arg\verb|}|

and the \marg approach allows to format the metavariables as we like.

Are there places where \verb or verbatim are necessary? Anything that can be done with \verb or verbatim can be obtained with suitable macros, but at the expense of markup. If I want to describe the code in ltugboat.cls for remarking the bad usage of \tt, should I type

\begin{verbatim}
\DeclareRobustCommand{\cs}[1]{{\tt \char`\\#1}}
\end{verbatim}

or

\begin{flushleft}\ttfamily
\string\DeclareRobustCommand\string{%
\string\cs\string}[1]\string{\string{%
\string\tt\space\string\char`\string\\\string#1%
\string}\string}
\end{flushleft}

that would give the same output?

enter image description here

I'd have no doubt and I think your opinion is the same.

egreg
  • 1,121,712