Write 'text' **correctly** in equations
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}
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.
\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}
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}