| 1 |
% dolines.doc --- version 1.0 (15 April 1991) |
| 2 |
% This is the documentation for dolines.tex. |
| 3 |
% To read this file, process it with (plain) TeX: tex dolines.doc |
| 4 |
% (To do that, you need manmac.tex, which has |
| 5 |
% the macros for typesetting the TeXbook, as explained in appendix E. |
| 6 |
% It comes free with the standard distribution of TeX.) |
| 7 |
% |
| 8 |
% NOTE: Processing this file with TeX will create empty files |
| 9 |
% index.tex and answers.tex in your directory. And if you |
| 10 |
% have a file pages.tex in your directory, TeX will probably |
| 11 |
% get very confused. |
| 12 |
% The manmac format will try to load a font called cmman; if |
| 13 |
% TeX complains that it cannot find that font you can savely |
| 14 |
% ignore that, since the font is not actually used. |
| 15 |
% |
| 16 |
% This file: copyright (c) 1991 Marcel van der Goot. |
| 17 |
% You may redistribute this file and printed versions of it. |
| 18 |
% |
| 19 |
% original: csvax.cs.caltech.edu [131.215.131.131] in pub/tex |
| 20 |
% (use anonymous ftp). Also in various archives. |
| 21 |
% |
| 22 |
% Marcel van der Goot |
| 23 |
% [email protected] |
| 24 |
% Caltech 256--80 |
| 25 |
% Pasadena, CA 91125 |
| 26 |
% (818) 356--4603 |
| 27 |
|
| 28 |
% ------ definitions for manual pages: |
| 29 |
% The documentation is typeset using Knuth's ``manmac'' macros, with a |
| 30 |
% few additional things defined here. You are free to use or adapt these |
| 31 |
% extra definitions. |
| 32 |
|
| 33 |
\input manmac % the macros used for typesetting The TeXbook (appendix E) |
| 34 |
\catcode`^=7 % manmac makes ^ active for creating an index |
| 35 |
% we don't make an index, and we need ^ to be non-active |
| 36 |
|
| 37 |
{\catcode`\^^M=12 \endlinechar=-1 % 12 = other |
| 38 |
\long\gdef\manpagehead#1^^M#2^^M#3^^M^^M% tex-file, description, version |
| 39 |
{\egroup % matches \bgroup in \manpage |
| 40 |
\par\vfill\eject |
| 41 |
\edef\rhead{\manualname: #1\unskip}% |
| 42 |
\setbox1=\hbox{\titlefont #1\quad}% |
| 43 |
\setbox3=\vtop{\advance\hsize by-\wd1 \parindent=0pt \raggedright |
| 44 |
#2\par |
| 45 |
#3\par |
| 46 |
\cnotice |
| 47 |
}% |
| 48 |
\line{\box1\box3}% |
| 49 |
\bigskip\bigskip\bigskip\noindent |
| 50 |
}% |
| 51 |
} % restore \catcode`\^^M |
| 52 |
|
| 53 |
\outer\def\manpage |
| 54 |
{\bgroup\catcode`\^^M=12 |
| 55 |
\manpagehead |
| 56 |
} |
| 57 |
|
| 58 |
\hsize=6.5in\vsize=8.9in % default plain TeX values |
| 59 |
\pagewidth=\hsize \pageheight=\vsize |
| 60 |
|
| 61 |
\def\cnotice{Copyright \copyright\ 1991 Marcel R. van der Goot} |
| 62 |
\def\manualname{Midnight Macros} |
| 63 |
|
| 64 |
% LaTeX logo, from latex.tex (except for the capital `A': manmac |
| 65 |
% doesn't have a real smallcaps font) |
| 66 |
\def\LaTeX{{\rm L\kern-.36em\raise.3ex\hbox{\sc A}\kern-.15em |
| 67 |
T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}} |
| 68 |
|
| 69 |
\tenpoint |
| 70 |
|
| 71 |
% (end of extra definitions) |
| 72 |
|
| 73 |
% -------------------- dolines.doc: |
| 74 |
|
| 75 |
\pageno=9 % follows loop.doc |
| 76 |
|
| 77 |
\manpage dolines.tex |
| 78 |
Meta-macros to separate arguments by newlines and by empty lines. |
| 79 |
Version: 1.0 (15 April 1991) (doc: 15 Apr 1991) |
| 80 |
|
| 81 |
The macros in dolines.tex are intended to help you write your own |
| 82 |
macros, rather than to typeset text directly. Consequently, it is |
| 83 |
assumed that you have some experience writing macros; in other words, |
| 84 |
assume that this whole section is preceded by a ``dangerous bend'' symbol. |
| 85 |
|
| 86 |
Dolines.tex\footnote*{To use dolines.tex you also need Midnight/loop.tex, |
| 87 |
which will be |\input| when you |\input dolines|.} |
| 88 |
helps to write macros for which line breaks inserted |
| 89 |
by the user are important. When the definitions from dolines.tex |
| 90 |
are in effect, text consists of paragraphs separated by one |
| 91 |
or more empty lines (as usual). Each paragraph consist of one |
| 92 |
or more lines, where a line is terminated by a normal newline character, |
| 93 |
`|^^M|' (normally, \TeX\ treats newlines as if they are spaces). Note that, |
| 94 |
as far as dolines.tex is concerned, a paragraph can only be |
| 95 |
terminated by an empty line, not by, e.g., |\par| or |\vskip|. |
| 96 |
To use dolines.tex, you have to define three macros, |\beforelines|, |
| 97 |
|\everyline#1|, and |\afterlines|. When dolines.tex detects the beginning |
| 98 |
of a paragraph, |\beforelines| is called. Next, for every line, |
| 99 |
|\everyline| is called with as argument a single macro. That macro |
| 100 |
has as its expansion the line that was read. (Usually, this is almost |
| 101 |
the same as saying that |\everyline| is called with the line as argument.) |
| 102 |
After the last line of the paragraph, |\afterlines| is called. |
| 103 |
No output is generated, other than what is produced by your own macros. |
| 104 |
|
| 105 |
{\def\fnm{\<\rm file name>} |
| 106 |
There are two ways of using dolines.tex. One is to read lines from |
| 107 |
a file, the other is to take the lines from the current input file. |
| 108 |
If you say |
| 109 |
\begintt |
| 110 |
\filedolines{|fnm} |
| 111 |
\endtt |
| 112 |
dolines.tex will attempt to read the file, calling |\beforelines|, |
| 113 |
|\everyline|, and |\afterlines| as described above. The file is read |
| 114 |
inside a group, so you may need to use |\global| if you do assignments |
| 115 |
within, e.g., |\everyline|. |
| 116 |
}% \fnm |
| 117 |
|
| 118 |
As an example, we define a ``poetry environment'' which reads a poem |
| 119 |
from a file, preserving the poet's line breaks. We draw horizontal |
| 120 |
lines around the poem, and put every stanza in a vbox to prevent |
| 121 |
page breaks. (It is certainly possible to write better poetry environments, |
| 122 |
we are just illustrating dolines.tex.) |
| 123 |
\beginlines |
| 124 |
|\input dolines| |
| 125 |
\nobreak\smallskip\nobreak |
| 126 |
|\def\readpoem#1{\medskip \hrule \filedolines{#1}\hrule \medskip}| |
| 127 |
\smallbreak |
| 128 |
|\def\beforelines{\medskip \vbox\bgroup}| |
| 129 |
|\def\everyline#1{\hbox{\kern\parindent \it #1}}| |
| 130 |
|\def\afterlines{\egroup \medskip} % end of the vbox| |
| 131 |
\endlines |
| 132 |
If file ``epic.tex'' contains a poem, we can typeset it with |
| 133 |
|\readpoem{epic}|. |
| 134 |
|
| 135 |
However, if you are typesetting several short rhymes, it is somewhat |
| 136 |
inconvenient to have each of them in a separate file. This is where |
| 137 |
the second method of using dolines.tex is useful. You have to define |
| 138 |
a macro |\enddolines|, the expansion of which should be |
| 139 |
the text you want to use to terminate |
| 140 |
dolines.tex's effect. You also have to define a macro |\finishdolines|. |
| 141 |
When you say |\begindolines|, dolines.tex starts reading lines, |
| 142 |
calling |\everyline| etc. This continues until dolines.tex reads a |
| 143 |
line equal to the (top level) expansion of |\enddolines|. Then your |
| 144 |
macro |\finishdolines| is called, and \TeX\ resumes normal processing. |
| 145 |
|\begindolines| and the line equal to |\enddolines| form a group |
| 146 |
(as with |\filedolines|); |\finishdolines| is called {\sl outside} |
| 147 |
this group. |
| 148 |
|
| 149 |
Here is how we can extend the poetry environment with two control |
| 150 |
sequences |\beginpoem| and |\endpoem|. We keep the above definitions |
| 151 |
of |\beforelines| etc. |
| 152 |
\beginlines |
| 153 |
|\def\beginpoem{\medskip \hrule \begindolines}| |
| 154 |
|\def\enddolines{\endpoem}| |
| 155 |
|\def\finishdolines{\hrule \medskip}| |
| 156 |
\endlines |
| 157 |
Now we can typeset a poem as follows (the English stanza is from The |
| 158 |
\TeX book): |
| 159 |
\beginlines |
| 160 |
|\beginpoem| |
| 161 |
\nobreak\medskip\nobreak |
| 162 |
|Roses are red,| |
| 163 |
|\quad Violets are blue;| |
| 164 |
|Rhymes can be typeset| |
| 165 |
|\quad With boxes and glue.| |
| 166 |
\nobreak\medskip\nobreak |
| 167 |
|Blauw de viooltjes,| |
| 168 |
|\quad Rood zijn de rozen;| |
| 169 |
|Een rijm kan gezet| |
| 170 |
|\quad Met plaksel en dozen.| |
| 171 |
\nobreak |
| 172 |
|\endpoem| |
| 173 |
\endlines |
| 174 |
Note that we never defined |\endpoem|, as it is never expanded. In fact, |
| 175 |
there is no reason to terminate the poem with a control sequence. If we |
| 176 |
define |\def\enddolines{***}|, three stars on a single line will play |
| 177 |
the r\^ole of |\endpoem| above. |
| 178 |
|
| 179 |
\danger There are a few things to be careful about. First, a line |
| 180 |
must have properly nested |{...}| groups. (Because a line is read as |
| 181 |
an argument delimited by a \<return> character.) You should not use |
| 182 |
this to fold long lines, because that will insert an explicit \<return> |
| 183 |
character with catcode 12 in the line. Second, spaces at the beginning |
| 184 |
of a line are skipped, unless you change the catcode of `\]'. This means |
| 185 |
that, for instance, there can be spaces in front of |\endpoem|. |
| 186 |
(Spaces are skipped at the beginning |
| 187 |
of a line because at that point \TeX\ is in ``state N'' --- see Chapter |
| 188 |
8 of The \TeX book.) Third, when |\everyline| is called, the line |
| 189 |
that is its argument is already tokenized. This means that a catcode |
| 190 |
change within |\everyline| cannot effect this line. Furthermore, |
| 191 |
when |\beforelines| is called, the first line is already read and |
| 192 |
tokenized. |
| 193 |
|
| 194 |
\danger |\filedolines| and |\begindolines| treat lines in pretty much |
| 195 |
the same way. There is however a significant difference in the way |
| 196 |
comments are treated. If a line has a comment (i.e., it ends with |
| 197 |
`|%...|'), |\begindolines| will miss the \<return> at the end of the |
| 198 |
line. This is normal for \TeX, it can be used to fold long lines. |
| 199 |
However, if you are using |\filedolines|, comments will be skipped, |
| 200 |
but lines are not concatenated: |\filedolines| will not miss the |
| 201 |
\<return>! (This is because |\filedolines| uses \TeX's |\read| primitive.) |
| 202 |
A rather obscure difference |
| 203 |
is that a line consisting of just `|\par|' is read as an empty line |
| 204 |
by |\filedolines|, whereas it is read as a non-empty line by |\begindolines|. |
| 205 |
If you want to use |\begindolines| to read from a file, you can say |
| 206 |
|\expandafter\begindolines\input ...|. |
| 207 |
|
| 208 |
It is possible for |\finishdolines| to take arguments, but they have to appear |
| 209 |
on the line following the line with the expansion of |\enddolines|. Suppose |
| 210 |
we change the poem environment by defining |\finishdolines| as |
| 211 |
\begintt |
| 212 |
\def\finishdolines#1% |
| 213 |
{\line{\strut \hss --- #1\hss\hss}\hrule \medskip} |
| 214 |
\endtt |
| 215 |
Hence, |\finishdolines| will print the author's name under the poem. |
| 216 |
The argument should be written on the line following |\endpoem|: |
| 217 |
\begintt |
| 218 |
\beginpoem |
| 219 |
... |
| 220 |
\endpoem |
| 221 |
{A.U. Thor} |
| 222 |
\endtt |
| 223 |
|
| 224 |
Sometimes we want to use dolines.tex to read just one paragraph by making |
| 225 |
|\enddolines| equal to an empty line. For instance, a macro |\address| which |
| 226 |
reads an address terminated by an empty line. The problem with defining |
| 227 |
|\def\enddolines{}| is that if there is an empty line before the address |
| 228 |
(or even if the rest of the line after |\address| is empty), no lines |
| 229 |
will be read at all. To ensure that at least one paragraph is read, we use |
| 230 |
|\beforelines| to define |\enddolines|: |
| 231 |
\begintt |
| 232 |
\def\beforelines{\normalbeforelines\def\enddolines{}} |
| 233 |
\let\enddolines=\relax |
| 234 |
\endtt |
| 235 |
Here, |\normalbeforelines| denotes whatever must be done at the beginning |
| 236 |
of the address. Initially |\enddolines| is equal to |\relax|, but as soon |
| 237 |
as the first non-empty line is found, |\enddolines| |
| 238 |
is redefined as an empty line. Following that, an empty line causes calls |
| 239 |
of |\afterlines| and |\finishdolines|. (A way to include an empty line in |
| 240 |
the address is to write a tie (`|~|') on a line of its own.) |
| 241 |
|
| 242 |
\danger As a more complicated example, we design a simple verbatim |
| 243 |
environment. The verbatim environment treats horizontal tabs as spaces, |
| 244 |
and considers multiple empty lines equal to a single empty line. |
| 245 |
We first write macros |\setupverbatim| and |\finishverbatim|. The first |
| 246 |
sets up the character codes for verbatim printing, the second resets |
| 247 |
those character codes. |
| 248 |
|
| 249 |
\beginlines |
| 250 |
|\def\setupverbatim| |
| 251 |
\nobreak |
| 252 |
| {\medskip \hrule \bgroup| |
| 253 |
| \def\do##1{\catcode`##1=12 }\dospecials| |
| 254 |
| \obeyspaces \tt| |
| 255 |
| }| |
| 256 |
\medskip |
| 257 |
|\def\finishverbatim{\egroup \hrule \medskip}| |
| 258 |
\medskip |
| 259 |
|{\obeyspaces\global\let|\]|=\|\]|}| |
| 260 |
\endlines |
| 261 |
Grouping is used to keep the catcode changes local. Plain \TeX's |\dospecials| |
| 262 |
macro applies the macro |\do| to all special characters (`|\|', `|$|', etc.), |
| 263 |
which, in this case, gives them all catcode 12 (``other''). But we don't want |
| 264 |
catcode 12 for spaces, because the |\tt| font has a visible space character |
| 265 |
`\]'. Therefore, spaces are made active with |\obeyspaces|, and in the |
| 266 |
last line we make an active space equal to \hbox{|\|\]}. |
| 267 |
|
| 268 |
To start setting verbatim text, we use |\beginverbatim|; |\readverbatim| |
| 269 |
is used to typeset a whole file in verbatim mode. |
| 270 |
The macros for typesetting lines and paragraphs are very straightforward. |
| 271 |
\beginlines |
| 272 |
|\def\beginverbatim{\setupverbatim\begindolines}| |
| 273 |
|\let\finishdolines=\finishverbatim| |
| 274 |
\medbreak |
| 275 |
|\def\readverbatim#1{\setupverbatim\filedolines{#1}\finishverbatim}| |
| 276 |
\medbreak |
| 277 |
|\def\beforelines{\smallskip}| |
| 278 |
|\def\everyline#1{\hbox{\strut #1}}| |
| 279 |
\nobreak |
| 280 |
|\def\afterlines{\smallskip}| |
| 281 |
\endlines |
| 282 |
|
| 283 |
Finally, we have to define |\enddolines|. We want to match |\beginverbatim| |
| 284 |
with |\endverbatim|, so we would like to do |
| 285 |
\begintt |
| 286 |
\def\enddolines{\endverbatim} |
| 287 |
\endtt |
| 288 |
The problem is that this makes |\enddolines| expand to the {\sl control |
| 289 |
sequence} |\endverbatim|. But while we are in verbatim mode, no control |
| 290 |
sequences are recognized, because `|\|' is not an escape character. |
| 291 |
What we really want is to make the expansion of |\enddolines| equal |
| 292 |
to the {\sl string} ``$\backslash$endverbatim''. This is done by |
| 293 |
temporarily making `|\|' a normal character and `|/|' an escape |
| 294 |
character: |
| 295 |
\beginlines |
| 296 |
|{\catcode`\/=0 /catcode`/\=12| |
| 297 |
| /gdef/enddolines{\endverbatim}| |
| 298 |
\nobreak |
| 299 |
|}| |
| 300 |
\endlines |
| 301 |
This completes the verbatim environment. Note that |\endverbatim| is |
| 302 |
only recognized if it appears on a line by its own and is not preceded |
| 303 |
by spaces. If you are not afraid of confusion, you can change the |
| 304 |
name |\finishverbatim| to |\endverbatim|. |
| 305 |
|
| 306 |
\bye |