6

Possible Duplicate:
What is the meaning of double pound symbol (##1) in an argument?

I see some LaTeX3 code with ##1 instead of #1 for parameters. What are the rules for this? This answer uses it, for example.

1 Answers1

11

You're referring to

\cs_new_protected:Npn \malmedal_output_direct:
 {
  \int_zero:N \l_malmedal_count_int
  \seq_map_inline:Nn \g_malmedal_input_seq
   {
    \int_incr:N \l_malmedal_count_int
    \malmedal_print:n { ##1 }
   }
 }

Let's see how \seq_map_inline:Nn works. According to the convention, the first argument should be a single token, in this case the name of a sequence; the second argument must be a braced list of tokens. The working of this (and most of the other mapping functions) is to present the code in the second argument each element of the sequence one after the other. This piece of information is represented by #1 (because so the developers chose, and it's very handy): so a "naked" call of \seq_map_inline:Nn is typically of the form

\seq_map_inline:Nn \l_some_seq { \some_cs:n { #1 } }

(but any other code can be used in the second argument). However, in the code above, the function appears in the replacement text of a definition, so the old TeX rule applies, of which

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

is a typical example: the call \x{\y} will be replaced by

\def\y#1{--#1--}

This is how Knuth allowed to put definition of macros with arguments inside other definitions; the rule is that, when a replacement text is being scanned, a single # must be followed by a digit 1 to 9 thus representing a parameter; but a ## will be reduced to a single # when the replacement text is used during macro expansion.

In other words, when \malmedal_output_direct: is expanded, the ##1 will become #1 and keep TeX happy, since the code will be just as expected (as in the typical example above).

egreg
  • 1,121,712