Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/ml-burg/doc/code.sty
ViewVC logotype

Annotation of /sml/trunk/src/ml-burg/doc/code.sty

Parent Directory Parent Directory | Revision Log Revision Log


Revision 651 - (view) (download) (as text)

1 : monnier 249 % code.sty: -*- latex -*-
2 :     % Latex macros for a "weak" verbatim mode.
3 :     % -- like verbatim, except \, {, and } have their usual meanings.
4 :    
5 :     % Environments: code, tightcode, codeaux, codebox, centercode
6 :     % Commands: \dcd, \cddollar, \cdmath, \cd, \codeallowbreaks, \codeskip, \^
7 :     % Already defined in LaTeX, but of some relevance: \#, \$, \%, \&, \_, \{, \}
8 :    
9 :     % Changelog at the end of the file.
10 :    
11 :     % These commands give you an environment, code, that is like verbatim
12 :     % except that you can still insert commands in the middle of the environment:
13 :     % \begin{code}
14 :     % for(x=1; x<loop_bound; x++)
15 :     % y += x^3; /* {\em Add in {\tt x} cubed} */
16 :     % \end{code}
17 :     %
18 :     % All characters are ordinary except \{}. To get \{} in your text,
19 :     % you use the commands \\, \{, and \}.
20 :    
21 :     % These macros mess with the definition of the special chars (e.g., ^_~%).
22 :     % The characters \{} are left alone, so you can still have embedded commands:
23 :     % \begin{code} f(a,b,\ldots,y,z) \end{code}
24 :     % However, if your embedded commands use the formerly-special chars, as in
25 :     % \begin{code} x := x+1 /* \mbox{\em This is $y^3$} */ \end{code}
26 :     % then you lose. The $ and ^ chars are scanned in as non-specials,
27 :     % so they don't work. If the chars are scanned *outside* the code env,
28 :     % then you have no problem:
29 :     % \def\ycube{$y^3$}
30 :     % \begin{code} x := x+1 /* {\em This is \ycube} */ \end{code}
31 :     % If you must put special chars inside the code env, you do it by
32 :     % prefixing them with the special \dcd ("decode") command, that
33 :     % reverts the chars to back to special status:
34 :     % \begin{code} x := x+1 /* {\dcd\em This is $y^3$} */ \end{code}
35 :     % \dcd's scope is bounded by its enclosing braces. It is only defined within
36 :     % the code env. You can also turn on just $ with the \cddollar command;
37 :     % you can turn on just $^_ with the \cdmath command. See below.
38 :     %
39 :     % Alternatively, just use \(...\) for $...$, \sp for ^, and \sb for _.
40 :    
41 :     % WARNING:
42 :     % Like \verb, you cannot put a \cd{...} inside an argument to a macro
43 :     % or a command. If you try, for example,
44 :     % \mbox{\cd{$x^y$}}
45 :     % you will lose. That is because the text "\cd{$x^y$}" gets read in
46 :     % as \mbox's argument before the \cd executes. But the \cd has to
47 :     % have a chance to run before LaTeX ever reads the $x^y$ so it can
48 :     % turn off the specialness of $ and ^. So, \cd has to appear at
49 :     % top level, not inside an argument. Similarly, you can't have
50 :     % a \cd or a \code inside a macro (Although you could use \gdef to
51 :     % define a macro *inside* a \cd, which you could then use outside.
52 :     % Don't worry about this if you don't understand it.)
53 :    
54 :     % BUG: In the codebox env, the effect of a \dcd, \cddollar, or \cdmath
55 :     % command is reset at the end of each line. This can be hacked by
56 :     % messing with the \halign's preamble, if you feel up to it.
57 :    
58 :     % Useage note: the initial newline after the \begin{code} or
59 :     % \begin{codebox} is eaten, but the last newline is not.
60 :     % So,
61 :     % \begin{code}
62 :     % foo
63 :     % bar
64 :     % \end{code}
65 :     % leaves one more blank line after bar than does
66 :     % \begin{code}
67 :     % foo
68 :     % bar\end{code}
69 :     % Moral: get in the habit of terminating code envs without a newline
70 :     % (as in the second example).
71 :     %
72 :    
73 :     %
74 :     % get the paragraph indentation (use \edef and \the to force evaluation)
75 :     %
76 :     %\edef\codeindent{\the\parindent}
77 :     \edef\codeindent{0pt}
78 :    
79 :    
80 :     % All this stuff tweaks the meaning of space, tab, and newline.
81 :     %===============================================================================
82 :     % \cd@obeyspaces
83 :     % Turns all spaces into non-breakable spaces.
84 :     % Note: this is like \@vobeyspaces except without spurious space in defn.
85 :     % @xobeysp is basically a space; it's defined in latex.tex.
86 :     %
87 :     {\catcode`\ =\active\gdef\cd@obeyspaces{\catcode`\ =\active\let =\@xobeysp}}
88 :    
89 :    
90 :    
91 :     % \cd@obeytabs
92 :     % Turns all tabs into 8 non-breakable spaces (which is bogus).
93 :     %
94 :     {\catcode`\^^I=\active %
95 :     \gdef\cd@obeytabs{\catcode`\^^I=\active\let^^I=\cd@tab}}
96 :    
97 :     \def\cd@tab{\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp}
98 :    
99 :    
100 :    
101 :     % \cd@obeylines
102 :     % Turns all cr's into linebreaks. Pagebreaks are not permitted between lines.
103 :     % This is copied from lplain.tex's \obeylines, with the cr def'n changed.
104 :     %
105 :     {\catcode`\^^M=\active % these lines must end with %
106 :     \gdef\cd@obeylines{\catcode`\^^M=\active\let^^M=\cd@cr}}
107 :    
108 :     % What ^M turns into.
109 :     \def\cd@cr{\par\penalty10000 } % TeX magicness
110 :     %
111 :     % If the "\leavevmode" is included, the blank lines are not compressed out
112 :     % but you will end up with extra space at the bottom of your code if you
113 :     % put the "\end{code}" on a new line.
114 :     %\def\cd@cr{\par\penalty10000\leavevmode} % TeX magicness
115 :     %\def\cd@cr{\par\penalty10000\mbox{}} % LaTeX
116 :    
117 :    
118 :     % \codeallowbreaks
119 :     % Same as \cd@obeylines, except pagebreaks are allowed.
120 :     % Put this command inside a code env to allow pagebreaks.
121 :    
122 :     {\catcode`\^^M=\active % these lines must end with %
123 :     \gdef\codeallowbreaks{\catcode`\^^M\active\let^^M\cd@crbr}}
124 :    
125 :     \def\cd@crbr{\leavevmode\endgraf} % What ^M turns into.
126 :    
127 :    
128 :     % \cd@obeycrsp
129 :     % Turns cr's into non-breakable spaces. Used by \cd.
130 :    
131 :     {\catcode`\^^M=\active % these lines must end with %
132 :     \gdef\cd@obeycrsp{\catcode`\^^M=\active\let^^M=\@xobeysp}}
133 :    
134 :     % =============================================================================
135 :    
136 :     % Set up code environment, in which most of the common special characters
137 :     % appearing in code are treated verbatim, namely: $&#^_~%
138 :     % \ { } are still enabled so that macros can be called in this
139 :     % environment. Use \\, \{, and \} to use these characters verbatim
140 :     % in this environment.
141 :     %
142 :     % Inside a group, you can make
143 :     % all the hacked chars special with the \dcd command
144 :     % $ special with the \cddollar command
145 :     % $^_ special with the \cdmath command.
146 :     % If you have a bunch of math $..$'s in your code env, then a global \cddollar
147 :     % or \cdmath at the beginning of the env can save a lot of trouble.
148 :     % When chars are special (e.g., after a \dcd), you can still get #$%&_{} with
149 :     % \#, \$, \%, \&, \_, \{, and \} -- this is standard LaTeX.
150 :     % Additionally, \\ gives \ inside the code env, and when \cdmath
151 :     % makes ^ special, it also defines \^ to give ^.
152 :    
153 :     %The hacked characters can be made special again
154 :     % within a group by using the \dcd command.
155 :    
156 :     % Note: this environment allows no breaking of lines whatsoever; not
157 :     % at spaces or hypens. To arrange for a break use the standard \- command,
158 :     % or a \discretionary{}{}{} which breaks, but inserts nothing. This is useful,
159 :     % for example for allowing hypenated identifiers to be broken, e.g.
160 :     % \def\={\discretionary{}{}{}} %optional break
161 :     % FOO-\=BAR.
162 :    
163 :     % setupsmallcode added by JHR
164 :     %
165 :     \def\setupsmallcode{\parsep=0pt\parindent=0pt%
166 :     \renewcommand{\baselinestretch}{1.0}%
167 :     \small\tt\frenchspacing\catcode``=13\@noligs%
168 :     \def\\{\char`\\}\def\_{\char`\_}%
169 :     \def\{{\char`\{}\def\}{\char`\}}%
170 :     \let\dcd=\cd@dcd\let\cddollar=\cd@dollarspecial\let\cdmath=\cd@mathspecial%
171 :     \@makeother\$\@makeother\&\@makeother\#%
172 :     \@makeother\^\@makeother\_\@makeother\~%
173 :     \@makeother\%\cd@obeytabs\cd@obeyspaces}
174 :    
175 :     \def\setupcode{\parsep=0pt\parindent=0pt%
176 :     \tt\frenchspacing\catcode``=13\@noligs%
177 :     \def\\{\char`\\}\def\_{\char`\_}%
178 :     \def\{{\char`\{}\def\}{\char`\}}%
179 :     \let\dcd=\cd@dcd\let\cddollar=\cd@dollarspecial\let\cdmath=\cd@mathspecial%
180 :     \@makeother\$\@makeother\&\@makeother\#%
181 :     \@makeother\^\@makeother\_\@makeother\~%
182 :     \@makeother\%\cd@obeytabs\cd@obeyspaces}
183 :     % other: $&#^_~%
184 :     % left special: \{}
185 :     % unnecessary: @`'"
186 :    
187 :    
188 :     %% codebox, centerbox
189 :     %%=============================================================================
190 :     %% The codebox env makes a box exactly as wide as it needs to be
191 :     %% (i.e., as wide as the longest line of code is). This is useful
192 :     %% if you want to center a chunk of code, or flush it right, or
193 :     %% something like that. The optional argument to the environment,
194 :     %% [t], [c], or [b], specifies how to vertically align the codebox,
195 :     %% just as with arrays or other boxes. Default is [c].
196 :    
197 :     %% Must be a newline immediately after "\begin{codebox}[t]"!
198 :    
199 :     {\catcode`\^^M=\active % these lines must end with %
200 :     \gdef\cd@obeycr{\catcode`\^^M=\active\let^^M=\cr}}
201 :    
202 :     % If there is a [<letter>] option, then the following newline will
203 :     % be read *after* ^M is bound to \cr, so we're cool. If there isn't
204 :     % an option given (i.e., default to [c]), then the @\ifnextchar will
205 :     % gobble up the newline as it gobbles whitespace. So we insert the
206 :     % \cr explicitly. Isn't TeX fun?
207 :     \def\codebox{\leavevmode\@ifnextchar[{\@codebox}{\@codebox[c]\cr}} %]
208 :    
209 :     \def\@codebox[#1]%
210 :     {\hbox\bgroup$\if #1t\vtop \else \if#1b\vbox \else \vcenter \fi\fi\bgroup%
211 :     \tabskip\z@\setupsmallcode\cd@obeycr% just before cd@obey
212 :     \halign\bgroup##\hfil\span}
213 :    
214 :     \def\endcodebox{\crcr\egroup\egroup\m@th$\egroup}
215 :    
216 :     % Center the box on the page:
217 :     \newenvironment{centercode}%
218 :     {\begin{center}\begin{codebox}[c]}%
219 :     {\end{codebox}\end{center}}
220 :    
221 :    
222 :     %% code, codeaux, tightcode
223 :     %%=============================================================================
224 :     %% Code environment as described above. Lines are kept on one page.
225 :     %% This actually works by setting a huge penalty for breaking
226 :     %% between lines of code. Code is indented same as other displayed paras.
227 :     %% Note: to increase left margin, use \begin{codeaux}{\leftmargin=1in}.
228 :    
229 :     % To allow pagebreaks, say \codeallowbreaks immediately inside the env.
230 :     % You can allow breaks at specific lines with a \pagebreak form.
231 :    
232 :     %% N.B.: The \global\@ignoretrue command must be performed just inside
233 :     %% the *last* \end{...} before the following text. If not, you will
234 :     %% get an extra space on the following line. Blech.
235 :    
236 :     %% This environment takes two arguments.
237 :     %% The second, required argument is the \list parameters to override the
238 :     %% \@listi... defaults.
239 :     %% - Usefully set by clients: \topsep \leftmargin
240 :     %% - Possible, but less useful: \partopsep
241 :     %% The first, optional argument is the extra \parskip glue that you get around
242 :     %% \list environments. It defaults to the value of \parskip.
243 :     \def\codeaux{\@ifnextchar[{\@codeaux}{\@codeaux[\parskip]}} %]
244 :     \def\@codeaux[#1]#2{%
245 :     \bgroup\parskip#1%
246 :     \begin{list}{}%
247 :     {\parsep\z@\leftmargin=\codeindent\rightskip\z@\listparindent\z@\itemindent\z@#2}%
248 :     \item[]\setupsmallcode\cd@obeylines}%
249 :     \def\endcodeaux{\end{list}\leavevmode\egroup\ignorespaces\global\@ignoretrue}
250 :    
251 :     %% Code env is codeaux with the default margin and spacing \list params:
252 :     \def\code{\codeaux{}} \let\endcode=\endcodeaux
253 :    
254 :     %% Like code, but with no extra vertical space above and below.
255 :     \def\tightcode{\codeaux[=0pt]{\topsep\z@}}%
256 :     \let\endtightcode\endcodeaux
257 :     % {\vspace{-1\parskip}\begin{codeaux}{\partopsep\z@\topsep\z@}}%
258 :     % {\end{codeaux}\vspace{-1\parskip}}
259 :    
260 :    
261 :    
262 :     % Reasonable separation between lines of code
263 :     \newcommand{\codeskip}{\penalty0\vspace{2ex}}
264 :    
265 :    
266 :     % \cd is used to build a code environment in the middle of text.
267 :     % Note: only difference from display code is that cr's are taken
268 :     % as unbreakable spaces instead of linebreaks.
269 :    
270 :     \def\cd{\leavevmode\begingroup\ifmmode\let\startcode=\startmcode\else%
271 :     \let\startcode\starttcode\fi%
272 :     \setupcode\cd@obeycrsp\startcode}
273 :    
274 :     \def\cdm{\leavevmode\begingroup\ifmmode\let\startcode=\startmcode\else%
275 :     \let\startcode\starttcode\fi%
276 :     \setupcode\cd@obeycrsp\cd@mathspecial\startcode}
277 :    
278 :     \def\starttcode#1{#1\endgroup}
279 :     \def\startmcode#1{\hbox{#1}\endgroup}
280 :    
281 :    
282 :     % Restore $&#^_~% to their normal catcodes
283 :     % Define \^ to give the ^ char.
284 :     % \dcd points to this guy inside a code env.
285 :     \def\cd@dcd{\catcode`\$=3\catcode`\&=4\catcode`\#=6\catcode`\^=7%
286 :     \catcode`\_=8\catcode`\~=13\catcode`\%=14\def\^{\char`\^}}
287 :    
288 :     % Selectively enable $, and $^_ as special.
289 :     % \cd@mathspecial also defines \^ give the ^ char.
290 :     % \cddollar and \cdmath point to these guys inside a code env.
291 :     \def\cd@dollarspecial{\catcode`\$=3}
292 :     \def\cd@mathspecial{\catcode`\$=3\catcode`\^=7\catcode`\_=8%
293 :     \def\^{\char`\^}}
294 :    
295 :    
296 :     % Change log:
297 :     % Started off as some macros found in C. Rich's library.
298 :     % Olin 1/90:
299 :     % Removed \makeatletter, \makeatother's -- they shouldn't be there,
300 :     % because style option files are read with makeatletter. The terminal
301 :     % makeatother screwed things up for the following style options.
302 :     % Olin 3/91:
303 :     % Rewritten.
304 :     % - Changed things so blank lines don't get compressed out (the \leavevmove
305 :     % in \cd@cr and \cd@crwb).
306 :     % - Changed names to somewhat less horrible choices.
307 :     % - Added lots of doc, so casual hackers can more easily mess with all this.
308 :     % - Removed `'"@ from the set of hacked chars, since they are already
309 :     % non-special.
310 :     % - Removed the bigcode env, which effect can be had with the \codeallowbreaks
311 :     % command.
312 :     % - Removed the \@noligs command, since it's already defined in latex.tex.
313 :     % - Win big with the new \dcd, \cddollar, and \cdmath commands.
314 :     % - Now, *only* the chars \{} are special inside the code env. If you need
315 :     % more, use the \dcd command inside a group.
316 :     % - \cd now works inside math mode. (But if you use it in a superscript,
317 :     % it still comes out full size. You must explicitly put a \scriptsize\tt
318 :     % inside the \cd: $x^{\cd{\scriptsize\tt...}}$. A \leavevmode was added
319 :     % so that if you begin a paragraph with a \cd{...}, TeX realises you
320 :     % are starting a paragraph.
321 :     % - Added the codebox env. Tricky bit involving the first line hacked
322 :     % with help from David Long.
323 :     %
324 :     % JHR 8/19/91:
325 :     % - Added \setupsmallcode to use in multi-line code displays (code, codeaux and
326 :     % codebox environments).
327 :     %
328 :     % JHR 8/31/91:
329 :     % - changed size of small code to \small (from \footnotesize). Also added
330 :     % code to set the baselinestretch to 1 in smallcode.
331 :     %
332 :     % JHR 9/12/91:
333 :     % - added \codeindent (set to \parindent)
334 :     %
335 :     % JHR 11/19/91
336 :     % - added \cdm{} command for supporting math mode in \cd{}

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0