from https://texfaq.org/FAQ-verbwithin
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 “normal” command parameter, regardless of \protection. (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 reliable as an indication of problems.)
So 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 package will help: it doesn’t suffer from all
the problems of \verb, though it’s still not robust.
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}}`
[And so on. More to be had in the web page at top]
{}button and backticks to format code in the question (see my edit)verbatimenvironments can not be used in the argument to another command. – David Carlisle Oct 23 '13 at 15:37verbatimstuff in a general macro; verbatim requires some trickery not suited to general macros. See this question, or the very useful CTAN FAQ. – jon Oct 23 '13 at 15:38[...] Any more hints?
– Flavio Sartoretto Oct 24 '13 at 07:33