summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/newfile/newfile.dtx
blob: 46444979c15b68538f44fd66a69a9bbf9f291a0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
% \iffalse meta-comment
% newfile.dtx
% Author: Peter Wilson, Herries Press
% Maintainer: Will Robertson (will dot robertson at latex-project dot org)
% Copyright 2002--2004 Peter R. Wilson
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any 
% later version: <http://www.latex-project.org/lppl.txt>
%
% This work has the LPPL maintenance status "maintained".
% The Current Maintainer of this work is Will Robertson.
%
% This work consists of the files listed in the README file.
%
% 
%<*driver>
\documentclass{ltxdoc}
\EnableCrossrefs
\CodelineIndex
\setcounter{StandardModuleDepth}{1}
\begin{document}
  \DocInput{newfile.dtx}
\end{document}
%</driver>
%
% \fi
%
% \CheckSum{329}
%
% \DoNotIndex{\',\.,\@M,\@@input,\@addtoreset,\@arabic,\@badmath}
% \DoNotIndex{\@centercr,\@cite}
% \DoNotIndex{\@dotsep,\@empty,\@float,\@gobble,\@gobbletwo,\@ignoretrue}
% \DoNotIndex{\@input,\@ixpt,\@m}
% \DoNotIndex{\@minus,\@mkboth,\@ne,\@nil,\@nomath,\@plus,\@set@topoint}
% \DoNotIndex{\@tempboxa,\@tempcnta,\@tempdima,\@tempdimb}
% \DoNotIndex{\@tempswafalse,\@tempswatrue,\@viipt,\@viiipt,\@vipt}
% \DoNotIndex{\@vpt,\@warning,\@xiipt,\@xipt,\@xivpt,\@xpt,\@xviipt}
% \DoNotIndex{\@xxpt,\@xxvpt,\\,\ ,\addpenalty,\addtolength,\addvspace}
% \DoNotIndex{\advance,\Alph,\alph}
% \DoNotIndex{\arabic,\ast,\begin,\begingroup,\bfseries,\bgroup,\box}
% \DoNotIndex{\bullet}
% \DoNotIndex{\cdot,\cite,\CodelineIndex,\cr,\day,\DeclareOption}
% \DoNotIndex{\def,\DisableCrossrefs,\divide,\DocInput,\documentclass}
% \DoNotIndex{\DoNotIndex,\egroup,\ifdim,\else,\fi,\em,\endtrivlist}
% \DoNotIndex{\EnableCrossrefs,\end,\end@dblfloat,\end@float,\endgroup}
% \DoNotIndex{\endlist,\everycr,\everypar,\ExecuteOptions,\expandafter}
% \DoNotIndex{\fbox}
% \DoNotIndex{\filedate,\filename,\fileversion,\fontsize,\framebox,\gdef}
% \DoNotIndex{\global,\halign,\hangindent,\hbox,\hfil,\hfill,\hrule}
% \DoNotIndex{\hsize,\hskip,\hspace,\hss,\if@tempswa,\ifcase,\or,\fi,\fi}
% \DoNotIndex{\ifhmode,\ifvmode,\ifnum,\iftrue,\ifx,\fi,\fi,\fi,\fi,\fi}
% \DoNotIndex{\input}
% \DoNotIndex{\jobname,\kern,\leavevmode,\let,\leftmark}
% \DoNotIndex{\list,\llap,\long,\m@ne,\m@th,\mark,\markboth,\markright}
% \DoNotIndex{\month,\newcommand,\newcounter,\newenvironment}
% \DoNotIndex{\NeedsTeXFormat,\newdimen}
% \DoNotIndex{\newlength,\newpage,\nobreak,\noindent,\null,\number}
% \DoNotIndex{\numberline,\OldMakeindex,\OnlyDescription,\p@}
% \DoNotIndex{\pagestyle,\par,\paragraph,\paragraphmark,\parfillskip}
% \DoNotIndex{\penalty,\PrintChanges,\PrintIndex,\ProcessOptions}
% \DoNotIndex{\protect,\ProvidesClass,\raggedbottom,\raggedright}
% \DoNotIndex{\refstepcounter,\relax,\renewcommand,\reset@font}
% \DoNotIndex{\rightmargin,\rightmark,\rightskip,\rlap,\rmfamily,\roman}
% \DoNotIndex{\roman,\secdef,\selectfont,\setbox,\setcounter,\setlength}
% \DoNotIndex{\settowidth,\sfcode,\skip,\sloppy,\slshape,\space}
% \DoNotIndex{\symbol,\the,\trivlist,\typeout,\tw@,\undefined,\uppercase}
% \DoNotIndex{\usecounter,\usefont,\usepackage,\vfil,\vfill,\viiipt}
% \DoNotIndex{\viipt,\vipt,\vskip,\vspace}
% \DoNotIndex{\wd,\xiipt,\year,\z@}
%
% \changes{v1.0}{2002/06/22}{First public release}
% \changes{v1.0a}{2002/07/07}{Corrected documentation}
% \changes{v1.0b}{2004/05/10}{Changed license from LPPL v1.0 to v1.3}
% \changes{v1.0c}{2009/09/03}{New maintainer (Will Robertson)}
%
% \def\dtxfile{newfile.dtx}
% \def\fileversion{v1.0}
% \def\filedate{2002/06/22}
% \def\fileversion{v1.0a}
% \def\filedate{2002/07/07}
% \def\fileversion{v1.0b}
% \def\filedate{2004/05/10}
% \newcommand*{\Lpack}[1]{\textsf {#1}}           ^^A typeset a package
% \newcommand*{\Lopt}[1]{\textsf {#1}}            ^^A typeset an option
% \newcommand*{\file}[1]{\texttt {#1}}            ^^A typeset a file
% \newcommand*{\Lcount}[1]{\textsl {\small#1}}    ^^A typeset a counter
% \newcommand*{\pstyle}[1]{\textsl {#1}}          ^^A typeset a pagestyle
% \newcommand*{\Lenv}[1]{\texttt {#1}}            ^^A typeset an environment
%
% \title{The \Lpack{newfile} package\thanks{This
%        file (\texttt{\dtxfile}) has version number \fileversion, last revised
%        \filedate.}}
%
% \author{
%   Author: Peter Wilson, Herries Press \\
%   Maintainer: Will Robertson \\
%   \texttt{will dot robertson at latex-project dot org}
% }
% \date{\filedate}
% \maketitle
% \begin{abstract}
%    The \Lpack{newfile} package provides convenient user level commands 
% for reading and writing new files during a LaTeX run.
% \end{abstract}
% \tableofcontents
%
%
% \section{Introduction}
%
% TeX has a maximum of 16 input and 16 output \emph{streams} 
% for reading and writing
% files. The \Lpack{newfile} package provides means of associating
% several different files with a particular stream.
%
% The \Lpack{newfile} package requires the \Lpack{verbatim} 
% package~\cite{VERBATIM}, which is one of the required packages in
% a LaTeX distribution.
%
% This manual is typeset according to the conventions of the
% \LaTeX{} \textsc{docstrip} utility which enables the automatic
% extraction of the \LaTeX{} macro source files~\cite{GOOSSENS94}.
%
%    Section~\ref{sec:usc} describes the usage of the package.
% Commented source code for the package is in Section~\ref{sec:code}.
%
% \section{The \Lpack{newfile} package} \label{sec:usc}
%
%
% \DescribeMacro{\newoutputstream}
% \DescribeMacro{\newinputstream}
%    The command |\newoutputstream|\marg{stream} creates a new stream
% called \meta{stream} for writing out text and commands. The \meta{stream}
% should be just alphabetic characters with no spaces; for example |myout|.
% The command |\newinputstream|\marg{stream} creates a new stream for
% reading from a file. The \meta{stream} names must be unique --- you cannot
% use the same name for both an input and an output stream.
%
% If you try and create too many streams, TeX will tell you via an error 
% message.
%
% These two commands also attempt to create two other new commands, 
% called respectively
% |\atstreamopen<stream>| and |\atstreamclose<stream>|. If these commands
% already exist then they are left untouched, otherwise they are
% created (like using |\providecommand{\atstreamopen<stream}{}|). 
% For example if
% if you have used |mystr| as the name of a stream (either input or output),
% then the macros |\atstreamopenmystr| and |\atstreamclosemystr| are
% defined; by default they do nothing, but you can |\renewcommand| them
% to do something.
%
% \DescribeMacro{\openoutputfile}
% The macro |\openoutputfile|\marg{filename}\marg{stream} opens the
% file called \meta{filename} and the output stream \meta{stream}. It
% then attaches the file to the stream for writing and calls the macro
% |\atstreamopen<stream>|. Any pre-existing contents of \meta{filname}
% are deleted.
%
% \DescribeMacro{\closeoutputstream}
% The macro |\closeoutputstream|\marg{stream} calls the macro
% |\atstreamclose<stream>|, closes the output stream
% \meta{stream} and closes whatever file
% is currently attached to \meta{stream}. It then detaches the file from
% the stream.
%
% \DescribeEnv{writeverbatim}
% The |writeverbatim| environment takes one argument, the name of an output
% stream, which must be open. The contents of the environment are written
% out verbatim to the file currently attached to the stream.
%
% \DescribeMacro{\addtostream}
% The command |\addtostream|\marg{stream}\marg{text} writes \meta{text}
% to the file currently attached to the output stream \meta{stream}, which
% must be open. Any commands within \meta{text} will be processed before
% being written. To prevent this, put |\protect| before any command that
% you do not want expanding.
%
% \DescribeMacro{\openinputfile}
% The macro |\openinputfile|\marg{filename}\marg{stream} opens the
% file called \meta{filename} and the input stream \meta{stream}. It
% then attaches the file to the stream for reading and calls the macro
% |\atstreamopen<stream>|. It is an error if \meta{filename} can not be found.
%
% \DescribeMacro{\closeinputstream}
% The macro |\closeinputstream|\marg{stream} calls the macro
% |\atstreamclose<stream>|, closes the input stream
% \meta{stream} and closes whatever file
% is currently attached to \meta{stream}. It then detaches the file from
% the stream.
%
% \DescribeMacro{\readstream}
% The macro |\readstream|\marg{stream} reads the contents of the file that
% is currently associated with the input stream \meta{stream}. This provides
% the same functionality as |\input|\marg{filename} does.
%
% \DescribeMacro{\readaline}
% The macro |\readaline|\marg{stream} reads what TeX considers to be
% one line from the file that
% is currently associated with the input stream \meta{stream}. Multiple lines
% can be read by calling |\readaline| multiple times. A warning is issued
% if there are no more lines to be read (i.e., the end of the file has
% been reached).
%
%
% \DescribeMacro{\readverbatim}
% The macro |\readverbatim|\marg{stream} reads the contents of the file that
% is currently associated with the input stream \meta{stream} as |verbatim|
% text. This provides
% the same functionality as the \Lpack{verbatim} package's
% |\verbatiminput|\marg{filename} command does.
%
% \DescribeMacro{\streamvfont}
% Text read in verbatim is typeset using the font specified by
% |\streamvfont|\marg{font}. The default is 
% |\streamvfont{\normalfont\ttfamily}|. To typeset in a smaller font, try
% for example,
% |\streamvfont{\small\ttfamily}|. 
%
%
% \DescribeMacro{\numbervstream}
% \DescribeMacro{\marginnumbervstream}
% \DescribeMacro{\streamvnumfont}
% The declaration |\numbervstream| causes |\readverbatim| to number each 
% line it reads.
% The declaration |\marginnumbervstream| is similar to |\numbervstream|
% except it puts the numbers in the margin.
% The |streamvline| counter is used for the line numbering. Both
% the numbering declarations (re)set it to zero. The numbers are
% typeset in the font specified by |\streamvnumfont|\marg{font}. 
% The default is |\streamvnumfont{\footnotesize}|.
%
% \DescribeMacro{\plainvstream}
% The declaration |\plainvstream| stops any numbering. The default
% is |\plainvstream|.
%
% \DescribeEnv{comment}
% The |comment| environment, which is part of the \Lpack{verbatim}
% package, throws away everything inside the environment.
%
% The \Lpack{verbatim} package~\cite{VERBATIM} provides some other 
% facilities, apart from
% the |comment| environment, and its documentation explains them.
%
% \section{Examples}
%
% \subsection{Output stream}
%
% This example is inspired by the \Lpack{endfloat} package~\cite{ENDFLOAT}
% which provides a more sophisticated approach than shown below. The
% example demonstrates the use of an output stream.
%
% Suppose that in an \Lpack{article} class document you want all the
% figures to be collected at the end, but to specify them at the appropriate
% places in the body of the text.
%
%    \begin{macrocode}
%<*ex>
%    \end{macrocode}
%    \begin{macrocode}
\documentclass{article}
\usepackage{newfile}
...
%    \end{macrocode}
% Create a new output stream called |figs| and open it to write to file
% |figures.tex|.
%    \begin{macrocode}
\newoutputstream{figs}
\openoutputfile{figures.out}{figs}
...
%    \end{macrocode}
% Write out verbatim the first |figure| environment to the |figs| stream.
%    \begin{macrocode}
\begin{writeverbatim}{figs}
\begin{figure}
...
\end{figure}
\end{writeverbatim}
...
%    \end{macrocode}
% After the last |figure| is written to |figs|, close the |figs| output
% stream, which also closes the |figures.out| file. 
%    \begin{macrocode}
\begin{writeverbatim}{figs}
\begin{figure}
...
\end{figure}
\end{writeverbatim}
\closeoutputstream{figs}
...
%    \end{macrocode}
% At the end |\input| |figures.out| to typeset the figures.
%    \begin{macrocode}
\input{figures.out}
\end{document}
%    \end{macrocode}
%
% \subsection{An output and input stream}
%
% This example is the kind of thing that the \Lpack{answers} 
% package~\cite{ANSWERS} does rather better. The example illustrates
% the use of an output and an input stream.
%
% Suppose you are writing a document that includes questions and answers
% and you want all the answers at the end. It is most convenient if you
% can write the answer to each question as it is posed, and then only
% print them at the end.
%
% \setcounter{CodelineNo}{0}
%    \begin{macrocode}
\documentclass{article}
\usepackage{newfile}
...
%    \end{macrocode}
% Create new output and input streams called |ans| and |ansin|. Just to 
% demonstrate the use of the |\atstreamopen...| command, redefine
% |\atstreamopenansin| so that it will start a section called 
% `Answers to all the questions'. Then open the |ans| stream to write to
% a file called |answers.all|.
%    \begin{macrocode}
\newoutputstream{ans}
\newinputstream{ansin}
\renewcommand{\atstreamopenansin}{\section{Answers to all the questions}}
\openoutputfile{answers.all}{ans}
...
%    \end{macrocode}
% For each question, write out the answer verbatim to the |ans| stream.
%    \begin{macrocode}
This is a question.
\begin{writeverbatim}{ans}
This is the answer to a question.
\end{writeverbatim}
...
%    \end{macrocode}
% At the end, close the |ans| stream and open the |ansin| stream to read from
% the file |answers.all|. Opening the stream will also start the new
% `Answers to all the questions' section (from |\atstreamopenansin|). Then
% read from the |ansin| stream.
%    \begin{macrocode}
\closeoutputstream{ans}
\openinputfile{answers.all}{ansin}
\readstream{ansin}
%    \end{macrocode}
% Finally, close the |ansin| stream, and we are done.
%    \begin{macrocode}
\closeinputstream{ansin}
\end{document}
%    \end{macrocode}
%
% If desired, instead of inserting the answers at the end of the question
% document, |answers.all| could have been |\input| into a seperate
% answer document.
%
% Along the same lines as above, perhaps the original document is
% a \Lpack{book} class, with questions (and answers) at the end of each
% chapter. A seperate answer file could be produced for each chapter, like:
% \setcounter{CodelineNo}{0}
%    \begin{macrocode}
...
\newoutputstream{ansout}
\newinputstream{ansin}
...
\chapter{First chapter} % chapter 1
\openoutputfile{\jobname\thechapter.ans}{ansout}
%    \end{macrocode}
% The |\jobname| is the name of the main LaTeX document file, without
% the |.tex| extension, so if the name of the LaTeX source file is 
% |mybook.tex| the above line creates a file called |mybook1.ans|.
%    \begin{macrocode}
...
\begin{writeverbatim}{ansout}
An answer
\end{writeverbatim}
...
\closeoutputstream{ansout}
\chapter{Another chapter} % chapter N
\openoutputfile{\jobname\thechapter.ans}{ansout}
...
\closeoutputstream{ansout}

\chapter{Answers}

\section{Chapter 1}
\openinputstream{\jobname1.ans}{ansin}
\readstream{ansin}
\closestream{ansin}
...
\section{Chapter N}
\openinputstream{\jobnameN.ans}{ansin}
\readstream{ansin}
\closestream{ansin}
...
%    \end{macrocode}
%
% The above example shows how you can associate different files with a single
% stream.
%
% \subsection{Multiple streams}
%
% This is a more complex example, again inspired by the \Lpack{endfloat}
% package. In a \Lpack{book} class document you want 
% all the
% figures and tables to be collected at the end, 
% but to specify them at the appropriate
% places in the body of the text.
%
% \setcounter{CodelineNo}{0}
%     \begin{macrocode}
\documentclass{book}
\usepackage{newfile}
...
%    \end{macrocode}
% Create and open two new output streams, one for figures (|figs|) and the 
% other (|tabs|) for tables.
% For demonstration purposes, also create a new input stream called figtab.
%    \begin{macrocode}
\newoutputstream{figs}
\openoutputfile{figures.out}{figs}
\newoutputstream{tabs}
\openoutputfile{tables.out}{tabs}
\newinputstream{figtab}
...
%    \end{macrocode}
% There is a slight difficulty with this example, as in the \Lpack{book}
% class, figures and table numbers start anew with each chapter and the
% chapter number is preprended to the sequence number. We can make a
% start on solving this by creating a new pseudo chapter number and changing
% the default definitions of the figure and table numbers (which are
% |\renewcommand{\thefigure}{\thechapter.\arabic{figure}}| and similarly
% for tables).
%    \begin{macrocode}
\newcounter{pseudochapter}
\renewcommand{\thepseudochapter}{\arabic{pseudochapter}}
\renewcommand{\thefigure}{\thepseudochapter.\arabic{figure}}
\renewcommand{\thetable}{\thepseudochapter.\arabic{table}}
...
%    \end{macrocode}
% Do the usual things at the start of the document.
%    \begin{macrocode}
\begin{document}
\maketitle
\tableofcontents
\listoffigures
..
%    \end{macrocode}
% At the start of each chapter we need to set the |pseudochapter| counter
% to the chapter number, and write this to the output streams so
% that there is a record of which chapters the figures and tables came from
% Normally, each chapter resets the figure and table numbers, but as
% these will now be at the end, we have to fake the start of chapters
% in the output files. We can do all of this by the |\addtostream| macro.
%    \begin{macrocode}
\chapter{First}
%    \end{macrocode}
% The next bit of code will result in \\
% |\setcounter{pseudochapter}{N}| \\
% where |N| is the number of this chapter, appearing in the output files.
%    \begin{macrocode}
\addtostream{figs}{\protect\setcounter{pseudochapter}{\thechapter}}
\addtostream{tabs}{\protect\setcounter{pseudochapter}{\thechapter}}
%    \end{macrocode}
% The next bit of code results in the following two lines in the output files:\\
% |\refstepcounter{chapter}| \\
% |\addtocounter{chapter}{-1}| \\
% The first of these has the effect of resetting the figure and table 
% counters by increasing the chapter counter by one. The second line resets
% the chapter counter back to its original value.
%    \begin{macrocode}
\addtostream{figs}{\protect\refstepcounter{chapter}}
\addtostream{figs}{\protect\addtocounter{chapter}{-1}}
\addtostream{tabs}{\protect\refstepcounter{chapter}}
\addtostream{tabs}{\protect\addtocounter{chapter}{-1}}
%    \end{macrocode}
% Write out verbatim each figure to the |figs| stream.
%    \begin{macrocode}
\begin{writeverbatim}{figs}
\begin{figure}
...
\end{figure}
\end{writeverbatim}
...
%    \end{macrocode}
% and write each table verbatim to the |tabs| stream.
%    \begin{macrocode}
\begin{writeverbatim}{tabs}
\begin{table}
...
\end{table}
\end{writeverbatim}
...
%    \end{macrocode}
% When we have done all the chapters, figures and tables, 
% close the two output streams.
%    \begin{macrocode}
\closeoutputstream{figs}
\closeoutputstream{tabs}
%    \end{macrocode}
% Then if we want the figures to be typeset before the tables, open
% the |figtab| stream to read from |figures.out|, read the stream and 
% close it.
%    \begin{macrocode}
\openinputfile{figures.out}{figtab}
\readstream{figtab}
\closestream{figtab}
%    \end{macrocode}
% We can use the same |figtab| stream for reading the tables.
%    \begin{macrocode}
\openinputfile{tables.out}{figtab}
\readstream{figtab}
\closestream{figtab}
\end{document}
%    \end{macrocode}
%    \begin{macrocode}
%</ex>
%    \end{macrocode}
%
% Note that the \Lpack{endfloat} package~\cite{ENDFLOAT} will produce the
% same effect as the previous example, but much simply.
%
%
%    \StopEventually{}
%
%
% \section{The package code} \label{sec:code}
%
%    This package can only be used with LaTeX2e, and requires the
% \Lpack{verbatim} package~\cite{VERBATIM}, which is one of the required 
% packages for a LaTeX distribution.
% \setcounter{CodelineNo}{0}
%    \begin{macrocode}
%<*outin>
\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesPackage{newfile}[2009/09/03 v1.0c Output and input files]
\RequirePackage{verbatim}
%    \end{macrocode}
%
% To try and avoid name clashes with other packages, each internal macro
% in this package includes the character string `|stre@m|'.
%
% \begin{macro}{\newoutputstream}
% |\newoutputstream|\marg{stream} creates a new output stream called \meta{stream}.
% Different files may be associated with the \meta{stream}. Note that
% TeX permits no more than 16 output streams.
%    \begin{macrocode}
\newcommand{\newoutputstream}[1]{%
  \@ifundefined{#1outstre@m}%
    {\expandafter\newwrite\csname #1outstre@m\endcsname
     \csname newif\expandafter\endcsname
       \csname ifstre@m#1open\endcsname
     \global\csname stre@m#1openfalse\endcsname
     \expandafter\ifx\csname atstreamopen#1\endcsname\relax
       \global\@namedef{atstreamopen#1}{}%
     \fi
     \expandafter\ifx\csname atstreamclose#1\endcsname\relax
       \global\@namedef{atstreamclose#1}{}%
     \fi
    }%
    {\PackageError{newfile}{Output stream #1 is already defined}{\@ehc}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newinputstream}
% |\newinputstream|\marg{stream} creates a new input stream called \meta{stream}.
% Different files may be associated with the \meta{stream}. Note that
% TeX permits no more than 16 input streams.
%    \begin{macrocode}
\newcommand{\newinputstream}[1]{%
  \@ifundefined{#1instre@m}%
    {\expandafter\newread\csname #1instre@m\endcsname
     \csname newif\expandafter\endcsname
       \csname ifstre@m#1open\endcsname
     \global\csname stre@m#1openfalse\endcsname
     \expandafter\ifx\csname atstreamopen#1\endcsname\relax
       \global\@namedef{atstreamopen#1}{}%
     \fi
     \expandafter\ifx\csname atstreamclose#1\endcsname\relax
       \global\@namedef{atstreamclose#1}{}%
     \fi
    }%
    {\PackageError{newfile}{Input stream #1 is already defined}{\@ehc}}}

%    \end{macrocode}
% \end{macro}
%
%    Some checking macros will be useful as some of the checks occur in
% multiple places.
%
% \begin{macro}{\@ifstre@mopen}
% |\@ifstre@mopen|\marg{stream}\marg{TRUE code}\marg{FALSE code}
% checks if stream \meta{stream} is currently open.
%    \begin{macrocode}
\newcommand{\@ifstre@mopen}[3]{%
  \csname ifstre@m#1open\endcsname#2\else#3\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\instre@mandopen}
% |\instre@mandopen|\marg{stream}\marg{TRUE code} checks if \meta{stream}
% is an input stream and is open. If so, it executes \meta{TRUE code}.
%    \begin{macrocode}
\newcommand{\instre@mandopen}[2]{%
  \@ifundefined{#1instre@m}{%
    \PackageError{newfile}{#1\space is not an input stream}{\@ehc}}%
  {\@ifstre@mopen{#1}{#2}{%
    \PackageError{newfile}{Input stream #1\space is not open}{\@ehc}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\instre@mandclosed}
% |\instre@mandclosed|\marg{stream}\marg{TRUE code} checks if \meta{stream}
% is an input stream and is closed (not open). 
% If so, it executes \meta{TRUE code}.
%    \begin{macrocode}
\newcommand{\instre@mandclosed}[2]{%
  \@ifundefined{#1instre@m}{%
    \PackageError{newfile}{#1\space is not an input stream}{\@ehc}}%
  {\@ifstre@mopen{#1}{%
    \PackageError{newfile}{Input stream #1\space is open}{\@ehc}}{#2}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\outstre@mandopen}
% |\outstre@mandopen|\marg{stream}\marg{TRUE code} checks if \meta{stream}
% is an output stream and is open. If so, it executes \meta{TRUE code}.
%    \begin{macrocode}
\newcommand{\outstre@mandopen}[2]{%
  \@ifundefined{#1outstre@m}{%
    \PackageError{newfile}{#1\space is not an output stream}{\@ehc}}%
  {\@ifstre@mopen{#1}{#2}{%
    \PackageError{newfile}{Output stream #1\space is not open}{\@ehc}}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\outstre@mandclosed}
% |\outtre@mandclosed|\marg{stream}\marg{TRUE code} checks if \meta{stream}
% is an output stream and is closed (not open). 
% If so, it executes \meta{TRUE code}.
%    \begin{macrocode}
\newcommand{\outstre@mandclosed}[2]{%
  \@ifundefined{#1outstre@m}{%
    \PackageError{newfile}{#1\space is not an output stream}{\@ehc}}%
  {\@ifstre@mopen{#1}{%
    \PackageError{newfile}{Output stream #1\space is open}{\@ehc}}{#2}}}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\openoutputfile}
% |\openoutputfile|\marg{filename}\marg{stream} opens the file called 
% \meta{filename} and attaches it to the stream \meta{stream} for
% writing. However, if the |\nofiles| command has been given the
% file is \emph{not} attached to the stream. No more than one file can be 
% attached to a stream at any given time.
%    \begin{macrocode}
\newcommand{\openoutputfile}[2]{%
  \outstre@mandclosed{#2}{%
    \global\@namedef{#1@filename}{#1}%
    \if@filesw 
      \immediate\openout\@nameuse{#2outstre@m}=\@nameuse{#1@filename}%
    \fi
    \global\csname stre@m#2opentrue\endcsname%
    \@nameuse{atstreamopen#2}%
  }%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\closeoutputstream}
% |\closeoutputstream|\marg{stream} closes the stream \meta{stream}.
%    \begin{macrocode}
\newcommand{\closeoutputstream}[1]{%
  \outstre@mandopen{#1}{%
    \@nameuse{atstreamclose#1}%
    \immediate\closeout\@nameuse{#1outstre@m}%
    \global\csname stre@m#1openfalse\endcsname}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\openinputfile}
% |\openinputfile|\marg{filename}\marg{stream} opens the file called 
% \meta{filename} and attaches it to the stream \meta{stream} for
% reading. The file is added to the list of files. 
% No more than one file can be attached to a stream at
% any given time.
%    \begin{macrocode}
\newcommand{\openinputfile}[2]{%
  \IfFileExists{#1}{%                   file exists
    \instre@mandclosed{#2}{% 
      \@addtofilelist{#1}%
      \global\@namedef{#1@filename}{#1}%
      \immediate\openin\@nameuse{#2instre@m}=\@nameuse{#1@filename}%
      \global\csname stre@m#2opentrue\endcsname%
      \@nameuse{atstreamopen#2}}}%
  {%                                    file not found
    \PackageError{newfile}{Can't find file #1}{\@ehc}%
  }%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\closeinputstream}
% |\closeinputstream|\marg{stream} closes the stream \meta{stream}.
%    \begin{macrocode}
\newcommand{\closeinputstream}[1]{%
  \instre@mandopen{#1}{%
     \@nameuse{atstreamclose#1}%
     \immediate\closein\@nameuse{#1instre@m}%
     \global\csname stre@m#1openfalse\endcsname}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{writeverbatim}
% |\begin{writeverbatim}|\marg{stream} writes the contents of
% the environment as verbatim text to the given \meta{stream}.
%    \begin{macrocode}
\def\writeverbatim#1{%
  \@bsphack
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \verbatim@startline
  \verbatim@addtoline
  \verbatim@finish
  \def\verbatim@processline{%
    \immediate\write\@nameuse{#1outstre@m}{\the\verbatim@line}}%
  \verbatim@start}
\def\endwriteverbatim{\@esphack}

%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\addtostream}
% |\addtostream|\marg{stream}\marg{text} writes \meta{text} to the
% given \meta{stream}.
%    \begin{macrocode}
\newcommand{\addtostream}[2]{%
  \@bsphack
  \outstre@mandopen{#1}{%
    {\let\protect\string
     \immediate\write\@nameuse{#1outstre@m}{#2}%
    }}%
  \@esphack
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifstre@mnoteof}
% \begin{macro}{\checkstre@mnoteof}
% |\checkstre@mnoteof|\marg{stream} sets |\ifstre@mnoteof| to TRUE if
% \meta{stream} is not at the end of the file (i.e., it is the opposite
% of |\ifeof|).
%    \begin{macrocode}
\newif\ifstre@mnoteof
\newcommand{\checkstre@meof}[1]{%
  \stre@mnoteoftrue\ifeof\@nameuse{#1instre@m}\stre@mnoteoffalse\fi}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\readstream}
% |\readstream|\marg{stream} reads the contents of
% the given \meta{stream} as |\input| text.
%    \begin{macrocode}
\def\readstream#1{
  \instre@mandopen{#1}{%
    \loop \checkstre@meof{#1} \ifstre@mnoteof
      \read\@nameuse{#1instre@m} to\temptokstre@m
     \temptokstre@m
    \repeat
    }%
}
  
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\readaline}
% |\readaline|\marg{stream} reads what TeX considers to be one line
% from the given \meta{stream} as |\input| text.
%    \begin{macrocode}
\def\readaline#1{
  \instre@mandopen{#1}{%
    \ifeof\@nameuse{#1instre@m}
      \PackageWarning{newfile}{No more to read from stream #1}
    \else
      \read\@nameuse{#1instre@m} to\temptokstre@m
      \temptokstre@m
    \fi
    }%
}
  
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\readverbatim}
% \begin{macro}{\stre@mverbatim@input}
% \begin{macro}{\verbatim@readstre@m}
% |\readverbatim|\marg{stream} reads the contents of
% the given \meta{stream} as verbatim text.
%
% The read verbatim code is a slight variation on code from the
% \Lpack{verbatim} package. Most of the setup is done by the macros
% |\stre@mverbatim@input|\marg{setup}\marg{stream} and
% |\verbatim@readstre@m|\marg{stream}. Finally, |\verbatim@read@file|
% is a \Lpack{verbatim} package macro.
%    \begin{macrocode}
\def\readverbatim{\begingroup
  \@ifstar{\stre@mverbatim@input\relax}%
          {\stre@mverbatim@input{\frenchspacing\@vobeyspaces}}}

\def\stre@mverbatim@input#1#2{%
  \@ifstre@mopen{#2}%
    {\@verbatim #1\relax
     \def\verbatim@in@stream{\@nameuse{#2instre@m}}
     \verbatim@readstre@m{#2}\endtrivlist\endgroup\@doendpe}%
    {\PackageError{newfile}{Stream #2 is not open}{\@ehc}\endgroup}%
}

\def\verbatim@readstre@m#1{%
  \verbatim@startline
  \expandafter\endlinechar\expandafter\m@ne
  \expandafter\verbatim@read@file
  \expandafter\endlinechar\the\endlinechar\relax
  \verbatim@finish
}

%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plainvstream}
% A macro that sets |\verbatim@processline| to its default definition.
%    \begin{macrocode}
\newcommand{\plainvstream}{%
  \def\verbatim@processline{\the\verbatim@line\par}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\streamvnumfont}
% \begin{macro}{\stre@mvnfont}
% We need a counter for numbering lines read in verbatim.
%
% |\streamvnumfont|\marg{font} defines |\stre@mvnfont| to be \meta{font}.
%    \begin{macrocode}
\newcounter{streamvline}
\newcommand{\streamvnumfont}[1]{\def\stre@mvnfont{#1}}
\streamvnumfont{\footnotesize}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\streamvfont}
% \begin{macro}{\verbatim@font}
% |\streamvfont|\marg{font} defines |\verbatim@font| to use \meta{font}.
%    \begin{macrocode}
\newcommand{\verbatimfont}[1]{%
  \def\verbatim@font{#1%
    \hyphenchar\font\m@ne
    \let\do\do@noligs
    \verbatim@nolig@list}}
\verbatimfont{\normalfont\ttfamily}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\numbervstream}
% |\numbervstream| puts numbers at the start of each line read verbatim.
%    \begin{macrocode}
\newcommand{\numbervstream}{%
  \setcounter{streamvline}{0}%
  \def\verbatim@processline{%
    \addtocounter{streamvline}{1}%
    \leavevmode
    {\stre@mvnfont \thestreamvline}\space
    \the\verbatim@line\par}%
}

%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\marginnumbervstream}
% |\marginnumbervstream| puts numbers in the margin at the start of 
% each line read verbatim.
%    \begin{macrocode}
\newcommand{\marginnumbervstream}{%
  \setcounter{streamvline}{0}%
  \def\verbatim@processline{%
    \addtocounter{streamvline}{1}%
    \leavevmode
    \llap{{\stre@mvnfont \thestreamvline} \hskip\@totalleftmargin}
    \the\verbatim@line\par}%
}

%    \end{macrocode}
% \end{macro}
%
%    The end of the \Lpack{newfile} package.
%    \begin{macrocode}
%</outin>
%    \end{macrocode}
%
%
%
% \bibliographystyle{alpha}
%
% \begin{thebibliography}{GMS94}
%
% \bibitem[GMS94]{GOOSSENS94}
% Michel Goossens, Frank Mittelbach, and Alexander Samarin.
% \newblock \emph{The LaTeX Companion}.
% \newblock Addison-Wesley Publishing Company, 1994.
%
% \bibitem[MG95]{ENDFLOAT}
% James Darrell McCauley and Jeff Goldberg.
% \newblock `The \texttt{endfloat} package'.
% \newblock October, 1995.
%
% \bibitem[Pif96]{ANSWERS}
% Mike Piff.
% \newblock `Production of solution sheets in LaTeX2e'.
% \newblock July, 1996.
%
% \bibitem[SRR99]{VERBATIM}
% Rainer Sch\"{o}pf, Bernd Raichle, and Chris Rowley.
% \newblock `A New Implementation of LaTeX's \texttt{verbatim} and 
%            \texttt{verbatim*} Environments'.
% \newblock December, 1999.
%
% \end{thebibliography}
%
%
% \Finale
% \PrintIndex
%
\endinput

%% \CharacterTable
%%  {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
%%   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
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}