[comp.text] verbatim macros in latex

hartzell@boulder.Colorado.EDU (George Hartzell) (08/31/89)

I just picked up the verbatimfile style from the latex-style archive,
and have to questions about it.  First, the verbatimlisting
environment doesn't number blank lines.  Second, it doesn't seem to
deal well with tabs.  I took a brief stab at getting the macro number
each line (replacing the everypar with every{cr,vbox,hbox}) to no
avail.  Before I try something complicated has anyone solved either of
these problems?

g.

George Hartzell			                  (303) 492-4535
 MCD Biology, University of Colorado-Boulder, Boulder, CO 80309
hartzell@Boulder.Colorado.EDU  ..!{ncar,nbires}!boulder!hartzell

hartzell@boulder.Colorado.EDU (George Hartzell) (08/31/89)

In article <11181@boulder.Colorado.EDU>, hartzell@boulder (George Hartzell) writes:
>I just picked up the verbatimfile style from the latex-style archive,
>and have to questions about it.  First, the verbatimlisting
>environment doesn't number blank lines.  Second, it doesn't seem to
>deal well with tabs.  I took a brief stab at getting the macro number
>each line (replacing the everypar with every{cr,vbox,hbox}) to no
>avail.  Before I try something complicated has anyone solved either of
>these problems?
>
>g.
>
>George Hartzell			                  (303) 492-4535
> MCD Biology, University of Colorado-Boulder, Boulder, CO 80309
>hartzell@Boulder.Colorado.EDU  ..!{ncar,nbires}!boulder!hartzell

I just found and tried the macros from the epic package from which the
verbatimfile macros were derived and found that it does label spaces.
I understand where the tabs are going, but would love it someone has a
way to get TeX to deal with them.   So, I guess that my immediate
problem has been solved, but I get the impression that there might be
a "cleaner" solution.  For reference, here are the macros.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% verbatimfiles.sty
%
% Here is a version of Adrian's VERBATIM.STY, for inputting whole files
% verbatim, which is "more LaTeX-like" in that it uses the actual LaTeX 
% macros which set up the `verbatim' environment.
%
% As a matter  of interest, it is much nicer for TeX if you input large
% quantities of "verbatim material" from an "external file" rather than by use 
% of the `verbatim' environment (i.e. putting the material in the main input 
% file. This is because this environment, in common with most (but not all)
% other "verbatim macros", makes the whole of the "vebatim material" a macro
% argument and therefore eats up TeTeX's internal memory.
%
% Chris Rowley
%

% ---              V E R B A T I M F I L E S . S T Y
%
% --- This LaTeX style-file defines two user-callable macros:
% ---     \verbatimfile{<filename>}    for verbatim inclusion of a file
% ---     \verbatimlisting{<filename>} for verbatim inclusion with line numbers
%
% --- The macros are based on those distributed with Sunil Podar's
% --- `EPIC.STY' macro package, attributed to Tim Morgan,
% --- modified to keep internal macros internal by Adrian F. Clark,
% --- alien@uk.ac.essex.ese
% --- ---and further latexification by Chris Rowley,
% --- ca_rowley@uk.ac.open.acs.vax.
%
% --- Note that the precise layout of line breaks in this file is important!
%

\def\verbatimfile#1{\begingroup
                    \@verbatim \frenchspacing \@vobeyspaces
                    \input#1 \endgroup
}

\newcounter{lineno}

\def\verbatimlisting#1{\setcounter{lineno}{0}%
    \begingroup \@verbatim \frenchspacing \@vobeyspaces \parindent=20pt
    \everypar{\stepcounter{lineno}\llap{\thelineno\ \ }}\input#1
    \endgroup
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% verbatim.sty from epic
%
%From: Tim Morgan <morgan@uci-icsa>
%Subject: verbatim macros
%Here are some macros which I mostly stole from the TeXbook source and
%other places.  You can say
%
%	\verbfile{filename}
%to \input all of "filename" in verbatim mode.  The end of the file is the
%only thing that gets you back out of verbatim -- no characters are special.
%\listing is the same except that each line of the file is automatically
%numbered.
%Tim
%
% Produce verbatim listings of various sorts

\def\uncatcodespecials{\def\do##1{\catcode`##1=12 } \dospecials}

\def\setupverbatim{%
    \par \tt \spaceskip=0pt	% Make sure we get fixed tt spacing
    \obeylines\uncatcodespecials\obeyspaces\verbatimdefs
}
{\catcode`\^^M=13{\catcode`\ =13\gdef\verbatimdefs{\def^^M{\ \par}\let =\ }}
  \gdef\verbatimgobble#1^^M{}}
%
% Input a file in verbatim mode.  Sometimes useful for including
% real-life examples into a paper.
\def\verbfile#1{\begingroup\setupverbatim
    \parskip=0pt plus .05\baselineskip \parindent=0pt
    \input#1 \endgroup
}

% This is the same as the above, but it adds line numbers to each
% line of the file printed.
\newcount\lineno
\def\listing#1{\lineno=0\begingroup\setupverbatim
    \parskip=0pt plus .05\baselineskip \parindent=20pt
    \everypar{\advance\lineno by 1 \llap{\the\lineno\ \ }}\input#1
    \endgroup
}
George Hartzell			                  (303) 492-4535
 MCD Biology, University of Colorado-Boulder, Boulder, CO 80309
hartzell@Boulder.Colorado.EDU  ..!{ncar,nbires}!boulder!hartzell

phil@delta.eecs.nwu.edu (William LeFebvre) (09/01/89)

In article <11181@boulder.Colorado.EDU> hartzell@boulder.Colorado.EDU (George Hartzell) writes:
>I just picked up the verbatimfile style...
>Second, it doesn't seem to deal well with tabs....
> has anyone solved either of these problems?

The best solution to the tabs problem is the Unix command "expand".
Honest.  Trying to do it correctly in TeX is real hard.  Much easier
to just expand the tabs ahead of time and be done with it.  I know
that's not what you wanted to hear......


		William LeFebvre
		Department of Electrical Engineering and Computer Science
		Northwestern University
		<phil@eecs.nwu.edu>

SRWMXPB@windy.dsir.govt.nz (Peter Burgess) (09/04/89)

In article <11181@boulder.Colorado.EDU>, hartzell@boulder.Colorado.EDU (George Hartzell) writes:
> I just picked up the verbatimfile style from the latex-style archive,
> and have to questions about it.  First, the verbatimlisting
> environment doesn't number blank lines.  Second, it doesn't seem to
> deal well with tabs...

Here are some macros I cobbled together from LaTeX, other's examples and the
TeXbook.  They cope with both tabs and blank lines.  It took me quite a while
to debug them, and I wouldn't stake my life on there not being any special
cases they don't cope with.

%---------------------- Cut Here -----------------------
% \inputverbatim and \listing based on \listing from The TeXbook.  Some things
% were taken from LaTeX's verbatim environment.
                                                        
\def\uncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}
\newcommand{\inputverbatim}[1]{\if@minipage\else\vskip\parskip\fi
  \begingroup\setupverbatim \frenchspacing\@vobeyspaces\input#1 \endgroup}
\newcommand{\listing}[1]{\if@minipage\else\vskip\parskip\fi
  \begingroup\setuplist \frenchspacing\@vobeyspaces\input#1 \endgroup}

\newcount\lineno % the number of file lines listed
\newcommand{\setuplist}{\tt \lineno=0
  \leftskip\@totalleftmargin\rightskip\z@
  \parindent\z@\parfillskip\@flushglue\parskip\z@
  \def\par{\leavevmode\egroup\box0\endgraf}
  \obeylines \uncatcodespecials
  \catcode`\`=\active \catcode`\^^I=\active
  \everypar{\advance\lineno by1
    \llap{\scriptsize\the\lineno\ \ }\startbox}}

\newcommand{\setupverbatim}{\tt
  \def\par{\leavevmode\egroup\box0\endgraf}
  \leftskip\@totalleftmargin\rightskip\z@
  \parindent\z@\parfillskip\@flushglue\parskip\z@
  \obeylines \uncatcodespecials
  \catcode`\`=\active \catcode`\^^I=\active
  \everypar{\startbox}}
                                     
{\catcode`\`=\active \gdef`{\relax\lq}}

% code to make tabs work in verbatim.

\newdimen\w \setbox0=\hbox{\tt\space} \w=8\wd0 % tab amount
\def\startbox{\setbox0=\hbox\bgroup}
{\catcode`\^^I=\active
  \gdef^^I{\leavevmode\egroup
    \dimen0=\wd0 % the width so far, or since the previous tab
    \divide\dimen0 by\w
    \multiply\dimen0 by\w % compute previous multiple of \w
    \advance\dimen0 by\w  % advance to next multiple of \w
    \wd0=\dimen0 \box0 \startbox}}
%------------------------ Cut Here ---------------------

Peter Burgess
New Zealand Government Department of Scientific and Industrial Research
Applied Mathematics Division
(SRWMXPB@WINDY.DSIR.GOVT.NZ)