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