[comp.text.tex] Looking for flexible verbatim mode

ferguson@cs.rochester.edu (George Ferguson) (06/24/91)

I often include small chunks of code or output as displays in my text.
The standard verbatim mode forces these to be aligned flush with the
lefthand side of the page. In addition, an attempt to fbox the result
shows that the lines stretch to the right side of the page, presumably
due to the way the linebreaks are "obeyed". Furthermore, various
attempts to wrap verbartim displays in other constructs, such as fbox
and centerline, usually result in an error.

I would like to be able to write, eg., 

\fbox{\begin{verbatim}               \centerline{begin{verbatim}
...                          or      ...
\end{verbatim}}                      \end{verbatim}}

although I could care less about whether the parts have to be on
separate lines or whatever (ie. the final close brace could be on the
next line if necessary). That is, I would like the verbatim text to
result in a box exactly as wide as the longest line, and exactly as
high as the number of lines (well, "exactly", more or less).

I tried the verbatim package from Mainz and it didn't do what I
wanted, at least not right out of the box.

Any help would be appreciated.
George
-- 
George Ferguson			ARPA: ferguson@cs.rochester.edu
University of Rochester		UUCP: {decvax,rutgers}!rochester!ferguson
Rochester  NY  14627-0226	VOX:  (716) 275-2527

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (06/24/91)

ferguson@cs.rochester.edu (George Ferguson) writes:

>I often include small chunks of code or output as displays in my text.
>The standard verbatim mode forces these to be aligned flush with the
>lefthand side of the page. In addition, an attempt to fbox the result
>shows that the lines stretch to the right side of the page, presumably
>due to the way the linebreaks are "obeyed". Furthermore, various
>attempts to wrap verbartim displays in other constructs, such as fbox
>and centerline, usually result in an error.

You're contradicting yourself: if fbox results in an error, how
can the above described attempt to fbox 'show that the lines stretch
to the right side o the page'?

Anyways. Your problems derive partly from the fact that
fbox is (silly enough) dfined as \def\fbox#1...
and verbatim text can intrinsically not be enclosed in an argument
(catcodes are frozen when the argument is read, and verbatim mode
works by changing catcodes). The way out is to use the regular
TeX boxes, and not that syntactic sugar that LL made around them.
For instance,
    \vbox{\begin{verbatim} ... \end{verbatim}}
will work fine.

Your problem about lines that are as wide as the page is more
difficult. I once wanted to display input and output next
to each other (for certain parts of my TeX book, to appear later
this year with Addison-Wesley), and I wrote the following macros
for that.

\def\snugbox{\setbox\z@\vbox\bgroup
    \leftskip\z@
    \bgroup\aftergroup\make@snug
    \let\next=}
\def\make@snug{\par\sn@gify\egroup \box\z@}
\def\sn@gify
   {\skip\z@=\lastskip \unskip
    \advance\skip\z@\lastskip \unskip
    \unpenalty
    \setbox\z@\lastbox
    \ifvoid\z@ \nointerlineskip \else {\sn@gify} \fi
    \hbox{\unhbox\z@}\nointerlineskip
    \vskip\skip\z@
    }
\endinput

This should be inserted in an environment where \makeatletter
holds; you can use this as
    \snugbox{ ... whatever, including verbatim ... }
and the result is a box that is as wide as the widest line
in it. This is not totally foolproof, but I've tried to foresee
some abuse.

Victor.