5

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.

  • I hope ##1 means the same thing in LaTeX3 that it does in TeX...: from TeX by Topic: "When TEX’s input processor scans a macro definition text, it inserts a parameter token for any occurrence of a macro parameter character followed by a digit. In effect, a parameter token in the replacement text states ‘insert parameter number such and such here’. Two parameter characters in a row are replaced by a single one." – Gonzalo Medina May 22 '12 at 20:32
  • 2
    It should mean the same: See What is the meaning of double pound symbol (##1) in an argument? – Werner May 22 '12 at 20:36
  • It seems I searched for the wrong word ... – Håkon Malmedal May 22 '12 at 20:42
  • 1
    @Werner In LaTeX3 it's the same, but the #1 (or ##1) sometimes apparently comes out from nothing and I think that some explanation of the feature can be useful. – egreg May 22 '12 at 20:43
10

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).

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.