3 % $Id: sverb.dtx,v 1.1 2002/02/03 20:49:03 mdw Exp $
5 % Verbatim typesetting done properly (ahem)
7 % (c) 1996 Mark Wooding
9 %----- Revision history -----------------------------------------------------
12 % Revision 1.1 2002/02/03 20:49:03 mdw
13 % Checkin for new build system.
15 % Revision 1.3 1996/11/19 21:01:18 mdw
21 % \begin{meta-comment} <general public licence>
23 %% sverb package -- handling of verbatim text
24 %% Copyright (c) 1996 Mark Wooding
26 %% This program is free software; you can redistribute it and/or modify
27 %% it under the terms of the GNU General Public License as published by
28 %% the Free Software Foundation; either version 2 of the License, or
29 %% (at your option) any later version.
31 %% This program is distributed in the hope that it will be useful,
32 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
33 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 %% GNU General Public License for more details.
36 %% You should have received a copy of the GNU General Public License
37 %% along with this program; if not, write to the Free Software
38 %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 % \begin{meta-comment} <Package preamble>
43 %<+package>\NeedsTeXFormat{LaTeX2e}
44 %<+package>\ProvidesPackage{sverb}
45 %<+package> [1996/05/08 1.3 Verbatim typesetting]
50 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
51 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
52 %% Digits \0\1\2\3\4\5\6\7\8\9
53 %% Exclamation \! Double quote \" Hash (number) \#
54 %% Dollar \$ Percent \% Ampersand \&
55 %% Acute accent \' Left paren \( Right paren \)
56 %% Asterisk \* Plus \+ Comma \,
57 %% Minus \- Point \. Solidus \/
58 %% Colon \: Semicolon \; Less than \<
59 %% Equals \= Greater than \> Question mark \?
60 %% Commercial at \@ Left bracket \[ Backslash \\
61 %% Right bracket \] Circumflex \^ Underscore \_
62 %% Grave accent \` Left brace \{ Vertical bar \|
63 %% Right brace \} Tilde \~}
66 % \begin{meta-comment}
70 \describespackage{sverb}
76 % \section{User guide}
78 % The \package{sverb} package provides some useful commands and environments
79 % for doing things with verbatim text. I prefer this code to the standard
80 % \package{verbatim} package (by Rainer Sch\"opf et al.)\ although I'm
83 % The package was written to fulfil a particular purpose: I wanted to be able
84 % to typeset ARM assembler code, 77~columns wide, on A5~paper, with the
85 % fields separated by \textit{tab} characters. It's grown up fairly
86 % organically from that, and I've tidied it when I've seen the code get too
89 % The current features are:
93 % \item A `listing' environment which typesets verbatim text nicely.
95 % \item A command to read verbatim text from an external file.
97 % \item Support for arbitrary-sized chunks of text without overflowing \TeX's
100 % \item Support for \textit{tab} characters in the verbatim text.
102 % \item An environment for typesetting demonstrations of \LaTeX\ markup.
104 % \item It all works correctly with the \package{doc} system for documenting
107 % \item A fairly hairy but quite powerful programmer interface to the yukky
108 % bits of the package.
112 % The interface is described in its own section, so that more timid readers
113 % can avoid it. That said, some of the stuff in this section gets rather
116 % Note that this package doesn't even try to do anything with short bits of
117 % verbatim text (as handled by the |\verb:...:| command). I have a separate
118 % package (\package{syntax}) which does all sorts of horrible things along
121 % \subsection{The \env{listing} environment}
123 % \DescribeEnv{listing}
124 % The main method for typesetting verbatim text is the \env{listing}
125 % environment. This works pretty much the same as the standard
126 % \env{verbatim} environment, with some exceptions, which are described
129 % So that you know exactly what you're getting, here are the rules by which
130 % \package{sverb} decides what the verbatim text actually is:
134 % \item If there's any text, other than spaces, on the same line as the
135 % `|\begin{listing}|', then the contents of the environment begins
136 % immediately after the closing brace (with all leading spaces
137 % preserved). Otherwise, the text begins on the following line.
139 % \item If there is any text, other than spaces, before the
140 % `|\end{listing}|', but on the same line, this is considered to be the
141 % last line of the text; otherwise the text is presumed to have ended
142 % at the end of the previous line.
144 % \item Any text following the |\end{listing}| on the same line is thrown
145 % away. There are good reasons for this, but they're technical.
146 % Essentially there's nothing I can do about it.
151 % \begin{demo}[w]{The \env{listing} environment}
152 %\dots in the following code:
155 %init MOV R0,#200 ;Version 2.00 please
156 % LDR R1,=&4B534154 ;Magic number (`TASK')
157 % ADR R2,appName ;Find application name
158 % SWI Wimp_Initialise ;Register as a WIMP task
161 %The next step is to \dots
165 % Tab characters are supported within the environment: tab stops are set
166 % every eighth column, although this can be modified.
168 % \subsubsection{Configuring the \env{listing} environment}
170 % The text size used in the \env{listing} environment is set by the
171 % |\listingsize| command. By default, this is set to |\small|, although you
172 % can redefine it in the document preamble, or it can be set in the document
175 % The amount by which the listing text is indented is controlled by the
176 % |\listingindent| length parameter. This is a fixed length, whose default
179 % \subsubsection{Choosing a different end-text}
181 % \DescribeEnv{listing*}
182 % The \env{listing} environment is terminated by the exact character sequence
183 % `|\end{listing}|'. This isn't too much of a problem, unless you want to
184 % include this string in the text. This is achieved by the \env{listing$*$}
185 % environment, which allows you to specify the end-text to find as an
190 % \begin{demo}{The \env{listing$*$} environment}
191 %Type a listing as follows:
193 %\begin{listing*}{<end-listing*>}
195 %This is a listing. Yes.
200 % Don't include `special' characters in your chosen end-text unless you know
203 % \subsection{Writing text to a file}
205 % \DescribeEnv{verbwrite}
206 % You can write verbatim text to a file using the \env{verbwrite}
207 % environment. The syntax is fairly straightforward:
210 % \syntax{"\\begin{verbwrite}{"<file-name>"}" \dots "\\end{verbwrite}"}
213 % The text of the environment is written to the named file. The rules about
214 % where the text actually starts and ends are the same as for the
215 % \env{listing} environment.
217 % There is also a $*$-variant, like \env{listing$*$}, which allows you to
218 % choose the end-text. The end-text is the first argument, the filename
221 % There is a restriction on the characters you can write to the file: they
222 % must all be considered `printable' by \TeX; otherwise they will be read
223 % back in as `\syntax{"^^"<chars>}' which isn't too good. Unfortunately,
224 % this includes tab characters, so you can't write them.\footnote{^^A
225 % Well, not without doing serious surgery on \TeX\ itself, anyway. }
227 % \iffalse [Example time... Ho hum. There is evilness here.] \fi
228 %\begin{verbwrite*}{<end-write>}{wrdemo1.tmp}
229 %\begin{verbwrite}{wrdemo.tmp}
230 %This is some text written to
231 %a file near the beginning of
236 % For example: \verbinput{wrdemo1.tmp}
238 % \input{wrdemo1.tmp} \iffalse [Now build the file ;-) ] \fi
240 % \subsection{The \cmd\verbinput\ command}
242 % \DescribeMacro{\verbinput}
243 % You can input a pre-prepared text file exactly as it is in the input using
244 % the |\verbinput| command. The filename is given as an argument. For
247 % \begin{demo}{The \cmd\verbinput\ command}
248 %\verbinput{wrdemo.tmp}
251 % \subsection{The \env{demo} environment}
253 % Package authors need to document their packages, and it's common to want
254 % to display examples showing the original text and the output side-by-side
255 % (or, when space doesn't permit this, one above the other). Both the
256 % \LaTeX\ book and \textit{The \LaTeX\ Companion} contain such examples.
258 % The \env{demo} environment allows such displays to be created easily. The
259 % syntax of the environment is as follows:
262 % \syntax{"\\begin{demo}["<shape>"]{"<title>"}" \dots "\\end{demo}"}
265 % The optional \synt{shape} argument can be either `|w|' (wide), or `|n|'
266 % (narrow). A `wide' shape places the input and output one above the other,
267 % while the `narrow' shape puts them side-by-side. The default shape is
268 % `narrow'. An attractive border is drawn around the display to finish it
273 %\begin{demo*}{<end-demo>}[w]{The \env{demo} environment}
274 %\begin{demo}{From the \textit{\TeX book}}
275 %\[ \sum_{p\;\rm prime}
277 % f(t)\,{\rm d}\pi(t) \]
281 % \DescribeEnv{demo*}
282 % As with the other environments created by this package, there's a
283 % $*$-variant which takes the end-text as an argument.
286 % \section{Programmer interface}
288 % This section describes the publicly available routines provided by the
289 % \package{sverb} package. Routines not described here are libable to be
290 % changed or even removed without warning, so don't use them.
292 % \subsection{Environment hooks}
294 % Each of the environments created here works in the same way. For each
295 % environment \env{foo}, there's a main command responsible for doing the
296 % work, called |\sv@foo|. This is given all the arguments of the normal
297 % environment, and two more:
301 % \item The `end-text' to search for, which marks the end of the environment.
303 % \item Some actions to perform after the text has been read and processed.
304 % This allows the calling macro to do some extra actions, like closing
309 % All the environments do is call the main command with appropriate
312 % \subsection{Reading the verbatim text}
314 % \DescribeMacro{\sv@read}
315 % The main scanning routine is |\sv@read|. It is called with three
320 % \item The end-text marking the end of the environment.
322 % \item The name of a macro (which must be a single token) which is called
323 % with a line of text as its single argument. This is given each
324 % line of text which is read from the environment in turn.
326 % \item A macro, or other sort of action, which is to be done when the text
327 % has been read and processed.
331 % The macro |\sv@read| assumes that the caller has already made some
332 % provision for removing the category codes of the following text, by either
333 % calling |\@verbatim| or using the construction
339 % \DescribeMacro{\sv@safespc}
340 % Note that any space characters you read using |\sv@read| will be catcoded
341 % as |\active|. Normally this is OK because |\obeyspaces| (or
342 % |\@vobeyspaces|) will be in effect. If you're doing something more exotic,
343 % like writing text to a file or building a command string, you can call
344 % |\sv@safespc| which defines the active-space character to be a normal
345 % whitespace-space when expanded.
349 % \section{Implementation}
351 % This section defines several macros and environments which allow verbatim
352 % typing, with a high degree of configurability. OK, so this sort of
353 % thing's been done so often before that it isn't true, but I don't really
360 % \subsection{Simple things}
362 % To help us build funny macros which involve strange and different category
363 % codes, I'll write some simple macros which I can use while building my
364 % complicated and clever ones.
366 % \begin{macro}{\@cspecials}
368 % This macro is used to assist the definition of some of the environments.
369 % It makes `|\|', `|{|' and `|}|' into `other' characters, and replaces them
370 % with `\verb"|"', `|<|' and `|>|' respectively. Note that `|[|' and `|]|'
371 % aren't used, because they make defining commands which take optional
372 % arguments awkward. Note that we open a group here. This should be closed
373 % using \verb"|endgroup" at the end of the special section.
388 % \begin{macro}{\sv@startlisting}
390 % This macro sets everything up nicely for a \env{listing}-type verbatim
394 \def\sv@startlisting{%
395 \def\par{\@@par\penalty\interlinepenalty}%
397 \leftskip\@totalleftmargin%
400 \let\do\@makeother\dospecials%
406 \lccode`\~9\lowercase{\let~\sv@vtab}%
407 \lccode`\~13\lowercase{\let~\vinput@cr}%
408 \interlinepenalty500%
414 % \subsection{Tab character handling}
416 % One of the things we want to do here is handle tab characters properly.
417 % (Here, `properly' means `moving to the next column which is a multiple of
418 % eight', the way these things were always meant to.)
420 % \begin{macro}{\settabwidth}
422 % The tabs used by our tabbed verbatim environments are set up by this
423 % routine. It sets the tab width parameter |\svtab| to 8 times the width
424 % of a |\tt| space. If you really want, you can redefine this macro.
428 \def\settabwidth{\setbox\z@\hbox{\texttt{\space}}\svtab8\wd\z@}
433 % \begin{macro}{\sv@vtab}
435 % Here we handle tabs inside verbatim environments. We expect each line to
436 % be typeset as a box, using something like
445 % The idea is that you make tab active, and set it to this macro. We stop
446 % the current box, stretch it to the right width, and start another one
447 % straight after, so nobody know the difference. The code here is straight
448 % from Appendix~D of \textit{The \TeX book}.
454 \divide\@tempdima\svtab%
455 \multiply\@tempdima\svtab%
456 \advance\@tempdima\svtab%
459 \setbox\z@\hbox\bgroup%
465 % \begin{macro}{\verbinput}
467 % We allow input from a file, by the |\verbinput| command. We display the
468 % text pretty much the same as the \env{listing} environment below.
470 % We set tab and return active, and get them to do appropriate things. This
471 % isn't actually all that hard.
478 \setbox\z@\hbox\bgroup%
485 \leavevmode\box\z@\par%
493 % \begin{macro}{\vinput@cr}
495 % This macro handles return characters while inputting text in |\verbinput|.
496 % We just output our current box, and start another.
503 \setbox\z@\hbox\bgroup%
509 % \subsection{Reading verbatim text}
511 % The traditional way of reading verbatim text is to use a delimited
512 % argument, as described in the \textit{\TeX book}. This works well-ish if
513 % the text isn't very long. A better solution would be to pick out the text
514 % line-by-line and process it like that. So this is what we do.
516 % \begin{macro}{\matcher}
518 % For long verbatim environments, we need to be able to find the end text.
519 % This is rather tricky. The solution here is rather horrible. The
520 % environment picks out each line of the text at a time, as an argument, and
521 % tests to see if it contains the text we're after. We do the test in a
522 % particularly yukky way: we add the actual target text to the end of the
523 % line, and inspect the text following the match to see if the match is at
526 % The |\matcher| macro creates a `matcher' which will test strings to see if
527 % they contain something interesting.
529 % To create a matcher, say
530 % \syntax{"\\matcher{"<cmd-name>"}{"<target>"}{"<process-cmd>"}"}. The
531 % command \synt{cmd-name} accepts a line of text as an argument and calls
532 % the \synt{process-cmd} with the text of the line before the match, or the
533 % whole lot. It also sets |\@ifmatched| appropriately.
535 % (Having spent ages coming up with this cruft myself, I found some very
536 % similar, but slightly better, code in Appendix~D. So I've changed mine to
537 % match Donald's. Anyway, credit where it's due: cheers Don.)
542 \expandafter\def\csname\string#1$match\endcsname##1#2##2##3\end{%
550 \expandafter\def\expandafter#1\expandafter##\expandafter1\expandafter{%
551 \csname\string#1$match\endcsname##1#2\relax\end%
558 % \begin{macro}{\sv@stripspc}
560 % This macro strips any trailing glue in the current horizontal list. This
561 % is fairly simple, actually: we just loop while glue is the last item. It's
562 % slightly complicated by penalties which \TeX\ puts into the list between
563 % the glue items, but we just remove them too.
568 \ifdim\lastskip=\z@\else%
569 \unskip\expandafter\sv@stripspc%
576 % \begin{macro}{\sv@percent}
578 % This macro strips a single leading percent character if there is one, and
579 % if the \env{doc} package is loaded. We store the possibly stripped text in
585 \gdef\sv@percent#1#2\relax
586 {\ifx\check@percent\@@undefined
587 \ifx#1\relax\def\@tempa{}\else
588 \def\@tempa{#1#2}\fi\else
589 \ifx#1\relax\def\@tempa{}\else
590 \ifx#1%\def\@tempa{#2}\else
591 \def\@tempa{#1#2}\fi\fi\fi}
597 % \begin{macro}{\@isspaces}
599 % We want to avoid writing the first and last lines of the environment to the
600 % file if there's nothing in them. To do this, we need to know whether a
601 % piece of text contains only space characters. This macro does this, in a
602 % rather nasty way. See the other macros below for details of how this
605 % We define |\sv@safespc| at the same time: this makes space active and
606 % expand to a space character which is not active. Neat, huh?
614 \def\@tempb{\@tempswafalse}%
616 \let\@tempb\@isspaces%
618 \def\@tempb##1\relax{}%
631 % \begin{macro}{\sv@read}
633 % This macro does the main job of reading a chunk of verbatim text. You call
637 % \syntax{"\\sv@read{"<end-text>"}{"<process-line-proc>"}{"<end-proc>"}"}
640 % The \synt{end-text} is the text to find at the end of the `environment': we
641 % stop when we find it.
643 % The \synt{process-line-proc} is a macro which is passed as an argument each
644 % line which we read from the text.
646 % The \synt{end-proc} is a macro to call once we've finished reading all of
647 % the text. This can tidy up an environment or close a file or whatever.
649 % We read the text by picking out newlines using a delimited macro. We have
650 % to be a little clever, because newlines are active in verbatim text.
652 % We will also strip `|%|' signs off the beginning if the \package{doc}
653 % package is here (\package{doc} tries to play with \LaTeX's verbatim stuff,
654 % and doesn't understand the way we do things).
660 % This code does all sorts of evil things, so I'll start by opening a group.
666 % So that I can spot the end-text, I'll create a matcher macro.
669 \matcher\@match{#1}\sv@read@ii%
672 % So that I can identify line ends, I'll make them active. I'll also make
673 % spaces active so that they can expand to whatever they ought to expand
674 % to (spaces in files, or funny \verb*" " characters or whatever.
681 % I'll use the |\if@tempswa| flag to tell me whether I ought to output the
682 % current line. This is a little messy, so I'll describe it later. I'll
683 % initialise it to false because this is the correct thing to do.
689 % Most of the job is done by two submacros. I'll define them in terms of
690 % my current arguments (to save lots of token munging). The first just
691 % extracts the next line (which ends at the next newline character) and
695 \lccode`\~13\lowercase{%
696 \def\sv@read@i##1~{\@match{##1}}%
700 % The results of the match get passed here, along with the text of the
701 % line up to the matched text.
707 % The first job to do is to maybe strip off percent signs from the beginning,
708 % to keep \package{doc} happy.
711 \sv@percent##1\relax\relax%
714 % Now I need to decide whether I ought to output this line. The method goes
715 % like this: if this is the first line (|\if@tempswa| is false) or the last
716 % (|\if@matched| is true), \emph{and} the text consists only of spaces, then
719 % The first thing to do is to notice the last line -- if |\if@matched| is
720 % true, then I'll make |\if@tempswa| false to make the first-line and
721 % last-line cases work the same way.
724 \if@matched\@tempswafalse\fi%
727 % Now if this is the first or last line, I'll examine it for spaces. This
728 % is done in a separate macro. It will set |\if@tempswa| false if the
729 % text contains only spaces.
732 \if@tempswa\else\@tempswatrue\expandafter\@isspaces\@tempa\relax\fi%
735 % Now, if |\if@tempswa| is still true, perform the \<process-line-proc> on
736 % the line of text. I'll provide a group, so that it doesn't upset me
742 \expandafter#2\expandafter{\@tempa}%
747 % The next line won't be the first one, so I'll set the flag true in
754 % Now, if that wasn't the last line, go round again; otherwise end the group
755 % I started ages ago, and do the user's \<end-proc>.
758 \if@matched\def\@tempa{\endgroup#3}\else\let\@tempa\sv@read@i\fi%
763 % Now to start the thing up. I'll read the first line.
772 % \begin{macro}{\sv@readenv}
774 % This macro works out an appropriate end-text for the current environment.
775 % If you say \syntax{"\\sv@readenv{"<macro-name>"}"}, it will expand do
776 % \begin{listinglist} \listingsize \synshorts
777 % <macro-name>"{\\"$_{12}$"end{"$_{12}$<current-env-name>"}"$_{12}$"}"^^A
778 % "{\\end{"<current-env-name>"}}"
782 % This is all done with mirrors. No, err\dots\ it's done with
792 \expandafter\expandafter\expandafter%
793 #1\expandafter\sv@readenv@i\@currenvir\@@%
795 \def\sv@readenv@i#1\@@{{|end<#1>}{\end{#1}}}
801 % \begin{macro}{\sv@verbline}
803 % This macro typesets a line in a verbatim way, so you can construct a real
804 % verbatim environment from it. It's a bit tricky in the way that it catches
805 % the last line. Don't worry about this: it's easy really. Note the
806 % |\relax| after the |\par| -- this is because \package{doc} tries to do
807 % clever things with |\par| to strip `|%|' signs out.
811 \setbox\z@\hbox{#1\sv@stripspc}%
813 \if@matched\ifhmode\par\relax\fi\else\leavevmode\par\relax\fi%
815 \leavevmode\box\z@\par\relax%
822 % \subsection{Listing environments}
824 % The \env{listing} environment is our equivalent of the standard
825 % \env{verbatim} environment. We do some slightly cleverer things, though,
826 % to make sure (for example) that even text which contains |\end{listing}|
829 % \begin{macro}{\listinglist}
830 % \begin{environment}{listinglist}
832 % This defines the layout for the \env{listing} environment. It starts a
833 % list with the appropriate shape. It's also made into an environment, so
834 % that the end-paragraph-environment bits work correctly.
836 % The |\listingindent| length parameter sets up the indentation of the
837 % listings. If there's a |\parindent| setting, I'll line listings up with
838 % that; otherwise I'll just choose something which looks right.
841 \newdimen\listingindent
843 \ifdim\parindent=\z@\listingindent1em\else\listingindent\parindent\fi%
847 % Now to define a size hook for the environment. This is fairly simple
851 \ifx\listingsize\@@undefined
852 \let\listingsize\small
856 % Now to define the environment itself. Suppress the indentation if we're
857 % first thing on a new list item, so that the listing lines up with
866 \leftmargin\listingindent%
873 \let\makelabel\relax%
876 \parfillskip\@flushglue%
879 \let\endlistinglist\endlist
885 % \begin{environment}{listing}
887 % The \env{listing} environment is the only real verbatim-like environment we
888 % create will all this kit, although it does the job very nicely.
890 % The environment indents its contents slightly, unlike \env{verbatim}, and
891 % uses a smaller typeface in an attempt to fit 77-column text on an A5~page.
892 % There is also a $*$-variant, which allows you to specify the terminating
893 % text. This enables you to include absolutely any text in the environment,
894 % including |\end{listing}|.
896 % First, we must define the |\listing| command.
902 \sv@readenv\sv@listing%
906 % Now we define the |\@listing| command, which does most of the work. We
907 % base the \env{listing} environment on a \env{list}.
910 \def\sv@listing#1#2{%
912 \sv@read{#1}\sv@verbline{\endlistinglist#2}%
916 % Now we define the starred version. The command name needs to include the
917 % `|*|' character, so we must use |\csname|. There's some hacking here to
918 % allow us to read the name using the appropriate catcodes for otherwise
919 % normal characters: \LaTeX\ activates some characters and makes them typeset
920 % themselves to suppress some ligaturing.
923 \expandafter\def\csname listing*\endcsname{%
928 \def\@tempa##1{\endgroup\sv@listing{##1}{\end{listing*}}}%
935 % \begin{environment}{ignore}
937 % The \env{ignore} environment entirely ignores its contents. Anything at
938 % all may be put into the environment: it is discarded utterly.
940 % We define some macros for defining ignoring environments, because this can
941 % be useful for version control, possibly.
946 \let\do\@makeother\dospecials%
947 \sv@read{#1}\@gobble{\@esphack#2}%
949 \def\ignore{\sv@readenv\sv@ignore}
951 \expandafter\let\csname #1\endcsname\ignore%
954 \expandafter\def\csname #1\endcsname{\endgroup}%
955 \expandafter\def\csname end#1\endcsname%
956 {\begingroup\def\@currenvir{#1}}%
962 % \subsection{The \env{verbwrite} environment}
964 % The \env{verbwrite} environment allows text to be written to a file in a
965 % verbatim way. Note that tab characters don't work, because \TeX\ refuses
968 % \begin{macro}{\sv@write}
970 % As seems to be traditional now, we first define a general hookable macro
971 % which allows a caller to specify the end-text and what to do afterwards.
974 \newwrite\sv@writefile
978 \let\do\@makeother\dospecials%
980 \sv@read{#1}\sv@writeline{\sv@endwrite#2}%
982 \def\sv@writeline#1{%
983 \immediate\write\sv@writefile{#1}%
993 % \begin{environment}{verbwrite}
995 % Now we can define the actual environment. We define a $*$-variant which
996 % allows the user to specify the end-text, just to make sure.
1000 \immediate\openout\sv@writefile#1\relax%
1001 \sv@readenv\sv@write%
1003 \def\endverbwrite{\immediate\closeout\sv@writefile}
1004 \expandafter\def\csname verbwrite*\endcsname#1#2{%
1005 \immediate\openout\sv@writefile#2\relax%
1006 \sv@write{#1}{\immediate\closeout\sv@writefile\end{verbwrite*}}%
1012 % \subsection{The \env{demo} environment}
1014 % By way of tying all of this together, I present an environment for
1015 % displaying demonstrations of \LaTeX\ markup. We read the contents of the
1016 % environment, write it to a temporary file, and read it back twice,
1017 % typesetting it the first time and displaying it verbatim the second time.
1019 % \begin{macro}{\sv@demoname}
1021 % This macro expands to the filename to use for the temporary data. To
1022 % allow the package documentation to demonstrate the \env{demo} environment
1023 % itself, we need to keep a nesting count. This avoids too much hackery,
1024 % which unfortunately appears to plague all of my \TeX\ code.
1027 \newcount\sv@nestcount
1028 \def\sv@demoname{demo\number\sv@nestcount.tmp}
1033 % \begin{macro}{\sv@demo}
1035 % As for listing, we do all the business through a private macro. This is
1036 % good because it means we can leave the main macro readable. The argument
1037 % is the end-text to spot.
1041 \@ifnextchar[{\sv@demo@i{#1}{#2}}{\sv@demo@i{#1}{#2}[n]}%
1043 \def\sv@demo@i#1#2[#3]#4{%
1044 \advance\sv@nestcount by\@ne%
1045 \immediate\openout\sv@writefile\sv@demoname\relax%
1047 \immediate\closeout\sv@writefile%
1048 \sv@dodemo{#2}{#3}{#4}%
1055 % \begin{environment}{demo}
1057 % This is the real environment. We provide \env{demo$*$} too, to allow the
1058 % user to choose the end-text.
1061 \def\demo{\sv@readenv\sv@demo}
1062 \expandafter\def\csname demo*\endcsname#1{\sv@demo{#1}{\end{demo*}}}
1067 % \begin{macro}{\sv@dodemo}
1069 % First, let's define some common bits of code in the stuff below. The
1070 % minipages used to typeset the material has some clever stuff to avoid
1071 % strange spacing in the output.
1075 \begin{minipage}[t]{\@tempdima}%
1082 \par\unpenalty\unskip%
1090 % This is the macro which actually typesets the demonstration.
1093 \def\sv@dodemo#1#2#3{%
1096 % Now work out some values. We set |\hsize| to the line width leaving 2\,em
1097 % of space on either side. The size of the minipages is calculated depending
1098 % on the shape of the demonstration. This is all fairly simple.
1102 \@tempdima\linewidth%
1103 \advance\@tempdima-2em%
1106 \advance\@tempdima-2em%
1108 \advance\@tempdima-3em%
1113 % Now we open a big vertical box, and put in a header to mark off the
1118 \setbox\z@\hbox{\strut\enspace#3\enspace\strut}%
1120 \advance\@tempdimb-.5\ht\z@%
1121 \ht\z@\@tempdimb\dp\z@\@tempdimb%
1122 \noindent\hskip1em\vtop{%
1125 \raise\@tempdimb\box\z@%
1129 \hb@xt@\hsize{\vrule\@height5\p@\hfil\vrule\@height5\p@}%
1133 % Now we insert the output text in the first minipage. I'll force `|%|'
1134 % to be a comment character, in case something like \package{doc} has had its
1139 \noindent\hbox{}\hskip1em%
1141 \catcode`\%14\relax%
1142 \input{\sv@demoname}%
1146 % Insert some kind of separation between the two. In `wide' format, we start
1147 % a new line, and put a ruleoff between the two. In `narrow' format, we just
1152 \vskip8\p@\hrule\vskip8\p@%
1158 % Now we put the verbatim copy of the text in the other minipage.
1163 \verbinput\sv@demoname%
1167 \hb@xt@\hsize{\vrule\@height5\p@\hfil\vrule\@height5\p@}%
1172 \vskip\baselineskip%
1179 % That's all there is. Have fun.
1185 % \hfill Mark Wooding, \today