summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-activechars.html
blob: 8f21c60a55612a002c282f19ec880aa4fcc169ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<head>
<title>UK TeX FAQ -- question label activechars</title>
</head><body>
<h3>Defining characters as macros</h3>
<p/>Single characters can act as macros (defined commands), and both
Plain TeX and LaTeX define the character
&ldquo;<code>~</code>&rdquo; as a &ldquo;non-breakable space&rdquo;.  A
character is made definable, or &ldquo;active&rdquo;, by setting its
<em>category code</em> (catcode) to be <code>\</code><code>active</code> (13):
<code>\catcode&lsquo;_=\active</code>.
<p/>Any character could, in principle, be activated this way and defined
as a macro (<code>\</code><code>def</code><code>_{</code><code>\</code><code>_</code><code>}</code> &mdash; the simple answer to

<a href="FAQ-underscore.html">using underscores</a>), but you must be
wary: whereas people expect an active tilde, other active characters
may be unexpected and interact badly with other macros.  Furthermore,
by defining an active character, you preclude the character&rsquo;s use for
other purposes, and there are few characters &ldquo;free&rdquo; to be subverted
in this way.
<p/>To define the character &ldquo;<code>z</code>&rdquo; as a command, one would say something
like:
<blockquote>
<pre>
\catcode`\z=\active
\def z{Yawn, I'm tired}%
</pre>
</blockquote><p>
and each subsequent &ldquo;<code>z</code>&rdquo; in the text would become a yawn. This would be
an astoundingly bad idea for most documents, but might have special
applications. (Note that, in &ldquo;<code>\def z</code>&rdquo;, &ldquo;<code>z</code>&rdquo; is no longer interpreted as
a letter; the space is therefore not necessary &mdash; &ldquo;<code>\defz</code>&rdquo; would do; we
choose to retain the space, for what little clarity we can manage.)
Some LaTeX packages facilitate such definitions. For example, the
<i>shortvrb</i> package with its <code>\</code><code>MakeShortVerb</code> command.
<p/>TeX uses category codes to interpret characters as they are read 
from the input.

<em>Changing a catcode value will not affect characters that have already been read</em>.
Therefore, it is best if characters have fixed category codes for the
duration of a document.  If catcodes are changed for particular
purposes (the <code>\</code><code>verb</code> command does this), then the altered
characters will not be interpreted properly when they  appear in the
argument to another command (as, for example, in

&ldquo;<a href="FAQ-verbwithin.html"><code>\</code><code>verb</code> in command arguments</a>&rdquo;).
An exemplary case is the <i>doc</i> package, which processes .dtx
files using the <i>shortvrb</i> package to define
<code>|...|</code> as a shorthand for
<code>\</code><code>verb</code><code>|...|</code>. But <code>|</code> is
also used in the preambles of tabular environments, so that tables in
<code>.dtx</code> files can only have vertical line separation between
columns by employing special measures of some sort.
<p/>Another consequence is that catcode assignments made
in macros often don&rsquo;t work as expected 
(see &ldquo;<a href="FAQ-actinarg.html">Active characters in command arguments</a>&rdquo;).
For example, the definition
<blockquote>
<pre>
\def\mistake{%
\catcode`_=\active
\def_{\textunderscore\-}%
}
</pre>
</blockquote><p>
does not work because it attempts to define an ordinary <code>_</code> character:
When the macro is used, the category change does not apply to the 
underscore character already in the macro definition.  Instead, one may
use:
<blockquote>
<pre>
\begingroup
\catcode`_=\active
\gdef\works{%    note the global \gdef
  \catcode`_=\active
  \def_{\textunderscore\-}%
}
\endgroup
</pre>
</blockquote><p>
The alternative (&ldquo;tricksy&rdquo;) way of creating such an isolated
definition depends on the curious properties of <code>\</code><code>lowercase</code>, which
changes characters without altering their catcodes.  Since there is
always <em>one</em> active character (&ldquo;<code>~</code>&rdquo;), we
can fool <code>\</code><code>lowercase</code> into patching up a definition without ever
explicitly changing a catcode:
<blockquote>
<pre>
\begingroup
  \lccode`\~=`\_
  \lowercase{\endgroup
    \def~{\textunderscore\-}%
  }%
</pre>
</blockquote><p>
The two definitions have the same overall effect (the character is
defined as a command, but the character does not remain active),
except that the first defines a <code>\</code><code>global</code> command.
<p/>For active characters to be used only in maths mode, it is much better
to leave the character having its ordinary catcode, but assign it a
special active <em>maths code</em>, as with
<blockquote>
<pre>
\begingroup
  \lccode`~=`x
  \lowercase{\endgroup
    \def~{\times}%
  }%
\mathcode`x="8000
</pre>
</blockquote><p>
The special character does not need to be redefined whenever it is
made active &mdash; the definition of the command persists even if the
character&rsquo;s catcode reverts to its original value; the definition
becomes accessible again if the character once again becomes active.
<dl>
<dt><tt><i>doc.sty</i></tt><dd>Distributed as part of the source of LaTeX, <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/base.zip">macros/latex/base</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/base/">browse the directory</a>)
<dt><tt><i>shortvrb.sty</i></tt><dd>Distributed as part of <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/required/tools.zip">macros/latex/required/tools</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/required/tools/">browse the directory</a>)
</dl>
<p/><p/><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=activechars">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=activechars</a>
</body>