|
|
1.1 root 1: % LaTeX tgrind environment
2:
3: % Based on Van Jacobson's ``tgrindmac'', a macro package for TeX grinding
4: % Our job here is to simplify it quite a bit and make it conform to LaTeX
5:
6: \newif\ifcomment\newif\ifstring
7: \newcount\linecount\newcount\linenext
8: \newbox\linesofar
9: \newdimen\TBwid
10: \newdimen\ts
11: \newbox\tbox
12:
13: \def\tgrind{%
14: % Stuff we ignore for right now.
15: \let\Head=\@gobble
16: \def\File##1,##2,##3{}
17: \let\Proc=\@gobble
18: \let\ProcCont=\@gobble
19: %
20: % Each formfeed in the input is replaced by a "\NewPage" macro. If
21: % you really want a page break here, define this as "\vfill\eject".
22: \def\NewPage{\filbreak\bigskip}
23: %
24: % Each line of the program text is enclosed by a "\L{...}". We turn
25: % each line into an hbox of size hsize. If we saw a procedure name somewhere
26: % in the line (i.e., "procbox" is not null), we right justify "procbox"
27: % on the line. Every 10 lines we output a small, right justified line number.
28: \def\L##1{\par\hbox to\hsize{\CF\strut\global\advance\linecount by1
29: ##1\hss\linebox}}
30: %
31: \linecount=0
32: \linenext=9
33: \def\linebox{\ifnum\linecount>\linenext\global\advance\linenext by10
34: \hbox{\sevrm\the\linecount}\fi}
35: %
36: % The following weirdness is to deal with tabs. "Pieces" of a line
37: % between tabs are output as "\LB{...}". E.g., a line with a tab at
38: % column 16 would be output as "\LB{xxx}\Tab{16}\LB{yyy}". (Actually, to
39: % reduce the number of characters in the .tex file the \Tab macro
40: % supplies the 2nd & subsequent \LB's.) We accumulate the LB stuff in an
41: % hbox. When we see a Tab, we grab this hbox (using "\lastbox") and turn
42: % it into a box that extends to the tab position. We stash this box in
43: % "\linesofar" & use "\everyhbox" to get \linesofar concatenated onto the
44: % front of the next piece of the line. (There must be a better way of
45: % doing tabs [cf., the Plain.tex tab macros] but I'm not not enough of a
46: % TeX wizard to come up with it. Suggestions would be appreciated.)
47: \def\LB{\CF\hbox}
48: \setbox\linesofar=\null
49: \everyhbox={\box\linesofar}
50: \def\Tab##1{\setbox\tbox=\lastbox\TBwid=1\wd\tbox\advance\TBwid by 1\ts
51: \ifdim\TBwid>##1\ts
52: \setbox\linesofar=\hbox{\box\tbox\space}\else
53: \setbox\linesofar=\hbox to ##1\ts{\box\tbox\hfil}\fi\LB}
54: %
55: % A normal space is too thin for code listings. We make spaces & tabs
56: % be in "\ts" units (which are the width of a "0" in the current font).
57: \setbox\tbox=\hbox{0} \ts=1\wd\tbox \setbox\tbox=\hbox{\hskip 1\ts}
58: \def\space{\hskip 1\ts\relax}
59: %
60: % Font changing stuff for keywords, comments & strings. We put keywords
61: % in boldface, comments in text-italic & strings in typewriter. Since
62: % we're usually changing the font inside of a \LB macro, we remember the
63: % current font in \CF & stick a \CF at the start of each new box.
64: % Also, the characters " and ' behave differently in comments than in
65: % code, and others behave differently in strings than in code.
66: \let\CF=\rm
67: \def\K##1{{\bf ##1}} % Keyword
68: \def\C{\it\global\let\CF=\it\global\commenttrue\relax} % Comment Start
69: \def\CE{\rm\global\let\CF=\rm\global\commentfalse\relax}% Comment End
70: \def\S{\tt\global\let\CF=\tt\global\stringtrue\relax} % String Start
71: \def\SE{\rm\global\let\CF=\rm\global\stringfalse\relax} % String End
72: %
73: % Special characters.
74: \def\{{\ifmmode\lbrace\else\ifstring{\char'173}\else$\lbrace$\fi\fi}
75: \def\}{\ifmmode\rbrace\else\ifstring{\char'175}\else$\rbrace$\fi\fi}
76: \def\!{\ifmmode\backslash\else\ifstring{\char'134}\else$\backslash$\fi\fi}
77: \def\|{\ifmmode|\else\ifstring{\char'174}\else$|$\fi\fi}
78: \def\<{\ifmmode<\else\ifstring<\else$<$\fi\fi}
79: \def\>{\ifmmode>\else\ifstring>\else$>$\fi\fi}
80: \def\/{\ifmmode/\else\ifstring/\else$/$\fi\fi}
81: \def\-{\ifmmode-\else\ifstring-\else$-$\fi\fi}
82: \def\_{\ifstring{\char'137}\else\underbar{\ }\fi}
83: \def\&{{\char'046}}
84: \def\#{{\char'043}}
85: \def\%{{\char'045}}
86: \def\~{{\char'176}}
87: \def\"{\ifcomment''\else{\tt\char'042}\fi}
88: \def\'{\ifcomment'\else{\tt\char'047}\fi}
89: \def\^{{\char'136}}
90: \def\${{\rm\char'044}}
91: %
92: \raggedright\obeyspaces%\let =\space%
93: }
94:
95: % \tagrind*[FLOAT]{FILE}{CAPTION}{LABEL}
96: % * optional
97: % FLOAT float options
98: % FILE reads LaTeXgrind input in file FILE
99: % CAPTION for list of figures
100: % LABEL for \ref and \pageref
101: \def\tagrind{\@ifstar{\@stagrind}{\@tagrind}}
102:
103: \def\@tagrind{\@ifnextchar[{\@@tagrind}{\@@tagrind[t]}}
104: \def\@stagrind{\@ifnextchar[{\@@stagrind}{\@@stagrind[t]}}
105:
106: \def\@@tagrind[#1]#2#3#4{%
107: \begin{figure}[#1]
108: \hrule
109: \vskip .5\baselineskip
110: \begin{minipage}\columnwidth\floatsize
111: \begin{tgrind}
112: \input #2\relax
113: \end{tgrind}
114: \end{minipage}
115: \vskip .5\baselineskip plus .5\baselineskip
116: \begingroup
117: \setbox\z@=\hbox{#4}%
118: \ifdim\wd\z@>\z@
119: \caption{#3}%
120: \label{#4}%
121: \else
122: \captcont{#3}%
123: \fi
124: \endgroup
125: \vskip 2pt
126: \hrule
127: \end{figure}
128: }
129:
130: \def\@@stagrind[#1]#2#3#4{%
131: \begin{figure*}[#1]
132: \hrule
133: \vskip .5\baselineskip
134: \begin{minipage}\textwidth\floatsize
135: \begin{tgrind}
136: \input #2\relax
137: \end{tgrind}
138: \end{minipage}
139: \vskip .5\baselineskip plus .5\baselineskip
140: \begingroup
141: \setbox\z@=\hbox{#4}%
142: \ifdim\wd\z@>\z@
143: \caption{#3}%
144: \label{#4}%
145: \else
146: \captcont{#3}%
147: \fi
148: \endgroup
149: \vskip 2pt
150: \hrule
151: \end{figure*}
152: }
153:
154:
155: \def\tgrindfile#1{%
156: \par\addvspace{0.1in}
157: \hrule
158: \vskip .5\baselineskip
159: \begin{floatsize}
160: \begin{tgrind}
161: \input #1\relax
162: \end{tgrind}
163: \end{floatsize}
164: \vskip .5\baselineskip
165: \hrule
166: \addvspace{0.1in}
167: }
168:
169:
170: % \tagrindfile{FILE}{CAPTION}{LABEL}
171:
172: \def\tagrindfile#1#2#3{%
173: \par\addvspace{0.1in}
174: \hrule
175: \vskip .5\baselineskip
176: \begin{floatsize}
177: \begin{tgrind}
178: \input #1\relax
179: \end{tgrind}
180: \end{floatsize}
181: \vskip .5\baselineskip plus .5\baselineskip
182: \begingroup
183: \def\@captype{figure}
184: \setbox\z@=\hbox{#3}%
185: \ifdim\wd\z@>\z@
186: \caption{#2}%
187: \label{#3}%
188: \else
189: \captcont{#2}%
190: \fi
191: \endgroup
192: \vskip 2pt
193: \hrule
194: \addvspace{0.1in}
195: }
196:
197: \expandafter\ifx\csname floatsize\endcsname\relax \def\floatsize{\small}\fi
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.