6

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

\def\th{\textormath{\text@th}{\math@th}}

and

\ifmmode{\nfss@text{\textnumero}}\else\textnumero\fi}

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 – David Carlisle Aug 11 '17 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 '17 at 16:18
8

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.

\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname protected\endcsname\relax
\DeclareRobustCommand\TextOrMath{%
  \ifmmode  \expandafter\@secondoftwo
  \else     \expandafter\@firstoftwo  \fi}
\protected@edef\TextOrMath#1#2{\TextOrMath{#1}{#2}}
\else
\protected\expandafter\def\csname TextOrMath\space\endcsname{%
  \ifmmode  \expandafter\@secondoftwo
  \else     \expandafter\@firstoftwo  \fi}
\edef\TextOrMath#1#2{%
  \expandafter\noexpand\csname TextOrMath\space\endcsname
  {#1}{#2}}
\fi

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

\documentclass{article}

\begin{document}



$
\begin{array}{c}
  \ifmmode math \else text\fi \\
  {}\ifmmode math \else text\fi \\
  \TextOrMath{text}{math} \\
  {}\TextOrMath{text}{math} \\
\end{array}
$

\end{document}
5

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

\def\textormath{%
  \ifmmode
    \expandafter\@secondoftwo
  \else
    \expandafter\@firstoftwo
  \fi}

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, – David Carlisle Aug 11 '17 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. – Harald Hanche-Olsen Aug 12 '17 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.