[comp.text.tex] ASCII tabs in verbatim sections

andy@research.canon.oz.au (Andy Newman) (03/27/91)

We use LaTeX for software documentation and often need to include
pieces of source code in our documents, this is usually placed
between \begin{verbatim}, \end{verbatim} pairs, so far so good.
What is really annoying us is the treatment of ASCII tabs. Has
anyone made modifications to LaTeX's verbatim handling to process
ASCII tabs in a ``correct'' manner? i.e., tab stops every eight
spaces. I've had a look a style called cprog but this simply
replaces a tab with four spaces regardless of the current position
on the line, this is not really good enough for our purposes.
Any help would be appreciated.


-- 
Andy Newman (andy@research.canon.oz.au) Canon Info. Systems Research Australia
"X: 2. An over-sized, over-featured, over-engineered window system developed
at MIT and widely used on UNIX systems." from the jargon file.

king@cs.uq.oz.au (Paul King) (03/27/91)

andy@research.canon.oz.au (Andy Newman) writes:

>Has anyone made modifications to LaTeX's verbatim handling to process
>ASCII tabs in a ``correct'' manner, i.e. tab stops every eight spaces?

Here is a style file that should do what you want.
It appeared in comp.text.tex (then called comp.text) several years ago:
--------------------cut here for tabverb.sty-----------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    Verbatim With Tabbing and Page Breaks
%
%       Written by:
%               Michael Fine
%               Distributed Systems Architecture.
%               September 1987
%
%
% This environment is similar to the LaTeX verbatim environment but it
% interpretes tab characters as usually expected.  It has the
% additional feature that a CTL-L in the verbatim environment invokes
% the LaTeX macro \newpage (thereby giving you some control over page
% breaks).
%
% There is also a command \inputverbatim which can be used to specify
% a file to be input and typeset in the verbatim environment.
%
% This does not and should be made to work correctly for verbatim* as
% well.  Also, it should probably use a different name.
%
% Usage:
%
%         \setlength{\tabwidth}            % or
%         \settowidth{\tabwidth}{text}
%             % default is width of 8 characters of \normalsize\tt
%
% Then:
%         \begin{verbatim}
%             Verbatim text set in \tt font with ^L for page ejects
%             and ^I for tabs
%         \end{verbatim}
%
%   or:
%         \inputverbatim{filename}         % input a file of text
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%
% A version message
%
\typeout{Tabbing Verbatim Style <September 29, 1987>}

\newlength{\tabwidth}
\settowidth{\tabwidth}{\normalsize\tt mmmmmmmm}
        % default is 8 characters of \tt font in \normalsize

\begingroup
\catcode`\^^I=\active\def^^L{}

\gdef\@newverbatim{\@verbatim
    \def\par{\egroup\@@par\noindent
    \@tempdima 0pt\hbox\bgroup{}}\obeylines
    \let\ignorespaces\relax    % for \@gobblecr
    \catcode`\^^I\active
    \def^^I{\egroup\setbox\@tempboxa\lastbox\@tempdimb \wd\@tempboxa
    \box\@tempboxa
    \advance\@tempdima by \@tempdimb \computeskip\penalty10000
    \hskip\@tempdimb\advance\@tempdima by \@tempdimb
    \hbox\bgroup{}}\def^^L{\egroup\setbox\@tempboxa\lastbox
    \@tempdimb\wd\@tempboxa
    \ifdim\@tempdimb=0pt\else\box\@tempboxa\fi
    {\let\par\@@par\newpage}\noindent\hbox\bgroup\@gobblecr}\relax
    \def\computeskip{\@tempcnta 0\@tempdimb 0pt\loop
        \ifdim \@tempdimb<\@tempdima
        \advance\@tempdimb by \tabwidth \advance \@tempcnta1 \repeat
    \ifdim\@tempdima=\@tempdimb \advance\@tempcnta1\fi
    \@tempdimb\@tempcnta \tabwidth \advance \@tempdimb
    by -\@tempdima}}

\gdef\verbatim{\@newverbatim\frenchspacing\@vobeyspaces
    \@xverbatim\noindent\hbox\bgroup\@gobblecr}

\gdef\endverbatim{\egroup\setbox\@tempboxa\lastbox
    \@tempdimb\wd\@tempboxa
    \ifdim\@tempdimb=0pt\else\box\@tempboxa\fi
    \let\par\@@par\endtrivlist}

%
% Now we define \inputverbatim
%
% This is similar to \verbatim but does not need \@xverbatim trickery
% to delimit the begining and end of the verbatim text.

\gdef\inputverbatim#1{{\@newverbatim\frenchspacing\@vobeyspaces
    \noindent\hbox\bgroup\input{#1}\endverbatim}}

\endgroup
-------------------end of tabverb.sty-----------------------

Simply store this somewhere in your TEXINPUTS path or in the
subdirectory you are working in and include `tabverb' as
a style option in your documentstyle command, e.g.:
\documentstyle[11pt,a4large,tabverb]{article}

Hope this helps, Paul.
-----------
Paul King                                                              _--_|\
Dept. of Computer Science, Univ. of Queensland                        /      X
Queensland, Australia, 4072                                           \_.--._/
king@cs.uq.oz.au (ACSNET)                                                   v
--
Paul King							       _--_|\
Dept. of Computer Science, Univ. of Queensland			      /      X
Queensland, Australia, 4072					      \_.--._/
king@cs.uq.oz.au (ACSNET)					            v