The way to think of this is that ##
gets replaced by #
in just the
same way that #1
gets replaced by ‘whatever is the first argument’.
So if you define a macro:
or (using the TeX primitive\newcommand\a[1]{+#1+#1+#1+}
\def
):
and use it as\def\a#1{+#1+#1+#1+}
\a
{b}
,
the macro expansion produces ‘+b+b+b+’,
as most people would expect.
However, if we now replace part of the macro:
then\newcommand\a[1]{+#1+\newcommand\x[1]{xxx#1}}
\a
{b}
will give us the rather odd
+b+so that the new\newcommand
{x}
[1]
{xxxb}
\x
ignores its argument.
If we use the TeX primitive:
\def\a#1{+#1+\def\x #1{xxx#1}}
\a
{b}
will expand to ‘+b+\def\x bb;xxxb}
’. This
defines \x
to be a macro delimited by b
, and taking no
arguments, which is surely not what was intended!
Actually, to define \x
to take an argument, we need
or, using the TeX primitive definition:\newcommand\a[1]{+#1+\newcommand\x[1]{xxx##1}}
and\def\a#1{+#1+\def\x ##1{xxx##1}}
\a
{b}
will expand to
+b+\def\x #1{xxx#1}
because #1
gets replaced by ‘b’
and ##
gets replaced by #
.
To nest a definition inside a definition inside a definition then you
need ####1
, doubling the number of #
signs; and at the next level
you need 8 #
s each time, and so on.
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=hash