Go to the first, previous, next, last question, table of contents.

Why are # signs doubled in macros?

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 and use it as:

  \def\a#1{...#1...#1...#1...}  \a{b}

the macro expansion produces `...b...b...b...', which people find normal. However, if we now fill in the `...':

  \def\a#1{---#1---\def\x #1{xxx#1}}

\a{b} will expand to `---b---\def\x b{xxxb}'. This defines \x to be a macro delimited by b, and taking no arguments, which people may find strange, even though it is just a specialisation of the example above. If you want \a to define \x to be a macro with one argument, you need to write:

  \def\a#1{---#1---\def\x ##1{xxx##1}}

and \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, as at each stage ## is replaced by #. At the next level you need 8 #s each time, and so on.


Go to the first, previous, next, last question, table of contents.