\verb
work within...?The LaTeX verbatim commands work by changing category codes. Knuth
says of this sort of thing "Some care is needed to get the timing
right...", since once the category code has been assigned to a
character, it doesn't change. So \verb
has to assume that it is
getting the first look at its parameter text; if it isn't, TeX has
already assigned category codes so that \verb
doesn't have a
chance. For example:
\verb+\error+will work (typesetting '
\error
'), but
\newcommand{\unbrace}[1]{#1} \unbrace{\verb+\error+}will not (it will attempt to execute
\error
). Other errors one
may encounter are '\verb
ended by end of line', or even '\verb
illegal in command argument'.
This is why the LaTeX book insists that verbatim
commands must not appear in the argument of any other command; they
aren't just fragile, they're quite unusable in any command parameter,
regardless of
\protect
ion.
The first question to ask yourself is: "is \verb
actually
necessary?".
\texttt{your text}
produces the same result
as \verb
+
your text
+
, then there's no need of
\verb
in the first place.
\verb
to typeset a URL or email
address or the like, then the \url
command from the
url package will help: it doesn't suffer from the problems
of \verb
.
\verb
into the argument of a boxing
command (such as \fbox
), consider using the lrbox
environment:
\newsavebox{\mybox} ... \begin{lrbox}{\mybox} \verb!VerbatimStuff! \end{lrbox} \fbox{\usebox{\mybox}}
Otherwise, there are three partial solutions to the problem.
\VerbatimFootnotes
, which redefines the \footnotetext
(and
hence the \footnote
) commands in such a way that you can include
\verb
commands in its argument. This approach could in
principle be extended to the arguments of other commands, but it can
clash with other packages: for example, \VerbatimFootnotes
interacts poorly with the para
option to the footmisc
package.
\SaveVerb
,
with a corresponding \UseVerb
command, that allow you to save
and then to reuse the content of its argument; for details of this
extremely powerful facility, see the package documentation.
Rather simpler is the verbdef package, which defines a (robust) command which expands to the verbatim argument given.
\texttt
), consider using
\string
. my\string_name
typesets the same as
\verb+my_name+
, and will work in the argument of a command. It
won't, however, work in a moving argument, and no amount of
\protect
ion will make it work in
such a case.