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