87

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..

7
  • 2
    \text picks up any setting outside of math mode. Issue \itshape before \input to see (also affects \textrm). Aug 28, 2013 at 14:26
  • 5
    You need to watch it here: beamer's font set up is rather odd and should not at all be taken as giving 'guidance' on normal input for LaTeX. (I can't do much about that at this point.)
    – Joseph Wright
    Aug 28, 2013 at 14:29
  • I think you have to use \mathsf Aug 28, 2013 at 14:47
  • 3
    "\mathrm does not behave as expected..." To me the expected result of \mathrm is upright roman font. Also "\textrm looks useless" : why so? Your beamer example shows that it allows you to typeset upright roman font, even when the environment (in this case, the beamer class) tries to enforce sans serif fonts. As I understand it, the wikibook mistakes \mathrm (roman font) for \textnormal (upright font).
    – T. Verron
    Aug 28, 2013 at 14:51
  • The expected behave for \mathrm was to reproduce the font type of cosine function shown above. So, have undefined functions to be enclosed in a \textnormal environment? Is this the common practice? And what about \text? Is it used for normal text?
    – Atcold
    Sep 1, 2013 at 5:15

2 Answers 2

84

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}
6
  • 1
    A marvelous answer. I've often wondered myself. Aug 28, 2013 at 15:49
  • To me, the expected outcome was to reproduce the font setting of a function (see the cosine example above). So, what shall I do to write my custom function (without defining it)? Anyway, I think I am still doubtful.. Let me rephrase my question: "How shall I write functions and how shall I write text in my formulas?". At the moment I am using \text for both functions and explanations. Am I doing it correctly?
    – Atcold
    Sep 1, 2013 at 5:25
  • Actually, for functions I saw someone uses \rm but it does not work "as expected" with beamer.
    – Atcold
    Sep 1, 2013 at 5:50
  • 4
    @Atcold \rm is a deprecated command since we introduced latex2e back in 1993 by default it isn't defined at al in latex, classes can give it compatibility (with latex 2.09) definitions if they wish. I updated the answer with various cos definitions working in beamer. Sep 1, 2013 at 9:42
  • 1
    @Atcold you can use \operatorname as above for -one-off finctions for which you don't want to define a command. normally I'd use \mathrm for things like d or e but your beamer-specific issue was that you wanted to use \mathsf so in that case I'd probably do \let\mathrm\mathsf Only use \text if you want to drop back out to the outer context so in a theorem set slanted you get slanted so usually only for natural language text not any kind of identifier in the math, which usually requires a consistent font. Sep 3, 2013 at 14:49
5

Copied from my answer at https://tex.stackexchange.com/a/394240/75284

David's answer explains it well, but what I found lacking there is code that shows distinctly what each command does versus the others. So I constructed the following example with a Times clone for text mode and default Computer Modern for math. Addionally text mode gets changed to sans-serif italic to show all the changes inside these commands:

  • \text just uses the text mode from "outside" with all the formatting.
  • \textup is similar, only upright.
  • \textnormal sets its content back to the normal document font without any other formatting.
  • \textrm keeps all the text formatting, it only resets the font family.
  • \mathrm behaves differently, I think of it as upright math mode, which is shown in my example text with the minus sign, gobbled space and square root without the compiler complaining about switching into math mode—it already is.

Depending on how much formatting you want to keep, one of these is the best choice.

\documentclass{article}
\usepackage{amsmath} % for \text
\usepackage{newtxtext} % Times-like font
\obeylines % automatic linebreaks
\begin{document}

default textmode
\sffamily\itshape
current textmode
\(\text{text}\)
\(\textup{textup}\)
\(\textnormal{textnormal}\)
\(\textrm{textrm}\)
\(\mathrm{m ath rm-\sqrt{f}}\)

\end{document}

MWE

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .