[comp.text.tex] An program listing environment for LaTeX

cchang@acsu.buffalo.edu (Chengping Chang) (06/29/90)

There has been a request for verbatim program listing environment 
recently.  The key points are not to include from another file 
using input and be able to caption the listing as well as to label 
it.

Here is something I use.  It works for me.

%%
%% program.sty   for use with LaTeX 2.09
%% version 1.5
%% June 25, 1990
%% Chengping Chang
%% Computer Science, University at Buffalo
%% internet address: cpchang@sybil.cs.buffalo.edu
%%
%%
%% I used to make sample programs available in handouts while teaching
%% CS113 and CS114.  This is the environment that I use.
%
%% Description: This is a verbatim alike environment.  Its purpose is to
%%
%%     (1) list short programs within LaTeX document
%%     (2) list the program in verbatim mode   
%%     (3) list the program with line numbers
%%     (4) enable caption to the listing
%%     (5) label the listing for reference
%%
%%     This is different from other postings earlier in comp.text.tex that
%%
%%     (1) The listing is "in" the document, not being "input" from another
%%         file.  I found this useful in submitting paper that I can e-mail
%%         just one file.
%%     (2) The listing is made as a "figure" and "table" environment.
%%         You can caption it and label it.
%%     (3) These are double-lines enclosing the listing, makes it clear from
%%         other text.
%%     (4) The listing can go longer than one page.
%%
%% Usage: Enclose short program segments between \begin{program}
%%        and \end{program}.  
%%        The caption is a mendantary argument, while the label is optional.
%%
%%        \begin{program}{{\tt foo.c} does foo to the output.}
%%        main () {
%%            foo (); 
%%        }
%%
%%        foo () {
%%            printf ("foo \n");
%%        }
%%        \end{program}
%%
%%        And the typeset output from LaTeX will look like this,
%%
%%        List 1. foo.c  does foo to the output.
%%        ========================================================
%%        1   main () {
%%        2       foo (); 
%%        3   }
%%        4
%%        5   foo () {
%%        6       printf ("foo \n");
%%        7   }
%%        ========================================================
%%
%%        If this listing is to be referred, add its label as an
%%        optional argument before the caption argument, such as
%%
%%        \begin{program}[foolabel]{{\tt foo.c} does foo to output.}
%%        ... ...
%%        \end{program}
%%        See the List \ref{foolabel} above for a listing of foo.c.
%%
%% Adjustment: I can't write one environment that satisfies all of you
%%        LaTeX gorus out there, so I'll let you to do the adjustments.
%%
%%        (1) The listing is titled "List".  If you prefer something else, 
%%            change the keyword "List" to your preference.
%%        (2) This environment is named "program". Andrew used "code" in his
%%            earlier posting.  If you prefer some other names, do a global
%%            substitution on the keyword "program".
%%        (3) There are a double-line before and after the listing. If
%%            you don't like it, make it \def\double@line{}
%%        (4) The line numbers are in \tt font.  You can change it to \sl
%%            or a smaller font in the \def\line@number statement.
%%        
%% Acknowledgement: Most codes are from Leslie Lamport's verbatim environment.
%%

\def\double@line {\hrule\endgraf\vspace*{1pt}\hrule}
\def\line@number {\makebox[0.7cm][l]{\tt \theprogram@list}}

\newcounter{program@label}
\newcounter{program@list}

\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=12 |gdef|@xprogram#1\end{program}[|@gobble#1|end[program]]
|endgroup

\def\program{\@ifnextchar [{\@program}{\@program[]}}
\def\@program[#1]#2{
\refstepcounter{program@label}\par\bigskip\noindent\label{#1} 
{List \theprogram@label\ ~#2}                % use this in article style
%{List \thechapter.\theprogram@label\ ~#2}   % use this in book style
\vspace*{3pt}\double@line \@verbatim \frenchspacing\@vobeyspaces
\setcounter{program@list}{1}
\def\par{\leavevmode\endgraf}\obeylines
\def\everypar{\line@number\stepcounter{program@list}}
\@xprogram}

\def\endprogram{\endtrivlist\double@line\bigskip}

%%
%% end of program.sty
%%

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SUNY at Buffalo
cpchang@sybil.cs.buffalo.edu