On Thu, Jul 16, 2015 at 12:13 PM, Procházka Lukáš Ing. - Pontex s. r. o. < LPr@pontex.cz> wrote: > Hello, > > On Thu, 16 Jul 2015 11:03:05 +0200, luigi scarso > wrote: > > \def\GG{\ifmmode G_G\else$\GG$\fi} >> means "define the macro \GG as G_G if mmmod is true, >> else as \GG " >> It's clear that you always are in a situation where mmod is true, then >> \GG >> is replaced with G_G >> > > Well, I intended "replaced with $G_G$", > not "replaced with G_G"; > so I thought this would force/ensure math mode and thus prevent infinite > recursion. > > but as soon as you fall into "mmod not true" then you have infinite >> recursion. >> > > Also, my observation was that my original macro worked well when called > for TeX code (at various places of use: > \GG $\GG$ > \startitemize[][] > \sym{\GG} \GG > \sym{$\GG$} $\GG$ > \sym{\m{\GG}} \m{\GG} > \item End > \stopitemize > ) > but stopped working when called from Lua (?!). > > \def\GG{\ifmmode G_G\else$\GG$\fi} define a macro \GG with replacement text \ifmmode G_G\else$\GG$\fi In your use in TeX, \if is expanded: if we are in mmode then we have the expansion of G ,then _ and then G. If we are not in mmode, then we have the expansion of $ , so we enter in mmode and then we have the expansion of \GG again. In this second level of expansion, we are in mmod for sure and so we end again with G, _ and G. We cannot know what happen in other situations. For example, in \edef\x{\GG} as shown by Hans, at definition time the replacement text is expanded until possible. If at definition time we are not in mmode, this gives the endless sequence \GG ->\ifmmode G_G\else $\GG $\fi {\ifmmode: (level 1) entered on line 9} {false} {\else: \ifmmode (level 1) entered on line 9} \GG ->\ifmmode G_G\else $\GG $\fi {\ifmmode: (level 2) entered on line 9} {false} {\else: \ifmmode (level 2) entered on line 9} \GG ->\ifmmode G_G\else $\GG $\fi {\ifmmode: (level 3) entered on line 9} {false} {\else: \ifmmode (level 3) entered on line 9} \GG ->\ifmmode G_G\else $\GG $\fi .... because in this case the first $ is just accumulated, not interpretated --- i.e. doesn't trig the change of state to math mode. To avoid expansion we can say \edef\x{\noexpand\GG} which means that the replacement text of \x is \GG . Perhaps we can see better here: \def\HH{\ifmmode H_H\else$H_H$\fi} \edef\x{\HH} means, if we are not in mmode when \x is defined, \HH ->\ifmmode H_H\else $H_H$\fi {\ifmmode: (level 1) entered on line 10} {false} {\else: \ifmmode (level 1) entered on line 10} {\fi: \ifmmode (level 1) entered on line 10} {changing \x=undefined} {into \x=macro:->$H_H$} so the replacement text of \x is $H_H$ -- luigi