1

Or "When would an expression like ###1 be useful?"

In macro programming, it's a well-known rule that you need to double your parameter identifiers with each successive layer:

\def\insult#1#2{%
  #1 says #2 is a quack.%
  \def\response##1##2{%
    #2 refutes #1's claim, saying, ``#1 is a ##1 ##2''.
    \def\response####1{#1 says,
      ``I'm not a ##1 ##2, but a simple ####1.''
      (#1 leaves.)}%
    Rock on, #2.}}

Why must each layer of definitions double the number of #s necessary? The only reason I can think of is to allow 'variadic parameter insertion', but this fails on the first case:

\def\use#1#2#3#4#5#6#7#8#9{I want ##1.}
\use{3}cbdefghi

obviously does not expand to I want b.

So why is this doubling necessary?

Sean Allred
  • 27,421
  • I'm confused: how else would you deal with say \def\foo#1{\expandafter\def\csname my#1\endcsname##1{#1:##1} and so on? – Joseph Wright Aug 01 '15 at 20:28
  • @JosephWright My question only manifests itself one level deeper: Why can't I say \def\foo#1{\def#1##1{\def##1####1{#1:##1:####1}}}? What's the reasoning stopping me from using just three # instead of four? I'm not saying that different 'scopes' of macro arguments is bad – they're certainly a crucial part of how TeX is used – this is a question about why it was implemented as it is. – Sean Allred Aug 01 '15 at 20:30
  • 1
    Ah, you are specifically wondering about the need for powers of two :-) – Joseph Wright Aug 01 '15 at 20:33
  • @JosephWright Yes :) I've read the answer before somewhere, but I can't put my finger on it. – Sean Allred Aug 01 '15 at 20:34
  • This is surely a duplicate, please search better. However, since at every stage ## is reduced to #, you of course need powers of two. – egreg Aug 01 '15 at 20:38
  • @egreg I did try searching :( But that's the point of the duplicate process: to link different wordings of the same question to a canonical answer. Duplicates aren't always bad. – Sean Allred Aug 01 '15 at 20:41
  • @SeanAllred Is the reason I gave for #, ##, ####, ######## unclear? – egreg Aug 01 '15 at 20:42
  • @egreg It's not unclear, but it does seem arbitrary. – Sean Allred Aug 01 '15 at 20:42
  • @SeanAllred Not at all. Your definition of \use makes no sense, sorry. – egreg Aug 01 '15 at 20:44
  • it's a duplicate but surely if ## in a replacement text produces # then it is inevitable that you need #### to put ## in a nested replacement text? – David Carlisle Aug 01 '15 at 20:50
  • @DavidCarlisle That's the key information, then. I didn't realize that ## was itself an 'escape' for # macro definition in that sense. – Sean Allred Aug 01 '15 at 20:51
  • 2
    @SeanAllred that's all it is: there is nothing special about macro parameters with doubled hash, ##1 is nothing more than ## which produces # and 1 which produces 1, it is only when (if) that replacement text is evaluated than the resulting #1 is seen as a macro parameter reference. – David Carlisle Aug 01 '15 at 20:57
  • @DavidCarlisle If a duplicate can't be found (and I'm still looking for one with this information – http://tex.stackexchange.com/a/71633/17423 has the information, but not as the main point), then that sounds like a simple, clear, and sufficient answer. – Sean Allred Aug 01 '15 at 21:07

0 Answers0