[texhax] recursive \defs

Paul Isambert zappathustra at free.fr
Thu Aug 16 13:21:53 CEST 2012


"Neal H. Walfield" <neal at walfield.org> a écrit:
> 
> Hi,
> 
> I have a large \def that takes some parameters.  I want to define a
> convenience macro, which I use from my outer definition, and which
> also takes parameters.  Consider:
> 
>   \documentclass{article}
>   
>   \begin{document}
>   
>   \def\foo#1{%
>     \def\bar#1{%
>       xxx #1 yyy%
>     }%
>     \bar{aaa #1 bbb}%
>   }
>   
>   \foo{!}
>   
>   \end{document}
> 
> When I compile this, I get:
> 
>   ! Use of \bar doesn't match its definition.
>   \foo #1->\def \bar #1{xxx #1 yyy}\bar {
>                                          aaa #1 bbb}
>   l.12 \foo{!}
>             
> This is because the #1 in the \bar definition is expanded to the value
> of the argument passed to \foo.  How do I escape the #1 in bar's
> definition?  What are good approaches to do this type of thing?
 
In the body of a definition (its replacement text), to denote a #, you
should double it: ##. So, in your case the definition is:

  \def\foo#1{%
    \def\bar##1{%
      xxx ##1 yyy%
      }%
    \bar{aaa #1 bbb}%
  }

If \bar iself defined another macro with parameter text, the latter
should use four #'s per argument, and so on.

Best,
Paul



More information about the texhax mailing list