3
\documentclass[a4paper]{article}
\usepackage{amsmath,amssymb,amsfonts}
\begin{document}
\begin{align*}
\eta_i\in{}&\ifmmode\mathcal C^\infty\else$\mathcal C^\infty$\fi
\end{align*}
\end{document}

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 &?

3
  • 1
    What's the purpose of \ifmmode inside align*?
    – egreg
    Commented 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
    Commented 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
    Commented Sep 7, 2020 at 14:43

2 Answers 2

4

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

$\displaystyle#$\hfill

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

\newcommand{\Cinfty}{\ifmmode\mathcal{C}^\infty\else$\mathcal{C}^\infty$\fi}

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

\newcommand{\Cinfty}{\ensuremath{\mathcal{C}^\infty}}

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.

1
  • 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
    Commented Sep 7, 2020 at 14:47
0

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

\documentclass[a4paper]{article}
\usepackage{mathtools}
\begin{document}
\newcommand{\calC}{\relax\ifmmode\mathcal{C}^{\infty}\else%
$\mathcal{C}^{\infty}$\fi}%


\begin{align*}
\eta_i\in{}&\calC
\end{align*}

\calC

\end{document}
1
  • Thanks to daleif comment above...
    – MadyYuvi
    Commented 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 .