Article View: comp.text
Article #1339Re: LaTex environments using verbatim
From: Chris Torek
Date: Sat, 02 Dec 1989 16:57
Date: Sat, 02 Dec 1989 16:57
90 lines
4188 bytes
4188 bytes
In article <1621@xn.LL.MIT.EDU> cogen@XN.LL.MIT.EDU (cogen) writes:
>I want to define environments which are based on verbatim. For example:
>\newenvironment{smallverbatim}{\begin{verbatim}\small}{\end{verbatim}}
>... Is there any way I can do this?
Alas, no (or rather, `not cleanly'). The way the verbatim `environment'
works is rather kludgey. It:
- begins a trivial list with one item;
- inserts and adjusts various glues;
- turns on obeylines;
- switches to \tt;
- disables ligatures by making ` active (\let equal to \@lquote);
- sets catcodes (everything becomes an `other');
- sets frenchspacing, and makes ` ' active (verbatim* differs here);
- then (last) executes a macro (@xverbatim) that has a single
parameter delimited at the end by `\end{verbatim}':
|gdef|@xverbatim#1\end{verbatim}[#1|end[verbatim]]
|gdef|@sxverbatim#1\end{verbatim*}[#1|end[verbatim*]]
(the above are performed in an environment in which |[] substitute
for \{} and \{} are `other').
(\endverbatim and \endverbatim* are \let equal to \endtrivlist.)
The tricky definitions of @xverbatim and @sxverbatim are the way LaTeX
manages to end verbatim text with a literal `\end{verbatim}' without
having \ act as an escape character. Unfortunately, they also mean
that \begin{verbatim} starts gobbling up stuff until a corresponding
\end{verbatim}, even if it is embedded in another macro. Too, they
cancel the advantage an environment has over a macro---that is, not
reading large chunks of text in as a macro parameter. The text inside
a verbatim `environment' is actually not in an environment: rather, it
is the argument to a special macro.
In fact, the result of all this is rather peculiar and/or amusing:
the environment begun with \begin{smallverbatim} can only be terminated
with an \end{verbatim}. This then leaves the `smallverbatim' environment
unclosed. An \end{smallverbatim} causes an error, since it attempts to
end a nonexistent trivlist, and LaTeX thinks it is trying to end the
document. If this error is ignored, one finds that the text inside the
`smallverbatim' was set in small Roman, rather than small \tt font! The
`cure' is to define the environment this way:
\newenvironment{smallverbatim}{\small\begin{verbatim}}{}
It must then be used as follows:
blah blah blah
\begin{smallverbatim}
Here is typewriter text
that appears verbatim.
\end{verbatim}
Here is Roman text that is also small.
\end{smallverbatim}
blah blah blah
There is an `unbalanced' \end{verbatim} in the text, but it is necessary.
TeX hackers may want to experiment to try to code a version of
\@xverbatim (with some other name, please) that takes *two*
arguments, one being the name of the `end string' to match (e.g.,
smallverbatim) and the next being the text to appear in verbatim.
This would be invoked after all \catcodes are set to `other' (12).
It would also have to invoke the end string itself. The preliminaries
in \begin{verbatim} (as noted above) should be given another name
(\setupverbatim, perhaps). Then the definition for \@xverbatim
would be something like
\def\@xverbatim{\verbatimhack{verbatim}}
and for \verbatim LaTeX could use
\def\verbatim{\setupverbatim \@xverbatim}
and the `smallverbatim' environment could be defined as
\newenvironment{\smallverbatim}
{\small\setupverbatim\verbatimhack{smallverbatim}}
{}
(\newenvironment takes care of grouping, so that the end part of
smallverbatim is a no-op.)
I am not sure that a suitable definition for \verbatimhack is possible.
Well, no, I imagine it is possible, but it may be unusable....
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris@cs.umd.edu Path: uunet!mimsy!chris
Technical Details
Message-ID:
<21080@mimsy.umd.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!iuvax!purdue!haven!mimsy!chrisReferences:
<1621@xn.LL.MIT.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!iuvax!purdue!haven!mimsy!chris From: chris@mimsy.umd.edu (Chris Torek) Newsgroups: comp.text Subject: Re: LaTex environments using verbatim Message-ID: <21080@mimsy.umd.edu> Date: 2 Dec 89 16:57:43 GMT References: <1621@xn.LL.MIT.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 90