TransWikia.com

What's a reliable way to detect math mode (including align*)?

TeX - LaTeX Asked by Jason Gross on November 2, 2020

I thought ifmmode detected math mode, but clearly it does not:

documentclass{article}
usepackage{amsmath}
begin{document}
[
    ifmmode math else not math fi
]
$ifmmode math else not math fi$
begin{align*}
  ifmmode math else not math fi
end{align*}
end{document}

enter image description here

How do I reliably detect math mode, including within align*?

One Answer

Adding relax, as you noted in the comment, is the solution basically. If you look at the LaTeX sources you'll see that most uses of ifmmode are preceded by relax, exactly for that reason:

defverb{relaxifmmodehboxelseleavevmodenullfi %[...]

At the beginning of a table cell (roughly what align uses) TeX is expanding macros looking for omit or noalign, and at this point it's not in math mode, and expands ifmmode which is then false.

Adding relax (or any non-expandable token) stops that scanning, and the column template is inserted, effectively starting math mode.

Correct answer by Phelype Oleinik on November 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP