File: FAQ-hash.html

package info (click to toggle)
tetex-base 3.0.dfsg.3-5
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 239,540 kB
  • ctags: 10,610
  • sloc: xml: 103,461; perl: 9,398; ruby: 2,850; python: 1,551; php: 1,067; sh: 981; lisp: 494; makefile: 371; awk: 88
file content (37 lines) | stat: -rw-r--r-- 1,687 bytes parent folder | download | duplicates (2)
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
<head>
<title>UK TeX FAQ -- question label hash</title>
</head><body>
<h3>Defining macros within macros</h3>
<p>The way to think of this is that <code>##</code> gets replaced by <code>#</code> in just the
same way that <code>#1</code> gets replaced by 'whatever is the first argument'.
<p>So if you define a macro and use it as:
<blockquote>
<pre>
\def\a#1{+++#1+++#1+++#1+++}  \a{b}
</pre>
</blockquote>
the macro expansion produces '+++b+++b+++b+++',
which people find normal.  However, if we now replace part of the macro:
<blockquote>
<pre>
\def\a#1{+++#1+++\def\x #1{xxx#1}}
</pre>
</blockquote>
<code>\</code><code>a{b</code>} will expand to '+++b+++<code>\def\x b{xxxb}</code>'.  This
defines <code>\</code><code>x</code> to be a macro <em>delimited</em> by <code>b</code>, and taking no
arguments, which people may find strange, even though it is just a
specialisation of the example above.  If you want <code>\</code><code>a</code> to
define <code>\</code><code>x</code> to be a macro with one argument, you need to write:
<blockquote>
<pre>
\def\a#1{+++#1+++\def\x ##1{xxx##1}}
</pre>
</blockquote>
and <code>\</code><code>a{b</code>} will expand to 
'+++b+++<code>\def\x #1{xxx#1}</code>', because <code>#1</code> gets replaced by 'b'
and <code>##</code> gets replaced by <code>#</code>.
<p>To nest a definition inside a definition inside a definition then
you need <code>####1</code>, as at each stage <code>##</code> is replaced by
<code>#</code>.  At the next level you need 8 <code>#</code>s each time, and so on.
<p><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=hash">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=hash</a>
</body>