[comp.text] LaTex environments using verbatim

cogen@XN.LL.MIT.EDU (cogen) (11/25/89)

I want to define environments which are based on verbatim. For example:


\newenvironment{smallverbatim}{\begin{verbatim}\small}{\end{verbatim}}

\begin{smallverbatim}
Some text in here.
\end{smallverbatim}


This doesn't work because LaTex apparently doesn't expand the macros when looking
for the string "\end{verbatim}"; it tells me "File ended while scanning use of
\@xverbatim". 

Is there any way I can do this? Do not be deceived by the simple example; my
actual environments are more complicated than the simple example.

-- David Cogen.

pyt@hprnd.HP.COM (Pierre-Yves Thoulon) (11/28/89)

> \newenvironment{smallverbatim}{\begin{verbatim}\small}{\end{verbatim}}
> 
> \begin{smallverbatim}
> Some text in here.
> \end{smallverbatim}
For this particular example, I've been using the following:
 
 \let\savett=\tt
 \def\tt{\small\savett}
 \begin{verbatim}
 TEXT_GOES_HERE
 \end{verbatim}
 \let\tt=\savett

Pyt

chris@mimsy.umd.edu (Chris Torek) (12/03/89)

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