Path: utzoo!attcan!uunet!munnari.oz.au!uhccux!ames!ucsd!usc!cs.utexas.edu!uwm.edu!rpi!uupsi!sunic!nuug!hod!asbjorns From: asbjorns@sfd.uit.no (Asbjorn Saetran) Newsgroups: comp.text.tex Subject: Re: 'code' environment with caption Summary: 'best' solution, long Keywords: environment verbatim caption Message-ID: <1990Jun12.113638.4294@hod.uit.no> Date: 12 Jun 90 11:36:38 GMT References: <1990Jun11.091343.10487@hod.uit.no> Sender: Asbj\o rn S\ae tran (asbjorns@sfd.uit.no) Distribution: comp.text.tex Organization: University of Tromsoe, Norway Lines: 100 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