[texhax] recursive \defs

Heiko Oberdiek heiko.oberdiek at googlemail.com
Thu Aug 16 13:16:33 CEST 2012


On Thu, Aug 16, 2012 at 01:07:23PM +0200, Neal H. Walfield wrote:

> 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:
> 
>   \def\foo#1{%
>     \def\bar#1{%
>       xxx #1 yyy%
>     }%
>     \bar{aaa #1 bbb}%
>   }
>   
>   \foo{!}

>   ! Use of \bar doesn't match its definition.
>   \foo #1->\def \bar #1{xxx #1 yyy}\bar {
>                                          aaa #1 bbb}

You have to double the # in the nested definition:

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

Otherwise #1 is replaced by the outside argument:

\foo{!} -> \def\bar!{xxx ! yyy}\bar{aaa ! bbb}

Yours sincerely
  Heiko Oberdiek


More information about the texhax mailing list