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