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