[Tex/LaTex] Write ‘text’ **correctly** in equations

equationsmath-mode

Although many have attempted to explain the difference among \mathrm, \textrm, \textnormal and \text (and others?), I found them misleading.

See for example the LaTeX's Wiki \mathrm explanation
enter image description here

Why is it wrong? Let's try all of them within an article and a beamer and you'll see the difference and, therefore, error.

equation.tex

\begin{eqnarray*}
  \int_1^9\! \cos{x} \,\mathrm{d}x & & \textrm{this is textrm}\
  \sum_1^9 y                       & & \textsf{this is textsf}\
  \prod_1^9 z                      & & \textnormal{this is textnormal}\
  \bigcup_1^9 w                    & & \text{and this is just text}
\end{eqnarray*}

article.tex

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\begin{document}
  \input {equation}
\end{document}

enter image description here

And until here everything looks fine…

beamer.tex

\documentclass{beamer}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\begin{document}
  \begin{frame}
    \input{equation}
  \end{frame}
\end{document}

enter image description here

And here we can clearly see that \mathrm does not behave as expected, i.e. does not provide a function aspect to the d. Does anyone know what am I supposed to use here instead? Just \text?? o.O

Moreover, I cannot see any difference between \textnormal and \text.

Finally, \textrm looks useless..

Best Answer

You do not say what you expect \mathrm to do however it switches to the roman font font specified by the math version in use, which is what your images show.

You will see the difference between \text (which switches to the current text font) and \textnormal (which switches to the document default font) if you test it at a point where the current text font is not the default.

\math.... commands are math mode commands which switch the font used for letters but the content is processed in math mode. \text... commands are text mode commands which process their content in text mode, they are also allowed in math but process the commands in an hbox (by default) or if amsmath is loaded they use \text internally so that they switch to smaller sizes in subscripts etc, however the setting is still in an hbox, so that the content is processed as text rather than math.

\documentclass{article}

\usepackage{amsmath}
\begin{document}


abcdef
$\text{abcdef} + \textrm{abcdef} + \textnormal{abcdef} + \mathrm{abcdef}$

{\sffamily
abcdef
$\text{abcdef} + \textrm{abcdef} + \textnormal{abcdef} + \mathrm{abcdef}$
}

\end{document}

enter image description here


When defining operators you should really use \DeclareMathOperator and not directly use font commands at all, then it works as you wish in beamer, giving sans serif. If you have existing expressions using \mathrm and you want \mathrm to behave like \mathsf in beamer` you can redefine it as shown in the second expression below.

enter image description here

\documentclass{beamer}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\DeclareMathOperator\mycos{cos}

\begin{document}
  \begin{frame}
 $
    \cos 1 
    \mathop{\mathrm{cos}} 2
    \mycos 3
    \operatorname{cos}4
    \mathop{\mathsf{cos}}
$

\let\mathrm\mathsf

 $
    \cos 1 
    \mathop{\mathrm{cos}} 2
    \mycos 3
    \operatorname{cos}4
    \mathop{\mathsf{cos}}
$

  \end{frame}
\end{document}