29

It seems to be the de facto standard to use \mathrm for upright letter notation in math mode as opposed to \text. Would it not be better to use \text in general?

One particular problem I am thinking of is when writing in a sans serif-style document (e.g. beamer). In many cases here, your text and math is set in a sans serif font (for screen readability, I guess). Using \mathrm in this case causes the argument to appear in a roman font (with serifs), whereas using \text seems to correctly pick up that the text style in the document is sans serif and display the argument in upright sans serif.

So, would it not be better to generally use \text instead of \mathrm for "upright" notation in math mode?

22
  • \text{} returns to the text mode and so it uses the current font. Also, try to type \alpha+\text{A_1}+x.
    – Sigur
    Feb 13, 2013 at 10:31
  • Both macros have their own purpose. \mathrm still contains mathematical symbols, whereas \text explicitly marks text and as such returns to text mode (which is different from math mode). In math mode you have different typesetting rules (e.g. spacing) and on top of that by using them interchangeably you don't make use of the semantic mark-up either. Bottomline: I strongly advise aginst this practice!
    – Count Zero
    Feb 13, 2013 at 10:34
  • I never use \mathrm. Upright letters only appear in operators for me, so I use \DeclareMathOperator. Otherwise it is text, so I use \text.
    – mafp
    Feb 13, 2013 at 10:35
  • 5
    @mafp: Not only operators. The upright font must also be chosen in descriptive indices, such as in $\rho_\textrm{Water}$. Descriptive indices aren't variables, as opposed to, e. g., i in $x_i$ which is clearly a variable.
    – AlexG
    Feb 13, 2013 at 11:21
  • 2
    @mafp: But \rho_{\text{Water}} won't give you upright Water in an italic context, so wouldn't \mathrm be the better choice? (Personally, I use the plain \rm ...) Feb 13, 2013 at 14:20

3 Answers 3

20

As has been said, \text is for text, and will change depending on the surrounding font. But math symbols in a document should always look the same: The meaning of a symbols also depends on the font used. So you should not use \text for mathematical symbols.

If you want an upright math font which adapts to the main document define it by using \familydefault:

\documentclass[12pt]{article}
\usepackage{amsmath}

\renewcommand\familydefault{\sfdefault} %comment to see the difference
\DeclareMathAlphabet      {\mathup}{OT1}{\familydefault}{m}{n}

\begin{document}

abc

$ a=\text{b}=\mathup{b}$

\itshape abc

$ a=\text{b}=\mathup{b}$


\sffamily abc

$ a=\text{b}=\mathup{b}$

\end{document}
3
  • Nice solution. I guess I had misunderstood \text as more of a "\mathup" solution, but the above definitely seems better. Feb 13, 2013 at 10:50
  • In this answer to a similar topic you make mention of \textrm. Shouldn't this one be preferred over \text and \mathrm after all?
    – AlexG
    Feb 13, 2013 at 11:09
  • @AlexG: None of the commands "should be preferred": It depends on the context: As shown above \text e.g. adjust itself to the current text font (it also adjust size when used e.g. in superscripts. It uses the sans serif family in the last line. \textrm switches always to the roman family. Feb 13, 2013 at 11:34
5

How complicated does it become if we write the same equation with \text instead of \mathrm?

enter image description here

\documentclass[preview,varwidth]{standalone}
\usepackage{amsmath}
\begin{document}
\preview
$\int f(x)\, \mathrm{d_a}x$

$\int f(x)\, \text{d$_\text{a}$}x$
\endpreview
\end{document}
2
  • Note: please ignore the mathematical aspect of the given expression above. Feb 13, 2013 at 11:08
  • 9
    The shape of the \text version will change in a say a theorem environment where the surrounding text is italic. Feb 13, 2013 at 11:21
4

that depends to your problem. \mathrm{...} uses always Computer Modern and \text{...} the current text font:

\documentclass{article}
\usepackage{libertine}
%% instead of libertine use the following two lines:
%\usepackage[T1]{fontenc}
%\usepackage{mathpazo}
\usepackage{amsmath}

\begin{document}
\Huge\sffamily
$\mathrm{Foo} \text{Foo}$

\end{document}

enter image description here

5
  • 3
    Unfortunately I don't have the libertine package, I guess. I'm getting this warning No file OT1fxl.fd. on input line 5.
    – Sigur
    Feb 13, 2013 at 10:44
  • use another package eg \usepackage{mathpazo}
    – user2478
    Feb 13, 2013 at 11:12
  • @Sigur You have an encoding problem. Your LaTeX found libertine.sty, that's why it is looking for OT1fxl.fd.
    – mafp
    Feb 13, 2013 at 14:49
  • 1
    \mathrm will use the font that is set to be used as roman math font. It can be changed from Computer Modern!!
    – rubenvb
    Nov 30, 2015 at 15:30
  • @rubenvb: That is obvious that it can be changed to another font.
    – user2478
    Nov 30, 2015 at 18:30

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