chiark / gitweb /
syntax.dtx: Allow decorative material following nonterminal name in `grammar'.
[mdwtools] / cmtt.dtx
1 % \begin{meta-comment}
2 %
3 % $Id: cmtt.dtx,v 1.3 2003/09/05 16:12:04 mdw Exp $
4 %
5 % Nicer handling of the Computer Modern Typewriter font
6 %
7 % (c) 1996 Mark Wooding
8 %
9 % \end{meta-comment}
10 %
11 % \begin{meta-comment} <general public licence>
12 %%
13 %% mdwlist package -- various list-related things
14 %% Copyright (c) 1996 Mark Wooding
15 %%
16 %% This program is free software; you can redistribute it and/or modify
17 %% it under the terms of the GNU General Public License as published by
18 %% the Free Software Foundation; either version 2 of the License, or
19 %% (at your option) any later version.
20 %%
21 %% This program is distributed in the hope that it will be useful,
22 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
23 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 %% GNU General Public License for more details.
25 %%
26 %% You should have received a copy of the GNU General Public License
27 %% along with this program; if not, write to the Free Software
28 %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 %%
30 % \end{meta-comment}
31 %
32 %
33 % \begin{meta-comment} <Package preambles>
34 %<+sty>\NeedsTeXFormat{LaTeX2e}
35 %<+sty>\ProvidesPackage{cmtt}
36 %<+fd>\ProvidesFile{mttcmtt.fd}
37 %<+def>\ProvidesFile{mttcmtt.def}
38 %<+sty|fd|def>         [1996/05/25 1.1 Handing of the cmtt font]
39 % \end{meta-comment}
40 %
41 % \CheckSum{174}
42 %% \CharacterTable
43 %%  {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
44 %%   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
45 %%   Digits        \0\1\2\3\4\5\6\7\8\9
46 %%   Exclamation   \!     Double quote  \"     Hash (number) \#
47 %%   Dollar        \$     Percent       \%     Ampersand     \&
48 %%   Acute accent  \'     Left paren    \(     Right paren   \)
49 %%   Asterisk      \*     Plus          \+     Comma         \,
50 %%   Minus         \-     Point         \.     Solidus       \/
51 %%   Colon         \:     Semicolon     \;     Less than     \<
52 %%   Equals        \=     Greater than  \>     Question mark \?
53 %%   Commercial at \@     Left bracket  \[     Backslash     \\
54 %%   Right bracket \]     Circumflex    \^     Underscore    \_
55 %%   Grave accent  \`     Left brace    \{     Vertical bar  \|
56 %%   Right brace   \}     Tilde         \~}
57 %%
58 %
59 % \begin{meta-comment}
60 %
61 %<*driver>
62 \input{mdwtools}
63 \describespackage{cmtt}
64 \mdwdoc
65 %</driver>
66 %
67 % \end{meta-comment}
68 %
69 %^^A-------------------------------------------------------------------------
70 % \section{Introductory note}
71 %
72 % \LaTeX\ has a rather cunning encoding handling system, which makes funny
73 % commands like accents work properly independent of the current font's
74 % actual layout.  While this works rather well most of the time, the standard
75 % \mtt{tt} font has been rather left out of things.  \LaTeX\ assumes that
76 % the Computer Modern Typewriter fonts have exactly the same layout as the
77 % more normal Computer Modern Roman family (i.e., that both conform to the
78 % \mtt{OT1} encoding).  This plainly isn't true, since the Typewriter font
79 % contains a bunch of standard ASCII characters which are omitted from the
80 % standard Computer Modern fonts, such as curly braces \mtt{\{} and \mtt{\}},
81 % and the backslash \mtt{\\}; these are usually dug up from the maths fonts,
82 % which looks fine in normal text, but looks really odd in monospace text.
83 % Compare `\texttt{\textbackslash begin\{document\}}' to
84 % `\mtt{\\begin\{document\}}', for example.
85 %
86 % There are two possibilities for dealing with this problem.  One is to use
87 % the \mtt{\\verb} command, which works since all the extra characters in
88 % the Typewriter font are in the correct places, or use the DC~fonts, which
89 % have a proper encoding set up which contains all of these special
90 % characters anyway.
91 %
92 % Neither of these solutions is perfect.  Using \mtt{\\verb} causes all
93 % manner of little niggly problems: you can't use it in footnotes or
94 % section headings, for example.  (There are of course workarounds for this
95 % sort of thing: the author's \package{footnote} package provides a
96 % \env{footnote} environment which will allow verbatim text, and verbatim
97 % text in section headings can be achieved if one is sufficiently
98 % \TeX nical.)  Using the DC~fonts is fine, although you actually lose a
99 % glyph or two.  As far as the author is aware, the character \mtt{\'} (an
100 % `unsexed' single quote) is not present in the \mtt{T1}-encoded version of
101 % Computer Modern Typewriter, although it is hidden away in the original
102 % version.  The author has found a need for this character in computer
103 % listings, and was horrified to discover that it was replaced by a German
104 % single quote character (\mtt{\\quotesinglbase}).
105 %
106 % This package defines a special encoding for the Computer Modern Typewriter
107 % font, so that documents can take advantage of its ASCII characters without
108 % resorting to verbatim text.  (The main advantage of the DC~fonts, that
109 % words containing accents can be hyohenated, doesn't really apply to the
110 % Typewriter font, since it doesn't allow hyphenation by default anyway.)
111 %
112 % There are several files you'll need to create:
113 % \begin{description} \def\makelabel#1{\hskip\labelsep\mttfamily#1\hfil}
114 %
115 % \item [cmtt.sty] tells \LaTeX\ that there's a new encoding.  It also
116 %       provides some options for customising some aspects of the
117 %       encoding, and defines some useful commands.
118 %
119 % \item [mttenc.def] describes the encoding to \LaTeX: it sets up all the
120 %       appropriate text commands so that they produce beautiful results.
121 %
122 % \item [mttcmtt.fd] describes the re-encoded version of the font.  this
123 %       is more or less a copy of the file \mtt{ot1cmtt.fd}.
124 %
125 % \end{description}
126 %
127 % The package accepts some options which may be useful:
128 % \begin{description} \def\makelabel#1{\hskip\labelsep\sffamily#1\hfil}
129 %
130 % \item [override] overrides the meaning of the \mtt{\\ttfamily} command
131 %       (and therefore also the \mtt{\\texttt} command too), making it the
132 %       same as the new \mtt{\\mttfamily} command.  This isn't the default
133 %       just in case the change breaks something in an unexpected way.
134 %
135 % \item [t1] informs the package that you're using the \mtt{T1} encoding,
136 %       and therefore can borrow some accented characters from the DC~version
137 %       of Computer Modern Typewriter.  This will probably be unnecessary,
138 %       since the package attempts to work out what to do all by itself.
139 %
140 % \item [ot1] forces the package \emph{not} to use the DC~version of the
141 %       Computer Modern Typewriter font for funny accents.  Only use this
142 %       option if the package thinks it should use the DC~Typewriter font
143 %       when it shouldn't.
144 %
145 % \end{description}
146 %
147 % \DescribeMacro{\mttfamily}
148 % The command \mtt{\\mttfamily} selects the properly-encoded Typewriter
149 % font.  It's a declaration which works just like the \mtt{\\ttfamily}
150 % command, except that comamnds like \mtt{\\\}} and \mtt{\\\_} use the
151 % characters from the font rather than choosing odd-looking versions from
152 % the maths fonts.  All of the accent commands still work properly.  In fact,
153 % some accent commands which didn't work before have been fixed.  For
154 % example, saying `\mtt{\\texttt\{P\\'al Erd\\H os\}}' would produce
155 % something truly appalling like `\texttt{P\'al Erd\H os}', which is
156 % obviously ghastly.  The new encoding handles this properly, and produces
157 % `\textmtt{P\'al Erd\H os}'.\footnote{
158 %   This isn't quite perfect.  The accent, which isn't actually present in
159 %   the Typewriter font, is taken from the Computer Modern bold font, but
160 %   it doesn't look too bad.  However, if you pass the option \textsf{t1}
161 %   to the \package{cmtt} package when you load it, the accent will be taken
162 %   from the DC~Typewriter font, and it will look totally wonderful.}
163 %
164 % \DescribeMacro{\textmtt}
165 % Font changing commands are much more convenient than th declarations,
166 % so a command \mtt{\\textmtt} is provided: it just typesets its argument
167 % in the re-encoded Typewriter font.
168 %
169 % \DescribeMacro{\mtt}
170 % Rather more excitingly, the \mtt{\\mtt} command allows you to generate
171 % almost-verbatim text very easily, without any of the restrictions of
172 % the \mtt{\\verb} command.  This command was inspired by something which
173 % David Carlisle said to me in an email correspondence regarding the
174 % overuse of verbatim commands.
175 %
176 % \mtt{\\mtt} redefines several `short' commands to typeset the obvious
177 % characters.  The complete list is shown below: there are some oddities,
178 % so watch out.
179 %
180 % ^^A This is an evil table.  See if I care.  (This is based on lots of
181 % ^^A hacking I did in glyphs.tex, but a good deal less horrible.)
182 %
183 % \medskip
184 % \hbox to \hsize\bgroup
185 % \hfil\vbox\bgroup
186 % \def\ex#1#2{\strut
187 %   \enskip
188 %   \mtt{\\\char`#2}\quad\hfil%
189 %   \mtt{#2}\enskip}
190 % \def\h{\noalign{\hrule}}
191 % \def\v{height2pt&\omit&&\omit&&\omit&&\omit&&\omit&\cr}
192 % \let~\relax
193 % \offinterlineskip
194 % \ialign\bgroup&\vrule#&\ex#\cr        \h\v
195 % &~\\&&~\{&&~\}&&~\_&&~\^&\cr        \v\h\v
196 % &~\$&&~\%&&~\&&&~\#&&~\~&\cr        \v\h\v
197 % &~\"&&~\'&&~\ &&~\|&&\omit\hfil&\cr \v\h
198 % \egroup\egroup
199 % \hfil\egroup
200 % \medskip
201 %
202 % As well as redefining these commands, \mtt{\\mtt} will endeavour to make
203 % single special characters display themselves in a verbatim-like way.  This
204 % only works on `active' characters (like \mtt{~}), and \mtt{\\mtt} makes
205 % no attempt to change the category codes of any characters.
206 %
207 % Among other things, you'll probably noticed that several accent-making
208 % commands have been redefined.  You can still use these accents through
209 % the \mtt{\\a} command, by saying \mtt{\\a'}, \mtt{\\a\^} and so on,
210 % as in the \env{tabbing} environment.
211 %
212 % There are also some oddities in the table: \mtt{\|} and \mtt{\"} can be
213 % accessed easily without playing with silly commands.  Well, that's almost
214 % the case: these two characters are both often used as `short' verbatim
215 % commands, so they are forced back to their normal meanings so you can
216 % type them.
217 %
218 % Finally, a word on spacing.  The \mtt{\\\ } command has been hijacked
219 % to produce a funny `visible space' character.  You can still produce
220 % multiple spaces by saying something like `\mtt{\ \{\}\ \{\}}\dots\mtt{\ }',
221 % which is a bit contrived, but that's tough.  Also, \mtt{~} has been stolen
222 % so that you can type \mtt{~} characters (e.g., in URLs), so the only
223 % way you can tpye a nonbreaking space is by using the \mtt{\\nobreakspace}
224 % command, which is a bit of a mouthful.  There's an abbreviation, though:
225 % \mtt{\\nbsp} now means exactly the same thing.
226 %
227 % Was that not all supremely useful?  Oh, just a note: this document doesn't
228 % use a single verbatim command or environment (except in the listings,
229 % where it's unavoidable) -- it's all done with \mtt{\\mtt}.
230 %
231 % \implementation
232 %
233 % \section{Implementation}
234 %
235 % \subsection{The package}
236 %
237 %    \begin{macrocode}
238 %<*sty>
239 %    \end{macrocode}
240 %
241 % I'll start with some options handling.
242 %
243 %    \begin{macrocode}
244 \newif\ifcmtt@override
245 \newif\ifcmtt@dcfonts
246 \def\@tempa{T1}\ifx\encodingdefault\@tempa
247   \cmtt@dcfontstrue
248 \fi
249 \DeclareOption{override}{\cmtt@overridetrue}
250 \DeclareOption{t1}{\cmtt@dcfontstrue}
251 \DeclareOption{ot1}{\cmtt@dcfontsfalse}
252 \ProcessOptions
253 %    \end{macrocode}
254 %
255 % This bit is really trivial.  I'll just declare the font encoding.  Oh, that
256 % was easy.
257 %
258 %    \begin{macrocode}
259 \DeclareFontEncoding{mTT}{}{}
260 %    \end{macrocode}
261 %
262 % Wait: there's a problem.  \LaTeX\ will now complain bitterly that it can't
263 % find the font \mtt{mTT/cmr/m/n}, which is reasonable, since I haven't
264 % declared any such font.  The following line should sort this out,
265 %
266 %    \begin{macrocode}
267 \DeclareFontSubstitution{mTT}{cmtt}{m}{n}
268 %    \end{macrocode}
269 %
270 % Now I'd better load all the text commands I'll need when in this funny
271 % font variant.
272 %
273 %    \begin{macrocode}
274 \input{mttenc.def}
275 %    \end{macrocode}
276 %
277 % \begin{macro}{\mttfamily}
278 % \begin{macro}{\textmtt}
279 %
280 % Finally, I'll need to define a command which switches to this funny font,
281 % and a \mtt{\\text}\dots\ command for it.
282 %
283 %    \begin{macrocode}
284 \DeclareRobustCommand{\mttfamily}{%
285   \fontencoding{mTT}\fontfamily{\ttdefault}\selectfont%
286 }
287 \DeclareTextFontCommand{\textmtt}{\mttfamily}
288 %    \end{macrocode}
289 %
290 % \end{macro}
291 % \end{macro}
292 %
293 % If an override was requested, make \mtt{\\ttfamily} the same as
294 % \mtt{\\mttfamily}.
295 %
296 %    \begin{macrocode}
297 \ifcmtt@override
298   \let\ttfamily\mttfamily
299 \fi
300 %    \end{macrocode}
301 %
302 % Well, that's all that's needed for the font definition.  Here's a command
303 % which will typeset its argument in the typewriter font, allowing easy
304 % access to all the funny characters, and printing them properly in the
305 % correct font (which \mtt{\\\{} doesn't do, for example).
306 %
307 % \begin{macro}{\mtt@setchar}
308 %
309 % This macro assigns the given meaning to the given control sequence.  Also,
310 % if the character named in the control sequence is currently set active,
311 % it will set the active meaning of the character to the same value.
312 %
313 %    \begin{macrocode}
314 \def\mtt@setchar#1#2{%
315   \ifx#1#2\chardef#1`#1\else\let#1#2\fi%
316   \ifnum\catcode`#1=13%
317     \begingroup%
318       \lccode`\~=`#1%
319     \lowercase{\endgroup\let~#1}%
320   \fi%
321 }
322 %    \end{macrocode}
323 %
324 % \end{macro}
325 %
326 % \begin{macro}{\mtt@chars}
327 %
328 % This macro lists the various control sequences which should be set up,
329 % so that they can be easily added to.
330 %
331 %    \begin{macrocode}
332 \def\mtt@chars{%
333   \do\#\#%
334   \do\%\%%
335   \do\&\&%
336   \do\^\^%
337   \do\~\~%
338   \do\'\textquotesingl%
339   \do\"\textquotedbl%
340   \do\|\textbar%
341   \do\$\textdollar%
342   \do\_\textunderscore%
343   \do\{\textbraceleft%
344   \do\}\textbraceright%
345   \do\\\textbackslash%
346   \do\ \textvisiblespace%
347 }
348 %    \end{macrocode}
349 %
350 % \end{macro}
351 %
352 % \begin{macro}{\mtt@do}
353 %
354 % This just sets up all the special characters listed above.  It's a simple
355 % abbreviation, really.
356 %
357 %    \begin{macrocode}
358 \def\mtt@do{\let\do\mtt@setchar\mtt@chars}
359 %    \end{macrocode}
360 %
361 % \end{macro}
362 %
363 % \begin{macro}{\mtt}
364 %
365 % And finally, the macro itself.  Ta-da!
366 %
367 %    \begin{macrocode}
368 \DeclareRobustCommand\mtt[1]{\textmtt{\mtt@do#1}}
369 %    \end{macrocode}
370 %
371 % \end{macro}
372 %
373 % \begin{macro}{\@tabacckludge}
374 %
375 % The otherwise almost totally perfect \mtt{\\@tabacckludge} gets very
376 % upset when its argument is an active character.  (If you're wondering,
377 % this is the command which is responsible for the behaviour of the \mtt{\\a}
378 % command.)  Adding a \mtt{\\string} makes everything work perfectly.
379 %
380 %    \begin{macrocode}
381 \def\@tabacckludge#1{%
382   \expandafter\@changed@cmd\csname\string#1\endcsname\relax%
383 }
384 \let\a\@tabacckludge
385 %    \end{macrocode}
386 %
387 % \end{macro}
388 %
389 % \begin{macro}{\nbsp}
390 %
391 % Make an abbreviation for \mtt{\\nobreakspace}.
392 %
393 %    \begin{macrocode}
394 \let\nbsp\nobreakspace
395 %    \end{macrocode}
396 %
397 % \end{macro}
398 %
399 % I think that's all that I have to do for the package.  If there's any
400 % more to do, I'll add it later.
401 %
402 %    \begin{macrocode}
403 %</sty>
404 %    \end{macrocode}
405 %
406 %
407 % \subsection{The font definition file}
408 %
409 % This is obviously copied almost verbatim from the file \mtt{OT1cmtt.fd}.
410 %
411 %    \begin{macrocode}
412 %<*fd>
413 \DeclareFontFamily{mTT}{cmtt}{\hyphenchar\font\m@ne}
414 \DeclareFontShape{mTT}{cmtt}{m}{n}{
415   <5> <6> <7> <8> cmtt8
416   <9> cmtt9
417   <10> <10.95> cmtt10
418   <12> <14.4> <17.28> <20.74> <24.88> cmtt12
419 }{}
420 \DeclareFontShape{mTT}{cmtt}{m}{it}{
421   <5> <6> <7> <8> <9> <10> <10.95> <12> <14.4> <17.28> <20.74> <24.88>
422   cmitt10
423 }{}
424 \DeclareFontShape{mTT}{cmtt}{m}{sl}{
425   <5> <6> <7> <8> <9> <10> <10.95> <12> <14.4> <17.28> <20.74> <24.88>
426   cmsltt10
427 }{}
428 \DeclareFontShape{mTT}{cmtt}{m}{sc}{
429   <5> <6> <7> <8> <9> <10> <10.95> <12> <14.4> <17.28> <20.74> <24.88>
430   cmtcsc10
431 }{}
432 \DeclareFontShape{mTT}{cmtt}{m}{ui}  {<->sub * cmtt/m/it} {}
433 \DeclareFontShape{mTT}{cmtt}{bx}{n}  {<->sub * cmtt/m/n}  {}
434 \DeclareFontShape{mTT}{cmtt}{bx}{it} {<->sub * cmtt/m/it} {}
435 \DeclareFontShape{mTT}{cmtt}{bx}{ui} {<->sub * cmtt/m/it} {}
436 %</fd>
437 %    \end{macrocode}
438 %
439 %
440 % \subsection{The encoding definitions file}
441 %
442 % I've saved the trickiest bit until last.  This file defines the mappings
443 % from text commands to glyphs in the font.
444 %
445 %    \begin{macrocode}
446 %<*def>
447 %    \end{macrocode}
448 %
449 % First for some fun with accents.  The |cmtt| font doesn't contain all of
450 % the accents which the other Computer Modern fonts do, because those slots
451 % contain the standard ASCII characters which usually have to be `borrowed'
452 % from the maths fonts.
453 %
454 % Anyway, there's a load which don't need any special treatment.  These are
455 % chosen from the \mtt{OT1} encoding by default anyway, so I needn't
456 % bother unless I'm really bothered about speed.  I'm not, so I'll save
457 % the memory.
458 %
459 % Following the example of the \TeX book, I'll use the bold roman font
460 % for accents, so that they don't look really spindly.  This is actually
461 % remarkably difficult to do, because the \textsf{NFSS} keeps getting in
462 % the way.  I'll look after the old font name in a macro (it's handy that
463 % \textsf{NFSS} maintains this for me) and change to a known font, do the
464 % accent, change font back again, do the argument to the accent, and then
465 % close the group I did all of this in, so that no-one else notices what a
466 % naughty chap I am, really.  This is startlingly evil.
467 %
468 %    \begin{macrocode}
469 \def\cmtt@accent#1#2{{%
470   \let\@old@font\font@name%
471   \ifcmtt@dcfonts%
472     \fontencoding{T1}\selectfont%
473   \else%
474     \usefont{OT1}{cmr}{bx}{n}%
475   \fi%
476   #1{\@old@font#2}%
477 }}
478 %    \end{macrocode}
479 %
480 % And now for the actual offending accents.
481 %
482 %    \begin{macrocode}
483 \DeclareTextCommand{\H}{mTT}{\cmtt@accent\H}
484 \DeclareTextCommand{\.}{mTT}{\cmtt@accent\.}
485 %    \end{macrocode}
486 %
487 % The `under' accents are all OK, so I shan't bother to define them either.
488 % Similarly, lots of the text symbol commands are fine as they are by
489 % default and I don't need to try and define them again.
490 %
491 % This, then, is the remaining commands which really need sorting out.
492 % (By the way, the only reason I've redefined \mtt{\\textellipsis} is
493 % because otherwise it will mess up the nice monospacing.)
494 %
495 %    \begin{macrocode}
496 \DeclareTextSymbol{\textbackslash}{mTT}{92}
497 \DeclareTextSymbol{\textbar}{mTT}{124}
498 \DeclareTextSymbol{\textbraceleft}{mTT}{123}
499 \DeclareTextSymbol{\textbraceright}{mTT}{125}
500 \DeclareTextSymbol{\textless}{mTT}{60}
501 \DeclareTextSymbol{\textgreater}{mTT}{62}
502 \DeclareTextSymbol{\textunderscore}{mTT}{95}
503 \DeclareTextSymbol{\textvisiblespace}{mTT}{32}
504 \DeclareTextCommand{\textellipsis}{mTT}{...}
505 \DeclareTextSymbol{\textquotedbl}{mTT}{34}
506 \DeclareTextSymbol{\textquotesingl}{mTT}{13}
507 %    \end{macrocode}
508 %
509 % That's all there is.  Please return to your homes.
510 %
511 % \Finale
512 %
513 \endinput