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
|
<head>
<title>UK TeX FAQ -- question label whatmacros</title>
</head><body>
<h3>What are (TeX) macros</h3>
<p/>TeX is a <em>macro processor</em>: this is a computer-science-y term
meaning “text expander” (more or less); TeX typesets text as it
goes along, but <em>expands</em> each macro it finds. TeX’s macros
may include instructions to TeX itself, on top of the simple text
generation one might expect.
<p/>Macros are a <em>good thing</em>, since they allow the user to
manipulate documents according to context. For example, the macro
<code>\</code><code>TeX</code> is usually defined to produce “TEX” with the ‘E’ lowered,
but in these FAQs the default definition of the macro is
overridden, and it simply expands to the letters “TeX”. (<em>You</em>
may not think this a good thing, but the author of the macros has his
reasons – see <a href="FAQ-logos.html">TeX-related logos</a>.)
<p/>Macro names are conventionally built from a <code>\</code>
followed by a sequence of letters, which may be upper or lower case
(as in <code>\</code><code>TeX</code>, mentioned above). They may also be
<code>\<<i>any single character</i>></code>, which allows all
sorts of oddities (many built in to most TeX macro sets, all the
way up from the apparently simple ‘<code>\</code><code> </code>’ meaning “insert a space
here”).
<p/>Macro programming can be a complicated business, but at their very
simplest they need little introduction — you’ll hardly need to be
told that:
<blockquote>
<pre>
\def\foo{bar}
</pre>
</blockquote><p>
replaces each instance of <code>\</code><code>foo</code> with the text “bar”. The
command <code>\</code><code>def</code> is Plain TeX syntax for defining commands;
LaTeX offers a macro <code>\</code><code>newcommand</code> that goes some way towards
protecting users from themselves, but basically does the same thing:
<blockquote>
<pre>
\newcommand{\foo}{bar}
</pre>
</blockquote><p>
Macros may have “arguments”, which are used to substitute for marked
bits of the macro expansion:
<blockquote>
<pre>
\def\foo#1{This is a #1 bar}
...
\foo{2/4}
</pre>
</blockquote><p>
or, in LaTeX speak:
<blockquote>
<pre>
\newcommand{\foo}[1]{This is a #1 bar}
...
\foo{3/4}
</pre>
</blockquote><p>
Macro writing can get very complicated, very quickly. If you are a
beginner (La)TeX programmer, you are well advised to read something
along the lines of the <a href="FAQ-books.html">TeXbook</a>; once you’re under
way, <a href="FAQ-ol-books.html">TeX by Topic</a> is possibly a more satisfactory
choice. Rather a lot of the answers in these FAQs tell you
about various issues of how to write macros.
<p/><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=whatmacros">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=whatmacros</a>
</body>
|