How can one split a macro call and its argument in a conditional? Why doesn't \ifnum 1=1\textbf\else\textit\fi{Foo} work? Why it yields error “Too many }'s”?

  • 3
    I don't exactly understand the question, but you need a couple of \expandafters there, as in \ifnum1=1\relax\expandafter\textbf\else\expandafter\textit\fi{Foo}.
    – campa
    Aug 20, 2015 at 15:46
  • The argument of \textbf is \else and for \textit is \fi. Also add a space or \relax after \ifnum1=1, otherwise \textbf is already expanded, before the comparison in \ifnum is complete. Aug 20, 2015 at 15:52
  • @campa Can you write an answer? Aug 20, 2015 at 15:53
  • @HeikoOberdiek I was kind of waiting for some context, but maybe there's nothing more to say...
    – campa
    Aug 20, 2015 at 15:55

1 Answer 1


As Heiko pointed out in his comment, \textbf is eating \else as argument, and \textit takes \fi. You thus need \expandafter in order to tell TeX to wait and finish the if-statement before expanding \textbf and \textit. You should then use



\ifnum1=1 \expandafter\textbf\else\expandafter\textit\fi{Foo}

Notice either \relaxor the space after the \ifnum statement, see e.g. Chapter 13.8 of TeX by Topic. (I'm pretty sure the issue has been already discussed somewhere here but I can't find it...)

  • 1
    Notwithstanding that comparing 1 to 1 is somehat pointless: \csname text\ifnum1=1 bf\else it\fi\endcsname{Foo} might do the trick without the need of using \expandafter.
    – user82850
    Aug 24, 2015 at 18:08
  • @UlrichDiez Cool. And obvious, when you think about it. (Maybe an \expandafter before \csname could be safer in some situations?) I agree that 1=1 is weird, but I guess the OP was just playing with this kind of constructs and reduced the problem to its root.
    – campa
    Aug 25, 2015 at 6:53
  • 1
    TeX will in any case expand expandable tokens when gathering the tokens for \csname until finding a matching \endcsname. Thus an \expandafterright before \csname to ensure expansion of the probably expandable token right behind \csname is not necessary. With \def\bar{foo} the sequence \csname foo\endcsname yields the same as \csname\bar\endcsname, i.e. the token \foo. You can also do \expandafter\csname\bar\endcsname if you like - expanding the first \expandafteryields: \csname foo\endcsname. The final result will be the same, the token \foo.
    – user82850
    Aug 26, 2015 at 20:21
  • Thx. I thought I remembered a situation where I needed an \expandafter but it was probably something else.
    – campa
    Aug 27, 2015 at 7:47
  • Perhaps it was something like \expandafter\let\csname foo\expandafter\endcsname\csname bar\endcsname in order to obtain \let\foo\bar .
    – user82850
    Aug 27, 2015 at 10:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .