[comp.text.tex] 'code' environment with caption

asbjorns@sfd.uit.no (Asbjorn Saetran) (06/11/90)

I'm writing a paper in which I need an environment for  short (<20 lines) 
code fragments.   The code should be typeset in \tt, just like in the verbatim
environment.  But in addition I would to be able to add a caption, for example
to give a short explanation to the code.  An example:

text text text text text text
\begin{codefragment}
        /*
         * Fill InitBlock
         */
#ifdef MONA
        E->ed_ib.ib_mode = bit(15);  /* promiscues mode */
#endif
        E->ed_ib.ib_padr[0] = etheraddr[1];
        E->ed_ib.ib_padr[1] = etheraddr[0];
\caption{Code for setting the mode bit in the Lance chip}
\end{codefragment}
more text text text text text text

This should be typeset as:

text text text text text text


        /*
         * Fill InitBlock
         */
#ifdef MONA
        E->ed_ib.ib_mode = bit(15);  /* promiscues mode */
#endif
        E->ed_ib.ib_padr[0] = etheraddr[1];
        E->ed_ib.ib_padr[1] = etheraddr[0];


	Code fragment 1.  Code for setting the mode bit in the Lande chip.


more text text text text text text

I tried to use \newenvironment and build on the 'verbatim' environment, but
failed.  Has anyone out in there in netland constructed something like this,
or does anyone have suggestions to how it could be done?

Greetings from Troms\o .
Asbj\o rn S\ae tran
University of Troms\o
Troms\o , Norway.
asbjorns@sfd.uit.no

kevin@cbmvax.commodore.com (Kevin Klop) (06/12/90)

In article <1990Jun11.091343.10487@hod.uit.no> asbjorns@sfd.uit.no (Asbjorn Saetran) writes:
>I'm writing a paper in which I need an environment for  short (<20 lines) 
>code fragments.   The code should be typeset in \tt, just like in the verbatim
>environment.  But in addition I would to be able to add a caption, for example
>to give a short explanation to the code.  An example:
>

>text text text text text text
>
>
>        /*
>         * Fill InitBlock
>         */
>#ifdef MONA
>        E->ed_ib.ib_mode = bit(15);  /* promiscues mode */
>#endif
>        E->ed_ib.ib_padr[0] = etheraddr[1];
>        E->ed_ib.ib_padr[1] = etheraddr[0];
>
>
>	Code fragment 1.  Code for setting the mode bit in the Lande chip.
>
>
>more text text text text text text
>

Funny you should ask.  It was for this prticular type of operation that I asked
about the indenting earlier.  Here's how I solved it in plain TeX:

\def\Listing{
\interspace % A macro that places a reasonable amount of blank space on the page
$$\ast\ast\ast\ast$$
\halfinterspace
\begingroup
\tt
\advance\leftskip by 36pt
\obeylines
}
\def\EndListing#1{
\endgroup
\halfinterspace
\centerline{Listing \the\ListingNo. #1}
\advance\ListingNo by 1
}
\countdef\ListingNo = 127

So, to typeset your code, I would have used:

\Listing
        /*
         * Fill InitBlock
         */
#ifdef MONA
        E->ed_ib.ib_mode = bit(15);  /* promiscues mode */
#endif
        E->ed_ib.ib_padr[0] = etheraddr[1];
        E->ed_ib.ib_padr[1] = etheraddr[0];
\EndListing{Code for setting the mode bit in the Lande chip.}


				-- Kevin --

asbjorns@sfd.uit.no (Asbjorn Saetran) (06/12/90)

Thanks to all who answered my query about a 'code' environment.  One of the
answers, with a minor change and used in conjunction with a modified 'verb-
atim' environment, fills the bill.  This posting presents the solution.

Review of problem:  I wanted an environment to include short (<20 line) 
fragments of code in a report.  The environment should be similar to the 
'verbatim' environment in that text should be typeset with \tt and all 
characters interpreted literally, but in addition it should offer the 
possibility of adding a caption (like in 'figure' and 'table' environments).
The code fragments should be numbered.

mathas_a@maths.su.oz.au (Andrew) proposed the following solution (reposted 
with permission) which satisfies all these conditions:

>Hi again. In an effort to avoid my thesis I decided that you 
>might be interested in cross-referencing these things - 
>afterall you want them numbered. Below is a version which
>allows this. If you don't want to refer to the segment later
>you give it a second argument of "N"
>
>\documentstyle[12pt]{report}
>
>\newcounter{Code}
>
>\newenvironment{code}[2]%
>    {\refstepcounter{Code}%    %** <- this is better than the \addtocounter on "version 1"
>     \if#2N{}\else\label{#2}\fi%
>     \def\CAPTION{#1}\begin{verbatim}}%
>    {\centerline{Code fragment \theCode.\quad\CAPTION}}
>
>
>\begin{document}
>   Some innocent innocuous pieces of tex
>   blaah    
>
>   \begin{code}{A lovely caption}{ONE}  %** <- can \ref{ONE} it!!
>    Some code ##!&^%&*^%!()*&!
>
>>    09840938234#$!%$#!
>   \end{verbatim}\end{code}	%% note the \end{verbatim}
>
>    More innocent text. In Fragment~\ref{ONE} we something
>    happending and get a label, but the next one needs no
>    label so we don't give it one.
>
>   \begin{code}{A lovely caption}{N}  %** <- no reference
>    Some code ##!&^%&*^%!()*&!
>
>    09840938234#$!%$#!
>   \end{verbatim}\end{code}  
>
>    After all, why should a repeated segment get two labels!
>
>\end{document}                       

I've made a minor change to this solution to ensure proper vertical spacing
between the code fragment and preceding and succeeding text.  The modified 
solution uses \vspace{\intextsep} to achieve this:

\newcounter{Code}
\newenvironment{code}[2]%
    {\refstepcounter{Code}%    
     \if#2N{}\else\label{#2}\fi%
     \def\CAPTION{#1}\vspace{\intextsep}\begin{verbatim}}%
    {\centerline{Code fragment \theCode.\quad\CAPTION}\vspace{\intextsep}}

Finally, the standard 'verbatim' environment does not deal with tabs (of 
which there are many in my code!) in a natural way (they are interpreted 
as spaces).  Michael Fine (sorry, no email address) has made an alternative 
'verbatim' environment that does this, and in addition offers the posibility 
of including entire files of code.  This is part of the heading of Mr. Fine's
.sty file:

>%    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.

I got this file by anonymous ftp some time ago, but have neither the address
of the machine I got it from, nor the address of Mr. Fine.  The file contains
no restrictions on redistribution, so if it isn't available from any other
source, and interest warrants it, I will make it available for anonymous ftp 
from a machine on our site.

Greetings from Troms\o .
Asbj\o rn S\ae tran
University of Troms\o
Troms\o , Norway.
asbjorns@sfd.uit.no