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