Debian Codegraph @

texmf-dist/source/generic/textmerg/textmerg.dtx

raw
1 % \iffalse meta-comment
2 % \begin{macrocode}
3 %<*textmerg|plain>
4 \def\fileversion{2.01}
5 \def\filedate{1994/06/20}
6 \def\filename{textmerg.dtx}
7 \def\Copyright{Copyright (C) 1992,1994 Mike Piff, University of Sheffield, England}
8 %</textmerg|plain>
9 % \end{macrocode}
10 %
11 % This file is placed in the public domain. No provision is made for
12 % support of the use of the facilities herein.
13 %
14 % For updates, contact your nearest CTAN site.
15 %
16 % \fi
17 %
18 % \changes{2.01}{1994/06/20}{First version for LaTeX2e}
19 % \changes{2.01a}{2000/08/25}{[email protected] --- relaxation of licence
20 % terms}
21 %
22 %\CheckSum{349}
23 %
24 %\title{Text merges in \TeX\ and \LaTeX\thanks{This article originally
25 %appeared in TUGboat Vol 13(4), 1992, p518--523.}}
26 %\author{Mike Piff}
27 %\maketitle
28 %\thispagestyle{headings}
29 %\begin{abstract}
30 % In this article the author explains how to do some standard
31 % and not so standard word processor text merges in \TeX\ documents,
32 % using no other tools than \TeX\ itself.
33 % A common application is to the mail merge or form letter,
34 % where names and addresses are
35 % stored in a file, together with other bits of information, and a standard
36 % letter with variable fields embedded in it is customized for every name
37 % from this file.
38 % Another application is to the pretty-printing of the contents of a database.
39 %
40 % The macros described in |textmerg.sty| work equally
41 % in both plain \TeX\ and \LaTeX. However, this has meant heavy use
42 % of |\def| where |\newcommand| would have been preferable.
43 %\end{abstract}
44 %
45 %\tableofcontents
46 %
47 %\section{Introduction}
48 %It is often said that although \LaTeX\ is good at typesetting
49 %mathematics, it is wholly unsuitable for common word processor functions
50 %such as mail merges.
51 %The latter are easy to achieve in most ordinary word processors,
52 %but in its raw state \LaTeX\ is incapable of doing a mail merge, or, indeed, of
53 %generating the same block of text over and over again but with different
54 %parameters in each block, those parameters having been read from a subsidiary
55 %merge file.
56 %The latter file might possibly be the output from a database or any other
57 %program.
58 %
59 %This article aims to show the reader that such a repetitive task need not be
60 %as difficult as it at first appears. In \TeX, it is possible to hide many
61 %details of a facility inside a subsidiary style file, so that the user is
62 %unaware of what fearful processes are going on in the background.
63 %It is then possible to present the end-user with an extremely simple
64 %interface, perhaps simpler and more powerful than is available in other
65 %systems.
66 %
67 %In earlier TUGboat articles
68 %\cite{Bell:TB8-1-54,Garavelli:TB8-1-53,Lee:TB7-3-187,%
69 %McKinstry:TB8-1-60} it was shown how a standard letter could be
70 %customized by adding names and addresses from a separate file. I aim to show
71 %that it is possible to achieve far more than this with a fairly compact
72 %but general set of macros.
73 %
74 %\section{A simple example}
75 %Suppose that we have a list of student names and examination grades, one
76 %per student, and that we wish to send a letter to each student giving
77 %his/her exam grade.
78 %We must decide first what bits of information must be prepared in
79 %our subsidiary file, by looking at an example letter and finding out which
80 %items change from letter to letter.
81 %
82 %Suppose that one instance of our letter is the following,
83 %a \LaTeX\ example.
84 %\begin{verbatim}
85 %\begin{letter}{Miss Iusta Mo\\
86 % 34 Winchester Road\\
87 % Sheffield\\
88 % England}
89 % \opening{Dear Miss Mo,}
90 % This letter is to inform you
91 % that you obtained grade A in
92 % your recent examinations.
93 % \closing{Yours faithfully,}
94 %\end{letter}
95 %\end{verbatim}
96 %We can see that we need to know the student's title, forename(s), surname,
97 %address and grade to compose such a letter.
98 %
99 %One of the simplest ways of achieving this effect is to prepare a file
100 %with lines of the form
101 %\begin{verbatim}
102 %\MyLetter{Mr}...{C}
103 %\end{verbatim}
104 %for each student and then simply |\input| it into a \LaTeX\ file in which
105 %|\MyLetter| has been defined as having five parameters.
106 %A problem with this approach is that we may not be able to coax the student
107 %database into producing such a file.
108 %Another problem is that we need something more subtle if there are fifty
109 %parameters. For example, we might want to print out the
110 %contents of the student database with one page per student, but it could be
111 %that there are fifty information fields per student. Even worse,
112 %the number of pieces of information per student might not be a
113 %constant number, because, say, we are printing out fields from a related
114 %file in which marks on individual examination papers are held.
115 %
116 %We shall tackle our simple example in a way that lends itself
117 %to more generality later on, and in a form that most database programs
118 %should be capable of handling.
119 %
120 %We thus prepare a subsidiary file |results.dat|
121 %with records of five fields in it.
122 %Each student is represented by five lines of this file,
123 % \begin{macrocode}
124 %<*results>
125 Miss
126 Iusta
127 Mo
128 34 Winchester Road\\Sheffield\\England
129 A
130 Mr
131 Arthur
132 Minit
133 43 Sheffield Road\\Winchester\\England
134 C
135 %</results>
136 % \end{macrocode}
137 %and the student records appear one after another in this file.
138 %Thus both the field and record separators are carriage returns.
139 %
140 %\DescribeMacro{\Merge}
141 %\DescribeMacro{\Fields}
142 %\TeX\ itself needs to know three bits of information:
143 %\begin{enumerate}
144 % \item the name of the subsidiary file,
145 % \item the fields to read, and
146 % \item the template of the letter.
147 %\end{enumerate}
148 %We pass it this information in the following form
149 % \begin{macrocode}
150 %<*examp>
151 \documentclass[12pt]{letter}
152 \usepackage{textmerg}
153 \begin{document}
154 \Fields{\Title\Forenames\Surname
155 \Address\Grade}
156 \Merge{results.dat}{%
157 \begin{letter}{\Title\ \Forenames\
158 \Surname\\\Address}
159 \opening{Dear \Title\ \Surname,}
160 This letter is to inform you
161 that you obtained grade \Grade\ in
162 your recent examinations.
163 \closing{Yours faithfully,}
164 \end{letter}}
165 \end{document}
166 %</examp>
167 % \end{macrocode}
168 %\LaTeX\ should
169 %open the subsidiary file and, for each set of five parameters,
170 %generate a letter in the |dvi| file. When it reaches the end of
171 %the merge file, \LaTeX\ should terminate execution of the |\Merge|
172 %command and presumably finish the document.
173 %
174 %\section{A few complications}
175 %
176 %Looking at the above example in a bit more generality,
177 %we see that we are reading
178 %records of $n$ fields from the merge file and placing them into a \TeX\ document
179 %in such a way that they replace $n$ preassigned control sequences.
180 %However, it may happen that the merge file is prepared by humans, who might
181 %possibly have inserted some extra blank lines into the file. Again, it
182 %could be that certain sorts of fields might be blank,
183 %whereas others can never be blank.
184 %Perhaps it would be better to build in some degree of error recovery.
185 %
186 %\DescribeMacro{\Fields}
187 %We shall make the assumption that the first field in any record is definitely
188 %a non-blank one and that we know beforehand whether each
189 %of the others might conceivably be blank.
190 %We make a modification to our |\Fields| statement.
191 %It can contain not only
192 %the field name control sequences but also the tokens |+| and~|-|,
193 %with the following interpretation. A |+| indicates that all following
194 %fields should be re-read until a non-blank result is obtained.
195 %A |-| indicates that any following fields could conceivably be blank,
196 %subject to the restriction that the very first field is always non-blank.
197 %
198 %Thus the command
199 %\begin{verbatim}
200 %\Fields{\a+\b\c-\d}
201 %\end{verbatim}
202 %would indicate that only |\d| is allowed to be blank, because the
203 %|+| token has no effect.
204 %In
205 %\begin{verbatim}
206 %\Fields{-\a\b+-\c+\d}
207 %\end{verbatim}
208 %the initial |-| token enables blank reading of data tokens, but the very
209 %first data token is not permitted to be blank anyway.
210 %Thus |\a| is read as a non-blank token and |\b| as a
211 %possibly blank token.
212 %The sequence |+-| now switches non-blank reading on and off again,
213 %so |\c| is read as possibly blank.
214 %Finally |\d| is non-blank.
215 %
216 %Another complication we allow is that the |\Fields| command can
217 %appear several times in our file. The interpretation is that
218 %the last occurrence of |\Fields| before we encounter the |\Merge|
219 %command will indicate the fields to be read for every record. Any
220 %occurrences of |\Fields| within the merged text indicate a new list
221 %of fields to be read when that command is encountered. This lets us
222 %do some conditional processing, such as\footnote{It is assumed that
223 %{\tt\string\Mrs} expands to {\tt Mrs}.}
224 %\begin{verbatim}
225 % \ifx\Title\Mrs
226 % \Fields{\MaidenName}
227 % \fi
228 %\end{verbatim}
229 %and also gives us some flexibility about the field order later on.
230 %
231 %It should also be stressed that the undefined control sequences
232 %appearing in the template need not
233 %correspond exactly to the fields in the subsidiary file.
234 %An example might be that the subsidiary file contains the text
235 %\begin{verbatim}
236 %Spriggs, Mr Abraham L
237 %\end{verbatim}
238 %and one field read is |\FullName|.
239 %\TeX\ would then have to pre-process this
240 %name to generate its several components as used in the template.
241 %The command |\PreProcess| could be included at the start of the template.
242 %\begin{verbatim}
243 %\def\parse#1, #2 #3\endparse{%
244 % \def\Surname{#1}\def\Title{#2}%
245 % \def\Forenames{#3}}
246 %\def\PreProcess{\expandafter
247 % \parse\FullName\endparse}
248 %\end{verbatim}
249 %
250 %An alternative and simpler looking approach to reading fields from a file
251 %|\fil| might be to define each such field as
252 %follows.
253 %\begin{verbatim}
254 %\def\Field#1{\def#1{\read\fil to#1#1}}
255 %\Field\Name \Field\Address \Field\Mark
256 %\end{verbatim}
257 %The first time |\Name| is encountered, it reads its own expansion
258 %from |\fil| and then expands itself.
259 %Henceforth, it has acquired its new expansion.
260 %The disadvantage is that |\Name| must appear in the
261 %text before any subsidiary field such as |\Surname|
262 %can be used.
263 %
264 %Finally we should consider the possibility that the second parameter of
265 %|\Merge| might be too large to fit into memory. We can clearly handle
266 %this problem by allowing the second parameter merely to consist of the text
267 %|\input template|, so that the root file handles two subsidiary
268 %files, one containing the template and the other containing the fields.
269 %
270 %
271 %\section{A complicated example}
272 %
273 %We will next look at an example in which the template contains a table
274 %of indeterminate length, albeit fixed width. So far our macros work
275 %in either plain \TeX\ or in \LaTeX, but the way in which these
276 %two packages handle
277 %tables is slightly different. However, the only difference that need
278 %concern us is that \LaTeX\ uses |\\| where plain \TeX\ uses |\cr|.
279 %
280 %\DescribeMacro{\MultiRead}
281 %The example given here is in \LaTeX, but our style will work equally well
282 %in plain \TeX. In our student letter we wish to insert a table of course
283 %codes and marks. Since each student did a different number of courses, we
284 %need some way of recognizing the end of the course list in the merge file.
285 %The default will be to insert a blank line at the end of such a sub-list.
286 %Thus, the following text appears before the close of the letter template.
287 %\begin{verbatim}
288 %Here are your marks on individual papers.
289 %\begin{center}
290 % \begin{tabular}{|lr|}\hline
291 % Code&Mark\\\hline
292 % \MultiRead{2}\\\hline
293 % \end{tabular}
294 %\end{center}
295 %\end{verbatim}
296 %The merge file now has the following structure.
297 %\begin{verbatim}
298 %Title
299 %...
300 %Grade
301 %Code
302 %Mark
303 %...
304 %Code
305 %Mark
306 %\end{verbatim}
307 %$\langle${\it blank\/}$\rangle$
308 %\begin{verbatim}
309 %Title
310 %...
311 %\end{verbatim}
312 %
313 %\DescribeMacro{\MarkEnd}
314 %In other applications some of the fields in the table might possibly be blank.
315 %We then let the user change the $\langle${\it blank\/}$\rangle$
316 %line marking the end of a list to some other string of his own choosing.
317 %\begin{verbatim}
318 %\MarkEnd{***}
319 %\end{verbatim}
320 %
321 %There might be multiple tables in the same template,
322 %with their data intermingled in the merge file with main fields.
323 %The generalized |\Fields| command allows us
324 %to order the merge file however we want. Thus we could have main fields, then
325 %a table, followed by more main fields, and so on.
326 %
327 %\DescribeMacro{\Process}
328 %A final complication is that the fields appearing in a table are essentially
329 %anonymous. By this I mean that they are transferred into the table as
330 %they are, without any pre-processing possible through appearing
331 %in the template as control sequences. If we wish what appears in
332 %the table to be different from what appears in the file, a mechanism is needed
333 %to tell \TeX\ that a certain column has to be treated in a certain way.
334 %The command
335 %\begin{verbatim}
336 %\Process{n}{\foo}
337 %\end{verbatim}
338 %will replace every field $\langle f\rangle$ read into column $n$
339 %by |\foo{|$\langle f\rangle$|}|.
340 %It is even possible to do some numerical calculations by this method.
341 %
342 %Here is a \LaTeX\ example to illustrate the table processing
343 %features of |textmerg.sty|.
344 % \begin{macrocode}
345 %<*example>
346 \documentclass[12pt]{article}
347 \usepackage{textmerg}
348 \MarkEnd{***}
349 \Process{2}{\Advance}
350 \def\Advance#1{#1\addtocounter{page}{#1}}
351 \Fields{+\Name\Verb}
352 \begin{document}
353 \Merge{silly.dat}{%
354 Dear \Name,\par
355 Here is a table to \Verb\ at:
356 \Fields{\Width}%
357 \begin{tabular}{*{\Width}c}
358 \MultiRead\Width
359 \end{tabular}.\par
360 \Fields{\Adj}%
361 That was \Adj!
362 \clearpage}
363 \end{document}
364 %</example>
365 % \end{macrocode}
366 %The effect of this file is not apparent until we see |silly.dat|.
367 %It is listed here.
368 % \begin{macrocode}
369 %<*silly>
370 Mike
371 look
372 3
373 1
374 2
375 3
376 11
377 12
378 13
379 ***
380 good
381 Shelagh
382 gaze
383 2
384 21
385 22
386 23
387 24
388 ***
389 horrid
390 %</silly>
391 % \end{macrocode}
392 %
393 % The same can be done in plain \TeX.
394 % \begin{macrocode}
395 %<*plainexample>
396 \input textmerg
397 \MarkEnd{***}
398 \Process{2}{\Advance}
399 \def\Advance#1{#1\global\advance\count0by#1}
400 \Fields{+\Name\Verb}
401 \Merge{silly.dat}{%
402 Dear \Name,\par
403 Here is a table to \Verb\ at:
404 \Fields{\Width}%
405 \vbox{\halign{\hfil{} ## {}\hfil&&\hfil{} ## {}\hfil\cr
406 \MultiRead\Width\cr
407 }}.\par
408 \Fields{\Adj}%
409 That was \Adj!
410 \vfill\eject}
411 \end
412
413 %</plainexample>
414 % \end{macrocode}
415 %
416 %
417 %\StopEventually{
418 %\begin{thebibliography}{GHWW84}
419 %\bibitem[{Bel}87]{Bell:TB8-1-54}
420 %Edwin~V. {Bell, II}.
421 %\newblock {{AutoLetter: A \TeX\ form letter procedure}}.
422 %\newblock {\em TUGBoat}, 8(1):54, April 1987.
423 %
424 %\bibitem[Gar87]{Garavelli:TB8-1-53}
425 %John~S. Garavelli.
426 %\newblock {{Form letter macros}}.
427 %\newblock {\em TUGBoat}, 8(1):53, April 1987.
428 %
429 %\bibitem[Lee86]{Lee:TB7-3-187}
430 %John Lee.
431 %\newblock {{Form letters}}.
432 %\newblock {\em TUGBoat}, 7(3):187, October 1986.
433 %
434 %\bibitem[McK87]{McKinstry:TB8-1-60}
435 %Graeme McKinstry.
436 %\newblock {{Form letters}}.
437 %\newblock {\em TUGBoat}, 8(1):60, April 1987.
438 %\end{thebibliography}
439 %}
440 %
441 % \section{Identification}
442 %
443 % This package can only be used with \LaTeXe, so
444 % an appropriate message is displayed when another
445 % format is used\footnote{However, some code is inserted
446 % to allow its use with plain \protect\TeX}.
447 % \begin{macrocode}
448 %<*textmerg>
449 \NeedsTeXFormat{LaTeX2e}[1994/01/01]
450 % \end{macrocode}
451 %
452 % Announce the package name and its version:
453 % \begin{macrocode}
454 \ProvidesPackage{textmerg}[\filedate]
455 % \end{macrocode}
456 %
457 % And display it on the terminal (and the log file):
458 % \begin{macrocode}
459 \typeout{Package `textmerg' <\filedate>.}
460 \typeout{\Copyright}
461 %</textmerg>
462 % \end{macrocode}
463 %
464 % The plain \TeX\ version will simply |\input| this package file. Thus
465 % we need to know that it will understand everything in the file.
466 % \begin{macrocode}
467 %<*plain>
468 \def\NeedsTeXFormat#1[#2]{}
469 \def\ProvidesPackage#1[#2]{}
470 \def\typeout#1{\immediate\write0{#1}}
471 \input textmerg.sty
472 %</plain>
473 % \end{macrocode}
474 %
475 %
476 %\section{Implementation of the simple case}
477 %
478 % \begin{macro}{\glet}
479 %For convenience we define a frequently used combination here.
480 % \begin{macrocode}
481 %<*textmerg>
482 \def\glet{\global\let}
483 % \end{macrocode}
484 % \end{macro}
485 %
486 % \begin{macro}{\MergeFile}
487 % \begin{macro}{\InputFile}
488 %The subsidiary merge file is defined next. A macro is then defined that
489 %attempts to open it for reading. If that is unsuccessful, the file is
490 %closed and an error message is issued.
491 % \begin{macrocode}
492 \newread\MergeFile
493 \def\InputFile#1{%
494 \openin\MergeFile=#1
495 \ifeof\MergeFile
496 \errmessage{Empty merge file}%
497 \closein\MergeFile
498 \long\def\MakeTemplate##1{%
499 \def\Template{}}%
500 \else\GetInput\fi}
501 % \end{macrocode}
502 % \end{macro}
503 % \end{macro}
504 %The command |\MakeTemplate| will be used later to generate the body
505 %of the form into which fields are inserted. We redefine it if the file
506 %is empty so that it produces no text.
507 %
508 % \begin{macro}{\GetInput}
509 %Because the conditional |\ifeof| does not return true until after an
510 %unsuccessful read operation, a mechanism of looking ahead is used which is
511 %similar to that found in Pascal.
512 % \begin{macrocode}
513 \def\GetInput{{\endlinechar=-1
514 \global\read\MergeFile to\InputBuffer}}
515 % \end{macrocode}
516 % \end{macro}
517 %
518 % \begin{macro}{\SeeIfEof}
519 % \begin{macro}{\LookAgain}
520 %We set up a mechanism for deciding whether or not we have
521 %exhausted the merge file. It forces |\ifeof| to return true
522 %by skipping over blank lines.
523 % \begin{macrocode}
524 \def\SeeIfEof{%
525 \let\NextLook\relax
526 \ifeof\MergeFile
527 \else
528 \ifx\InputBuffer\empty
529 \LookAgain
530 \fi
531 \fi
532 \NextLook}
533 \def\LookAgain{\GetInput
534 \let\NextLook\SeeIfEof}
535 % \end{macrocode}
536 % \end{macro}
537 % \end{macro}
538 %
539 % \begin{macro}{\ifNonBlank}
540 % \begin{macro}{\AllowBlank}
541 % \begin{macro}{\DontAllowBlank}
542 %We can now prepare to read actual fields from the merge file. A conditional
543 %is used to indicate whether or not the field we are about to read is
544 %allowed to be blank. We also set up a mechanism for changing its value.
545 % \begin{macrocode}
546 \newif\ifNonBlank \NonBlankfalse
547 \def\AllowBlank{\global\NonBlankfalse}
548 \def\DontAllowBlank{\global\NonBlanktrue}
549 % \end{macrocode}
550 % \end{macro}
551 % \end{macro}
552 % \end{macro}
553 %
554 % \begin{macro}{\ReadIn}
555 % \begin{macro}{\MissingField}
556 %Fields are actually read by means of the following command. Its only
557 %parameter is the name of the control sequence into which the field is
558 %read.
559 % \begin{macrocode}
560 \def\ReadIn#1{%
561 \ifNonBlank\SeeIfEof\fi
562 \ifeof\MergeFile
563 \gdef#1{??}\MissingField
564 \else
565 \glet#1\InputBuffer
566 \GetInput
567 \fi}
568 \def\MissingField{%
569 \message{Missing field in file}}
570 % \end{macrocode}
571 % \end{macro}
572 % \end{macro}
573 %
574 % \begin{macro}{\GlobalFields}
575 % \begin{macro}{\Fields}
576 %The |\Fields| command places its parameter into a token
577 %register called |\GlobalFields|.
578 %This command will be redefined by the |\Merge| command.
579 % \begin{macrocode}
580 \newtoks\GlobalFields
581 \def\Fields#1{\GlobalFields{#1}}
582 % \end{macrocode}
583 % \end{macro}
584 % \end{macro}
585 %
586 % \begin{macro}{\ParseFields}
587 % \begin{macro}{\EndParseFields}
588 %When a field token list is read,
589 %each individual token within it must be either
590 %read as a field or interpreted as a blank/nonblank switch.
591 %The next token is then read by tail recursion.
592 %It is assumed that the final token in the list is |\EndParseFields|.
593 %This must be defined to expand to something unlikely to be read as a
594 %value of one of the fields, and so we |\let| it to |\ParseFields|.
595 % \begin{macrocode}
596 \def\ParseFields#1{%
597 \ifx#1\EndParseFields
598 \let\NextParse\relax
599 \else
600 \let\NextParse\ParseFields
601 \ifx#1+\DontAllowBlank
602 \else
603 \ifx#1-\AllowBlank
604 \else\ReadIn#1
605 \fi
606 \fi
607 \fi\NextParse}
608 \let\EndParseFields\ParseFields
609 % \end{macrocode}
610 % \end{macro}
611 % \end{macro}
612 %
613 % \begin{macro}{\ReadFields}
614 %We apply this command to our token register after expanding it.
615 % \begin{macrocode}
616 \def\ReadFields#1{\expandafter\ParseFields
617 \the#1\EndParseFields
618 \AllowBlank}
619 % \end{macrocode}
620 % \end{macro}
621 %
622 % \begin{macro}{\Merge}
623 % \begin{macro}{\MakeTemplate}
624 %At long last we are ready to define the |\Merge| command itself.
625 %The first parameter is the filename of the subsidiary file and the second
626 %is the template or form into which fields are inserted.
627 %Since a |\Fields| command within the |\Merge| text is meant
628 %to act immediately on the token list that follows it, we redefine it
629 %to operate in a different way.
630 % \begin{macrocode}
631 \long\def\Merge#1#2{\begingroup%
632 \InputFile{#1}%
633 \def\Fields##1{%
634 \ParseFields##1\EndParseFields}%
635 \MakeTemplate{#2}\Iterate}
636 \long\def\MakeTemplate#1{\def\Template{#1}}
637 % \end{macrocode}
638 % \end{macro}
639 % \end{macro}
640 %The grouping keeps any changes to the definition of |\MakeTemplate|
641 %local to this merge.
642 %Thus several consecutive merges can be handled
643 %within one document.
644 %The |\endgroup| is supplied by the macro |\Iterate|
645 %when the merge file has been exhausted.
646 %
647 % \begin{macro}{\Iteratecounter}
648 % \begin{macro}{\Iterate}
649 %|\Iterate| must read the fields which were declared before it was entered,
650 %substitute them into its template and repeat itself using tail recursion
651 %if the end of the merge file has not been encountered.
652 % \begin{macrocode}
653 \countdef\Iteratecounter=2
654 \Iteratecounter=0
655 \def\Iterate{%
656 \global\advance\Iteratecounter by1
657 \ReadFields\GlobalFields
658 \Template
659 \SeeIfEof
660 \ifeof\MergeFile
661 \def\NextIteration{%
662 \endgroup\closein\MergeFile}%
663 \else
664 \let\NextIteration\Iterate
665 \fi
666 \NextIteration}
667 % \end{macrocode}
668 % \end{macro}
669 % \end{macro}
670 %The point of the use of counter 2 in the above is that it is accessible to
671 %the print driver for page selection.
672 %Anyone who has started printing 150 letters, all with page number~1,
673 %only to run out of paper half way, will appreciate the use of this artifice!
674 %
675 %\section{Implementation of merged tables}
676 %
677 % \begin{macro}{\MultiCount}
678 % \begin{macro}{\MaxCount}
679 % \begin{macro}{\ifStartOfList}
680 %We set up two counters, one for the column we are reading and the
681 %other for the total number of columns in the table.
682 %We also need a conditional to mark the start of the table, so that
683 %we terminate each row correctly with |\\| or |\cr|, or nothing at all
684 %at the beginning of the first row.
685 % \begin{macrocode}
686 \newcount\MultiCount \newcount\MaxCount
687 \newif\ifStartOfList
688 % \end{macrocode}
689 % \end{macro}
690 % \end{macro}
691 % \end{macro}
692 %
693 % \begin{macro}{\MultiRead}
694 %The parameter to |\MultiRead| is the number of columns to read at a
695 %time. This command passes control to |\NextRead| after initializing
696 %certain parameters.
697 % \begin{macrocode}
698 \def\MultiRead#1{%
699 \ifnum#1>0
700 \SelectCR
701 \MakeEmpty{#1}%
702 \global\StartOfListtrue
703 \glet\NextRead\MRead
704 \AllowBlank
705 \global\MaxCount=#1
706 \NextRead
707 \fi}
708 % \end{macrocode}
709 % \end{macro}
710 %
711 % \begin{macro}{\Emptyctr}
712 % \begin{macro}{\MakeEmpty}
713 %The command |\MakeEmpty| is required by the pre-pro\-cess\-ing
714 %of each field.
715 %The idea is that the command |\csname pr|$nn$|\endcsname|,
716 %which we will loosely call |\pr|$nn$,
717 %is executed on each field in column $nn$. However, most of these commands
718 %will be undefined, and so we equate each of those that has not been
719 %defined to |\empty|.
720 % \begin{macrocode}
721 \newcount\Emptyctr
722 \def\MakeEmpty#1{\Emptyctr=0
723 \loop
724 \advance\Emptyctr by1
725 \expandafter\ifx\csname
726 pr\the\Emptyctr\endcsname\relax
727 \expandafter\glet\csname
728 pr\the\Emptyctr\endcsname\empty
729 \fi
730 \ifnum\Emptyctr<#1
731 \repeat}
732 % \end{macrocode}
733 % \end{macro}
734 % \end{macro}
735 %Note that, because of the way we are accessing it via |\csname|,
736 %the first time |\pr|$nn$ is encountered it equates to |\relax|.
737 %
738 % \begin{macro}{\Process}
739 %The command |\Process#1#2| defines |\pr#1| to mean |#2|.
740 % \begin{macrocode}
741 \def\Process#1#2{%
742 \expandafter\def\csname
743 pr#1\endcsname##1{#2{##1}}}
744 % \end{macrocode}
745 % \end{macro}
746 %
747 % \begin{macro}{\MarkEnd}
748 %We need to know how the last row is to be recognized. The default is an
749 %empty line in the merge file.
750 % \begin{macrocode}
751 \def\MarkEnd#1{\gdef\EndMarker{#1}}
752 \MarkEnd{}
753 % \end{macrocode}
754 % \end{macro}
755 %
756 % \begin{macro}{\NextLine}
757 % \begin{macro}{\NextField}
758 %We collect each row in a token register. The full row is assembled
759 %in |\NextLine| before being passed back to \TeX. Each field is
760 %read in |\TempField| and then placed temporarily into
761 %|\NextField|.
762 % \begin{macrocode}
763 \newtoks\NextLine \newtoks\NextField
764 % \end{macrocode}
765 % \end{macro}
766 % \end{macro}
767 %It is not necessary to do things this way; |\edef| can be used
768 %instead, but that approach might expand tokens prematurely.
769 %
770 % \begin{macro}{\AppendNextField}
771 %After the next field has been read, it is appended to |\NextLine|.
772 % \begin{macrocode}
773 \def\AppendNextField{%
774 \global\advance\MultiCount1
775 \NextField=\expandafter{\TempField}%
776 \edef\Append{\NextLine=
777 {\the\NextLine&\csname
778 pr\the\MultiCount\endcsname
779 {\the\NextField}}}%
780 \Append}
781 % \end{macrocode}
782 % \end{macro}
783 %
784 % \begin{macro}{\EndLine}
785 % \begin{macro}{\FinishLine}
786 %We need to insert the correct end marker after each row of the table.
787 %The token |\cr| must be disguised a little before it is acceptable
788 %in a \LaTeX\ document.
789 % \begin{macrocode}
790 \def\SelectCR{\glet\EndLine\\}%
791 %</textmerg>
792 %<plain>\def\SelectCR{\gdef\EndLine{\cr}}%
793 %<*textmerg>
794 \def\FinishLine{%
795 \ifStartOfList
796 \global\StartOfListfalse
797 \else\EndLine\fi}
798 % \end{macrocode}
799 % \end{macro}
800 % \end{macro}
801 %This makes the assumption that if |\array| is defined then we must be
802 %in \LaTeX.
803 %
804 % \begin{macro}{\StopProcessing}
805 %We need a command to finish off a table. This should reset |\NextRead|
806 %to |\AllowBlank| to terminate the tail recursion,
807 %and also do some
808 %error recovery in case the file ends prematurely in the middle of a row.
809 % \begin{macrocode}
810 \def\StopProcessing{%
811 \global\MultiCount\MaxCount
812 \glet\NextRead\AllowBlank}
813 % \end{macrocode}
814 % \end{macro}
815 %
816 % \begin{macro}{\MRead}
817 %The command |\MRead| prepares to read a row of a table. It reads a
818 %field from the merge file and checks to see whether the table has been
819 %exhausted.
820 % \begin{macrocode}
821 \def\MRead{%
822 \global\MultiCount=1
823 \ReadIn\TempField
824 \ifx\TempField\EndMarker
825 \StopProcessing
826 \else
827 \FinishLine
828 \NextField=\expandafter{\TempField}%
829 \edef\StartLine{\NextLine={\csname
830 pr1\endcsname{\the\NextField}}}%
831 \StartLine
832 \ConstructNextRow
833 \fi
834 \NextRead}
835 % \end{macrocode}
836 % \end{macro}
837 %
838 % \begin{macro}{\ConstructNextRow}
839 %Command |\ConstructNextRow| does most of the work of assembling a row of
840 %the table.
841 %It assembles |\MaxCount| fields
842 %at a time into |\NextLine| unless an error is encountered.
843 % \begin{macrocode}
844 \def\ConstructNextRow{%
845 \ifnum\MultiCount<\MaxCount
846 \loop
847 \ReadIn\TempField
848 \ifx\TempField\EndMarker
849 \glet\TempField\empty
850 \StopProcessing
851 \MissingField
852 \else
853 \ifeof\MergeFile
854 \glet\TempField\empty
855 \StopProcessing
856 \MissingField
857 \fi
858 \fi
859 \AppendNextField
860 \ifnum\MultiCount<\MaxCount
861 \repeat
862 \fi
863 \the\NextLine}
864 %</textmerg>
865 % \end{macrocode}
866 % \end{macro}
867 %
868 %
869 % \section{The documentation driver file}
870 %
871 % This is the driver file that produces this documentation.
872 % We use the document class provided by the \LaTeXe\ distribution
873 % for producing the documentation.
874 % \begin{macrocode}
875 %<*driver>
876 \documentclass{ltxdoc}
877 \RecordChanges
878 \begin{document}
879 \DocInput{textmerg.dtx}
880 \PrintIndex
881 \PrintChanges
882 \end{document}
883 %</driver>
884 % \end{macrocode}
885 %
886 %
887 %
888 %\Finale
889 %
890 %\endinput