summaryrefslogtreecommitdiff
path: root/info/digests/ttn/ttn2n3-gather.sty
blob: 73e55b9b60be50fc240606ab088bba15bd0ecb9a (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
%%  8 JUN 93: this file needed to run Johannes Braams' article in TTN
%%            2,3 (Ch.)


%%  7 JUL 93: This file renamed  ttn2n3-gather.sty  to show its
%%            relationship with TTN 2,3 files (Ch.)

% gather.sty

%
% The following macro hackery I use to extract the macrocode
% from the source of the article in order to use it later on.
%
\newwrite\codefile     % To gather all the code together
\newwrite\tmpfile      % Temporary file for typesetting the code
%
{\catcode`\%=12 \gdef\comment{%% }}
\chardef\other=12
\def\myverbatim{\begingroup
    \catcode`\\=\other\catcode`\&=\other\catcode`\$=\other
    \catcode`\{=\other\catcode`\}=\other\catcode`\#=\other
    \catcode`\^=\other\catcode`\~=\other\catcode`\%=\other
    \catcode`\"=\other\catcode`\<=\other\catcode`\>=\other
    \catcode`\_=\other\catcode`\|=\other \obeyspaces}
% Macro to write code to the codefile *and* to the temporary file
\def\spaces{\space\space\space\space}
%
% DOUBLE DANGEROUS BEND::
% I make the ^^M an active character
% I use it to write macrocode line by line to the files.
\begingroup
  \catcode`\^^M\active% Hide all ^^M's from TeX...
%
% I also have to temporarily change some other category codes,
% in order to be able to scan for the line containing 
% the string "\end{code}"
%
  \begingroup%
    \catcode`\[=1  \catcode`\]=2  \catcode`\|=0%
    \catcode`\{=12 \catcode`\}=12 \catcode`\\=12%
    |gdef|writeit#1^^M[|endgroup|def|tempa[#1]|def|tempb[\end{code}]%
                |ifx|tempa|tempb%
                    |def|next[|end[code]]%
                |else%
                  |def|next[|immediate|write|codefile[#1]%
                            |immediate|write|tmpfile[|spaces#1]^^M]%
                |fi|next]%
  |endgroup%
% Here is a new LaTeX environment which can contain TeX macrocode
% The code will be written to two files, the file to gather all code
% in and the temporary fill that is \input at the end of the
% environment. In that file the code will be in a normal verbatim
% environment.
  \gdef\code{\bgroup\catcode`\^^M\active%
      \def^^M{\myverbatim\writeit}%
      \immediate\openout\tmpfile=tmp%
      \immediate\write\tmpfile{\string\begin{verbatim}}}%
% And an environment to typeset the examples
  \gdef\example{\bgroup\catcode`\^^M\active%
      \def^^M{\myverbatim\writeit}%
      \immediate\openout\tmpfile=tmp%
      \immediate\openout\codefile=tmpb%
      \immediate\write\tmpfile{\string\begin{verbatim}}}%
\endgroup%         Now the ^^M is no longer active
\def\endcode{\egroup
    \immediate\write\tmpfile{\string\end{verbatim}}
    \immediate\closeout\tmpfile
    \input{tmp}}
\def\endexample{\egroup
    \immediate\write\tmpfile{\string\end{verbatim}}
    \immediate\closeout\tmpfile
    \immediate\closeout\codefile
    \leavevmode
    \begin{minipage}[c]{.45\textwidth}\small\input{tmp}\end{minipage}%
    \hfil
    \begin{minipage}[c]{.45\textwidth}\small\input{tmpb}\end{minipage}}
\def\startgather#1{%
    \immediate\openout\codefile=#1
    \immediate\write\codefile{\comment File  : #1.tex}
    \immediate\write\codefile{\comment Author: J.L. Braams}
    {\let~\space
    \immediate\write\codefile{\comment Date  : \today}}
}
\def\endgather{%
    \immediate\write\codefile{\string\endinput}
    \immediate\write\codefile{\comment the rest of this file is ignored.}
    \immediate\closeout\codefile}
  \def\startexamples{\let\code\example\let\endcode\endexample
    \def\spaces{}}