\eta_i\in{}&\ifmmode\mathcal C^\infty\else$\mathcal C^\infty$\fi

Typing pdflatex in the command line and running this, I would expect it to work. align* is a math mode environment, so the \ifmmode should take the YES branch, and just produce \mathcal C^\infty. However, that is not the case. The result of the above is Missing } inserted, and if I add \show\mathcal right after the \else, the \show gets executed, meaning it's going into the NO branch as if it weren't in math mode. If I remove the &, however, everything works fine, and the result is:

enter image description here

Same goes with xelatex.

What is going on here? Why is it not in math mode after the &?

  • 1
    What's the purpose of \ifmmode inside align*?
    – egreg
    Sep 7, 2020 at 12:57
  • Try adding a \relax before \ifmmode, explanation: tex.stackexchange.com/a/82656/3929 (founf via a simple google search on latex ifmmode align
    – daleif
    Sep 7, 2020 at 12:59
  • @egreg This is just an MWE. The \ifmmode part is from a macro \Cinf that's supposed to ensure math mode. This way, when I have to say a function is smooth, I can just type \Cinf without dollars.
    – MickG
    Sep 7, 2020 at 14:43

2 Answers 2


The align environment is defined in terms of the primitive function \halign.

When TeX is processing \halign and finds & it ignores following explicit spaces and expands tokens in order to see whether \omit or \span follows. Only after finding either of them or neither, it inserts the template for the current column.

The template for an even numbered column in align is basically


where # stands for the cell content, found by scanning to the next & or row ending.

What happens in your case is that \ifmmode is expanded and TeX is not in math mode at the time, so what remains is $\mathcal C^\infty$

Now TeX applies the template, resulting in

$\displaystyle$\mathcal C^\infty$$

which places \mathcal outside of math mode. Hence the error.

A naïvely defined macro


would not solve the issue. The LaTeX kernel provides \ensuremath for the purpose:


But you should think twice (or more) before doing this as you have no advantage whatsoever in being able to type \Cinfty in text mode instead of the correctly marked up $\Cinfty$. Note that $\Cinfty$ and \Cinfty{} require the same number of keys.

  • A \Cinf like that is where I came from. I actually also noticed that putting a \relax after the & fixed the problem. I may have defined it at a time when I didn't know about \ensuremath. \Cinf\ saves one keystroke, and two shifts, not to mention that if I have \Cinf. \Cinf, \Cinf; \Cinf: I have saved two characters both with a shift, so I guess when I had to take notes it felt like a valid reason for such a macro. And now I'm used to it.
    – MickG
    Sep 7, 2020 at 14:47

I believe that you can't use the \ifmmode inside math, and this command is used to define something, the modified MWE is:




  • Thanks to daleif comment above...
    – MadyYuvi
    Sep 7, 2020 at 13:06

You must log in to answer this question.

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