[Tex/LaTex] Why does \mathrm{\varphi} work, but \text{\varphi} does not

amsmathgreekmath-mode

This:

\documentclass{article}

\usepackage{amsmath}

\begin{document}
$\mathrm{\varphi}$
\end{document}

Versus:

\documentclass{article}

\usepackage{amsmath}

\begin{document}
$\text{\varphi}$
\end{document}

The first works, the last does not. Why? Especially when defining macros with \text this can lead to problems. Any way around it?

Best Answer

\varphi is a math symbol and requires math mode. Since \mathrm still "provides" math mode, \varphi doesn't complain. On the other hand, \text (from amstext; loaded by amsmath) is a macro that makes the correct choice in font size when switching to text mode - super convenient when typesetting test in exponents/subscripts. However, it still switches to text mode.

If you which to use \varphi in text mode without having to switch to math mode explicitly, use something like

\newcommand{\textphi}{\ensuremath{\varphi}}

which will allow you to use \textphi in either text or math mode. The \ensuremath conditional switches to math mode only when necessary (when you're in text mode, say).

enter image description here

\documentclass{article}
\newcommand{\textphi}{\ensuremath{\varphi}}
\begin{document}
$\varphi^\varphi\ \textphi^\textphi$\ \textphi\textsuperscript{\textphi}
\end{document}