Inspecting code of a package I saw the following two lines:




I guess that textormath and \ifmmode do basically the same: they print either their first or their second argument depending on whether math or text mode is active. But what is the difference between these two commands? And which one is preferable?

  • 2
    you have an unmatched } in your \ifmmode example, note that it doesn't take brace arguments but \ifmmode yes \else no \fi Aug 11, 2017 at 15:50
  • 3
    Neither is good; the first redefines a LaTeX kernel command (defined in t1enc.def), whereas the second may work wrongly in array (it should have \relax\ifmmode).
    – egreg
    Aug 11, 2017 at 16:18

2 Answers 2


The latex format defines \TextorMath which is a very thin wrapper around \ifmmode You don't say what defined \textormath but it is presumably similar.

\expandafter\ifx\csname protected\endcsname\relax
  \ifmmode  \expandafter\@secondoftwo
  \else     \expandafter\@firstoftwo  \fi}
\protected\expandafter\def\csname TextOrMath\space\endcsname{%
  \ifmmode  \expandafter\@secondoftwo
  \else     \expandafter\@firstoftwo  \fi}
  \expandafter\noexpand\csname TextOrMath\space\endcsname

In general it's better to use the latex form unless you know the context and it it is safe to use the TeX primitive, notably in section headings and at the start of table cells \ifmmode acts in a well defined way but not as you may expect and not usefully, (it is always false at the start of an array cell for example)

enter image description here



  \ifmmode math \else text\fi \\
  {}\ifmmode math \else text\fi \\
  \TextOrMath{text}{math} \\
  {}\TextOrMath{text}{math} \\


The (lowercase) macro \textormath is defined in babel.def and provides a wrapper to the \ifmmode macro:


As such, it will have the same problems as a direct use of \ifmmode e.g. at the beginning of a cell (see David's answer).

  • 2
    I upvoted anyway:-) hmm babel should probably at least stick a \relax in there, Aug 11, 2017 at 16:10
  • 1
    Apparently, \TextOrMath was added to LaTeX in 2005, first as part of the fixltx2e package (see ltnews.pdf, issue 17). I think it's possible that babel's \textormath command predates that. You might argue that these days, they should just \let\textormath=\TextOrMath. Aug 12, 2017 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.