UK TeX FAQ -- question label verbwithin

### Why doesn’t verbatim 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` and `\``begin{verbatim}` have to assume that they are getting the first look at the parameter text; if they aren’t, TeX has already assigned category codes so that the verbatim command 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 the rather more helpful ‘`\``verb` illegal in command argument’. The same sorts of thing happen with `\``begin{verbatim}` … `\``end{verbatim}`:

>     \ifthenelse{\boolean{foo}}{%
>     \begin{verbatim}
>     foobar
>     \end{verbatim}
>     }{%
>     \begin{verbatim}
>     barfoo
>     \end{verbatim}
>     }

provokes errors like ‘File ended while scanning use of `\``@xverbatim`’, as `\``begin{verbatim}` fails to see its matching `\``end{verbatim}`.

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 `\``verb` command tries hard to detect if you’re misusing it; unfortunately, it can’t always do so, and the error message is therefore not a reliable indication of problems.)

The first question to ask yourself is: “is `\``verb` actually necessary?”.

- If `\``texttt{your text}` produces the same result as `\``verb``+your text+`, then there’s no need of `\``verb` in the first place.
- If you’re using `\``verb` to typeset a URL or email address or the like, then the `\``url` command from the url will help: it doesn’t suffer from all the problems of `\``verb`, though it’s still not robust; “typesetting URLs” offers advice here.
- If you’re putting `\``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.

- Some packages have macros which are designed to be responsive to verbatim text in their arguments. For example, the fancyvrb package defines a command `\``VerbatimFootnotes`, which redefines the `\``footnotetext` command, and hence also the behaviour of the `\``footnote`) command, 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 of the footmisc package.

  The memoir class defines its `\``footnote` command so that it will accept verbatim in its arguments, without any supporting package.
- The fancyvrb package defines a command `\``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, whose `\``verbdef` command defines a (robust) command which expands to the verbatim argument given.
- If you have a single character that is giving trouble (in its absence you could simply use `\``texttt`), consider using `\``string`. `\``texttt{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.

  A robust alternative is:

  >     \chardef\us=`\_
  >     ...
  >     \section{... \texttt{my\us name}}

  Such a definition is ‘naturally’ robust; the construction “<back-tick>`\``<char`>” may be used for any troublesome character (though it’s plainly not necessary for things like percent signs for which (La)TeX already provides robust macros).

Documentation of both url and verbdef is in the package files.

`fancyvrb.sty`
  macros/latex/contrib/fancyvrb (or browse the directory)
`memoir.cls`
  macros/latex/contrib/memoir (or browse the directory)
`url.sty`
  macros/latex/contrib/misc/url.sty
`verbdef.sty`
  macros/latex/contrib/misc/verbdef.sty

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=verbwithin