[Tex/LaTex] Use verbatim inside \texttt

typewriterverbatim

Why can't I use \begin{verbatim} inside \texttt?

\texttt{ \small \begin{verbatim} 
iab <key> <expansion>
<key> is the string which should be expanded to <expansion>
\end{verbatim}}

This throws an error.

I can do this without any problem,

{\tt \small \begin{verbatim} 
iab <key> <expansion>
<key> is the string which should be expanded to <expansion>
\end{verbatim}}

but I read, \tt is gone now.

Best Answer

Environment verbatim and macro \verb change the catcodes. The catcodes are used, when TeX generates tokens from the input characters. If the tokenization is already done, the catcode changes do not have an effect. This happens if the verbatim stuff is put in the argument of another macro (here: \texttt). This can be avoided by using a group with \ttfamily instead of \texttt:

{% start group
\par % see below
\ttfamily\small % font changes
text bla bla ...
\begin{verbatim}
verbatim text ...
\end{verbatim}
text bla bla ...
\par % see below
}% end group

\small also changes the line spacing. TeX uses the values for the line spacing at the end of a paragraph, when it generates lines from the paragraph. Therefore I have explicitly added \par at the begin and end of the group.

Related Question