Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/doc/report/code.sty
ViewVC logotype

Annotation of /branches/pure-cfg/doc/report/code.sty

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jhr 97 % 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 :     % indentation for code and codeaux environments
75 :     %
76 :     \newlength{\codeindent}
77 :     \setlength{\codeindent}{\parindent}
78 :     %
79 :     % The font family used for code
80 :     %
81 :     \newcommand{\codefontfamily}{\ttfamily}
82 :     %
83 :     % The font size used for code (\codesize) and code displays (\displaycodesize).
84 :     %
85 :     \newcommand{\codesize}{\normalsize}
86 :     \newcommand{\codedisplaysize}{\small}
87 :    
88 :     % All this stuff tweaks the meaning of space, tab, and newline.
89 :     %===============================================================================
90 :     % \cd@obeyspaces
91 :     % Turns all spaces into non-breakable spaces.
92 :     % Note: this is like \@vobeyspaces except without spurious space in defn.
93 :     % @xobeysp is basically a space; it's defined in latex.tex.
94 :     %
95 :     {\catcode`\ =\active\gdef\cd@obeyspaces{\catcode`\ =\active\let =\@xobeysp}}
96 :    
97 :    
98 :    
99 :     % \cd@obeytabs
100 :     % Turns all tabs into 8 non-breakable spaces (which is bogus).
101 :     %
102 :     {\catcode`\^^I=\active %
103 :     \gdef\cd@obeytabs{\catcode`\^^I=\active\let^^I=\cd@tab}}
104 :    
105 :     \def\cd@tab{\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp}
106 :    
107 :    
108 :    
109 :     % \cd@obeylines
110 :     % Turns all cr's into linebreaks. Pagebreaks are not permitted between lines.
111 :     % This is copied from lplain.tex's \obeylines, with the cr def'n changed.
112 :     %
113 :     {\catcode`\^^M=\active % these lines must end with %
114 :     \gdef\cd@obeylines{\catcode`\^^M=\active\let^^M=\cd@cr}}
115 :    
116 :     % What ^M turns into.
117 :     \def\cd@cr{\par\penalty10000} % TeX magicness
118 :     %
119 :     % If the "\leavevmode" is included, the blank lines are not compressed out
120 :     % but you will end up with extra space at the bottom of your code if you
121 :     % put the "\end{code}" on a new line.
122 :     %\def\cd@cr{\par\penalty10000\leavevmode} % TeX magicness
123 :     %\def\cd@cr{\par\penalty10000\mbox{}} % LaTeX
124 :     %\def\cd@cr{\par\penalty10000\mbox{}} % LaTeX
125 :    
126 :    
127 :     % \codeallowbreaks
128 :     % Same as \cd@obeylines, except pagebreaks are allowed.
129 :     % Put this command inside a code env to allow pagebreaks.
130 :    
131 :     {\catcode`\^^M=\active % these lines must end with %
132 :     \gdef\codeallowbreaks{\catcode`\^^M\active\let^^M\cd@crbr}}
133 :    
134 :     %\def\cd@crbr{\leavevmode\endgraf} % What ^M turns into.
135 :     \def\cd@crbr{\par\leavevmode} % What ^M turns into.
136 :    
137 :    
138 :     % \cd@obeycrsp
139 :     % Turns cr's into non-breakable spaces. Used by \cd.
140 :    
141 :     {\catcode`\^^M=\active % these lines must end with %
142 :     \gdef\cd@obeycrsp{\catcode`\^^M=\active\let^^M=\@xobeysp}}
143 :    
144 :     % =============================================================================
145 :    
146 :     % Set up code environment, in which most of the common special characters
147 :     % appearing in code are treated verbatim, namely: $&#^_~%
148 :     % \ { } are still enabled so that macros can be called in this
149 :     % environment. Use \\, \{, and \} to use these characters verbatim
150 :     % in this environment.
151 :     %
152 :     % Inside a group, you can make
153 :     % all the hacked chars special with the \dcd command
154 :     % $ special with the \cddollar command
155 :     % $^_ special with the \cdmath command.
156 :     % If you have a bunch of math $..$'s in your code env, then a global \cddollar
157 :     % or \cdmath at the beginning of the env can save a lot of trouble.
158 :     % When chars are special (e.g., after a \dcd), you can still get #$%&_{} with
159 :     % \#, \$, \%, \&, \_, \{, and \} -- this is standard LaTeX.
160 :     % Additionally, \\ gives \ inside the code env, and when \cdmath
161 :     % makes ^ special, it also defines \^ to give ^.
162 :    
163 :     %The hacked characters can be made special again
164 :     % within a group by using the \dcd command.
165 :    
166 :     % Note: this environment allows no breaking of lines whatsoever; not
167 :     % at spaces or hypens. To arrange for a break use the standard \- command,
168 :     % or a \discretionary{}{}{} which breaks, but inserts nothing. This is useful,
169 :     % for example for allowing hypenated identifiers to be broken, e.g.
170 :     % \def\={\discretionary{}{}{}} %optional break
171 :     % FOO-\=BAR.
172 :    
173 :     % generic code setup
174 :     %
175 :     \def\cd@setup{%
176 :     \parsep=0pt%
177 :     \parindent=0pt%
178 :     \def\\{\char`\\}\def\_{\char`\_}%
179 :     \def\{{\char`\{}\def\}{\char`\}}%
180 :     \let\dcd=\cd@dcd\let\cddollar=\cd@dollarspecial\let\cdmath=\cd@mathspecial%
181 :     \@makeother\$\@makeother\&\@makeother\#%
182 :     \@makeother\^\@makeother\_\@makeother\~%
183 :     \@makeother\%\cd@obeytabs\cd@obeyspaces}
184 :     % other: $&#^_~%
185 :     % left special: \{}
186 :     % unnecessary: @`'"
187 :    
188 :     %
189 :     % Set-up for code in displays (code, codeaux, tightcode, centercode).
190 :     %
191 :     %\def\setupdisplaycode{%
192 :     \newcommand{\setupdisplaycode}{%
193 :     \renewcommand{\baselinestretch}{1.0}%
194 :     \normalfont\codedisplaysize\codefontfamily\selectfont\frenchspacing\catcode``=13\@noligs%
195 :     \cd@setup{}}
196 :     %
197 :     % Set-up for inline code (\cd{}).
198 :     %
199 :     \def\setupcode{%
200 :     \normalfont\codefontfamily\codesize\frenchspacing\catcode``=13\@noligs%
201 :     \cd@setup{}}
202 :    
203 :    
204 :     %% codebox, centercode
205 :     %%=============================================================================
206 :     %% The codebox env makes a box exactly as wide as it needs to be
207 :     %% (i.e., as wide as the longest line of code is). This is useful
208 :     %% if you want to center a chunk of code, or flush it right, or
209 :     %% something like that. The optional argument to the environment,
210 :     %% [t], [c], or [b], specifies how to vertically align the codebox,
211 :     %% just as with arrays or other boxes. Default is [c].
212 :    
213 :     %% Must be a newline immediately after "\begin{codebox}[t]"!
214 :    
215 :     {\catcode`\^^M=\active % these lines must end with %
216 :     \gdef\cd@obeycr{\catcode`\^^M=\active\let^^M=\cr}}
217 :    
218 :     % If there is a [<letter>] option, then the following newline will
219 :     % be read *after* ^M is bound to \cr, so we're cool. If there isn't
220 :     % an option given (i.e., default to [c]), then the @\ifnextchar will
221 :     % gobble up the newline as it gobbles whitespace. So we insert the
222 :     % \cr explicitly. Isn't TeX fun?
223 :     \def\codebox{\leavevmode\@ifnextchar[{\@codebox}{\@codebox[c]\cr}} %]
224 :    
225 :     \def\@codebox[#1]%
226 :     {\hbox\bgroup$\if #1t\vtop \else \if#1b\vbox \else \vcenter \fi\fi\bgroup%
227 :     \tabskip\z@\setupdisplaycode\cd@obeycr% just before cd@obey
228 :     \halign\bgroup##\hfil\span}
229 :    
230 :     \def\endcodebox{\crcr\egroup\egroup\m@th$\egroup}
231 :    
232 :     % Center the box on the page:
233 :     \newenvironment{centercode}%
234 :     {\begin{center}\begin{codebox}[c]}%
235 :     {\end{codebox}\end{center}}
236 :    
237 :    
238 :     %% code, codeaux, tightcode
239 :     %%=============================================================================
240 :     %% Code environment as described above. Lines are kept on one page.
241 :     %% This actually works by setting a huge penalty for breaking
242 :     %% between lines of code. Code is indented same as other displayed paras.
243 :     %% Note: to increase left margin, use \begin{codeaux}{\leftmargin=1in}.
244 :    
245 :     % To allow pagebreaks, say \codeallowbreaks immediately inside the env.
246 :     % You can allow breaks at specific lines with a \pagebreak form.
247 :    
248 :     %% N.B.: The \global\@ignoretrue command must be performed just inside
249 :     %% the *last* \end{...} before the following text. If not, you will
250 :     %% get an extra space on the following line. Blech.
251 :    
252 :     %% This environment takes two arguments.
253 :     %% The second, required argument is the \list parameters to override the
254 :     %% \@listi... defaults.
255 :     %% - Usefully set by clients: \topsep \leftmargin
256 :     %% - Possible, but less useful: \partopsep
257 :     %% The first, optional argument is the extra \parskip glue that you get around
258 :     %% \list environments. It defaults to the value of \parskip.
259 :     \newenvironment{codeaux}[2][\parskip]{%
260 :     \bgroup%
261 :     \setlength{\parskip}{#1}%
262 :     \begin{list}{}{%
263 :     \setlength{\partopsep}{0pt}%
264 :     \setlength{\topsep}{0pt}%
265 :     \setlength{\parsep}{0pt}%
266 :     \setlength{\itemsep}{0pt}%
267 :     \setlength{\listparindent}{0pt}%
268 :     \setlength{\leftmargin}{\codeindent}%
269 :     #2%
270 :     }%
271 :     \item[]\setupdisplaycode\cd@obeylines%
272 :     }{%
273 :     \end{list}\leavevmode\egroup\ignorespaces\global\@ignoretrue%
274 :     }
275 :    
276 :     %% Code env is codeaux with the default margin and spacing \list params:
277 :     \newenvironment{code}{\begin{codeaux}{}}{\end{codeaux}}
278 :    
279 :     %% Like code, but with no extra vertical space above and below.
280 :     \newenvironment{tightcode}{\begin{codeaux}[0pt]{}}{\end{codeaux}}
281 :    
282 :    
283 :     % Reasonable separation between lines of code
284 :     \newcommand{\codeskip}{\penalty0\vspace{2ex}}
285 :    
286 :    
287 :     % \cd is used to build a code environment in the middle of text.
288 :     % Note: only difference from display code is that cr's are taken
289 :     % as unbreakable spaces instead of linebreaks.
290 :    
291 :     \def\cd{\leavevmode\begingroup\ifmmode\let\startcode=\startmcode\else%
292 :     \let\startcode\starttcode\fi%
293 :     \setupcode\cd@obeycrsp\startcode}
294 :    
295 :     \def\cdm{\leavevmode\begingroup\ifmmode\let\startcode=\startmcode\else%
296 :     \let\startcode\starttcode\fi%
297 :     \setupcode\cd@obeycrsp\cd@mathspecial\startcode}
298 :    
299 :     \def\starttcode#1{#1\endgroup}
300 :     %\def\startmcode#1{\hbox{#1}\endgroup} %% changed hbox to mbox --- JHR
301 :     \def\startmcode#1{\mbox{#1}\endgroup}
302 :    
303 :    
304 :     % Restore $&#^_~% to their normal catcodes
305 :     % Define \^ to give the ^ char.
306 :     % \dcd points to this guy inside a code env.
307 :     \def\cd@dcd{\catcode`\$=3\catcode`\&=4\catcode`\#=6\catcode`\^=7%
308 :     \catcode`\_=8\catcode`\~=13\catcode`\%=14\def\^{\char`\^}}
309 :    
310 :     % Selectively enable $, and $^_ as special.
311 :     % \cd@mathspecial also defines \^ give the ^ char.
312 :     % \cddollar and \cdmath point to these guys inside a code env.
313 :     \def\cd@dollarspecial{\catcode`\$=3}
314 :     \def\cd@mathspecial{\catcode`\$=3\catcode`\^=7\catcode`\_=8%
315 :     \def\^{\char`\^}}
316 :    
317 :    
318 :     % Change log:
319 :     % Started off as some macros found in C. Rich's library.
320 :     % Olin 1/90:
321 :     % Removed \makeatletter, \makeatother's -- they shouldn't be there,
322 :     % because style option files are read with makeatletter. The terminal
323 :     % makeatother screwed things up for the following style options.
324 :     % Olin 3/91:
325 :     % Rewritten.
326 :     % - Changed things so blank lines don't get compressed out (the \leavevmove
327 :     % in \cd@cr and \cd@crwb).
328 :     % - Changed names to somewhat less horrible choices.
329 :     % - Added lots of doc, so casual hackers can more easily mess with all this.
330 :     % - Removed `'"@ from the set of hacked chars, since they are already
331 :     % non-special.
332 :     % - Removed the bigcode env, which effect can be had with the \codeallowbreaks
333 :     % command.
334 :     % - Removed the \@noligs command, since it's already defined in latex.tex.
335 :     % - Win big with the new \dcd, \cddollar, and \cdmath commands.
336 :     % - Now, *only* the chars \{} are special inside the code env. If you need
337 :     % more, use the \dcd command inside a group.
338 :     % - \cd now works inside math mode. (But if you use it in a superscript,
339 :     % it still comes out full size. You must explicitly put a \scriptsize\tt
340 :     % inside the \cd: $x^{\cd{\scriptsize\tt...}}$. A \leavevmode was added
341 :     % so that if you begin a paragraph with a \cd{...}, TeX realises you
342 :     % are starting a paragraph.
343 :     % - Added the codebox env. Tricky bit involving the first line hacked
344 :     % with help from David Long.
345 :     %
346 :     % JHR 8/19/91:
347 :     % - Added \setupsmallcode to use in multi-line code displays (code, codeaux and
348 :     % codebox environments).
349 :     %
350 :     % JHR 8/31/91:
351 :     % - changed size of small code to \small (from \footnotesize). Also added
352 :     % code to set the baselinestretch to 1 in smallcode.
353 :     %
354 :     % JHR 9/12/91:
355 :     % - added \codeindent (set to \parindent)
356 :     %
357 :     % JHR 11/19/91
358 :     % - added \cdm{} command for supporting math mode in \cd{}
359 :     %
360 :     % Olin 8/94
361 :     % Changed the font commands for LaTeX2e.
362 :     %
363 :     % JHR 5/25/95
364 :     % Merged Olin's LaTeX2e changes into JHR's version
365 :     %
366 :     % JHR 9/16/98
367 :     % Use LaTeX2e features to make the code a bit cleaner.
368 :     % Added \codeindent length, which controls indentation.
369 :     % Added \codefontfamily, \codedisplaysize and \codesize
370 :     % commands to control the font family and sizes used in
371 :     % code.

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