Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Why would you, verbatim prints the contents in typewriter text anyways. –  Torbjørn T. Feb 5 '13 at 8:41
    
@TorbjørnT. So that the text shows as a separate block of code. –  ATOzTOA Feb 5 '13 at 8:49
    
Yes, that's what the verbatim environment does, nothing to with \texttt. If you remove \tt or \texttt and recompile, does that give you what you want? –  Torbjørn T. Feb 5 '13 at 9:07
    
@TorbjørnT. Thanks, it works. Then, what is \tt and \small doing there? Oops, I know I should know, but I can't remember. –  ATOzTOA Feb 5 '13 at 9:10
    
\small reduces the font size a little, but the use of \tt is just a misunderstanding I guess, it is unnecessary. –  Torbjørn T. Feb 5 '13 at 9:12

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.