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?

nickpapior
  • 17,275
Thomas Arildsen
  • 1,855
  • 1
  • 19
  • 27
  • \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 '13 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 '13 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 '13 at 10:35
  • My beamer uses sans-serif fonts for Latin letters inside math blocks. – Sigur Feb 13 '13 at 10:37
  • @ulrike-fischer's solution seems to capture the best of both modes, so to speak. I suppose a good manual practice would then be to remember to use \mathrm when you are in a serif font "environment" and to use \mathsf when you are in a sans serif font "environment"? – Thomas Arildsen Feb 13 '13 at 10:52
  • @ThomasArildsen: You can also simply redefine \mathrm when you are in a sans serif environment (as far as I remember beamer e.g. does it). See also http://tex.stackexchange.com/questions/22350/difference-between-textrm-and-mathrm/22353#22353 – Ulrike Fischer Feb 13 '13 at 10:58
  • @UlrikeFischer: That is also an easy fix. However, at least in the beamer template I am trying here, \mathrm produces serif font. So I had better redefine to be on the safe side. – Thomas Arildsen Feb 13 '13 at 11:04
  • 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 '13 at 11:21
  • @AlexG: The only problem is that \textrm won't always give you an upright font! – Hendrik Vogt Feb 13 '13 at 12:07
  • @AlexG: Isn't this actually a good example of where \text is appropriate, then? – Thomas Arildsen Feb 13 '13 at 12:52
  • @HendrikVogt: Thanks for pointing this out. Would \mathrm be the better choice? – AlexG Feb 13 '13 at 13:26
  • @ThomasArildsen: \text would use the document's main text font which may be Sans. I'd prefer the upright shape of the current math font. – AlexG Feb 13 '13 at 13:32
  • @AlexG That would be text, as in $\rho_{\text{Water}}$. Still no use for \mathrm. – mafp Feb 13 '13 at 14:04
  • @mafp: You argued against upright font in general in equations (apart from operators). – AlexG Feb 13 '13 at 14:19
  • 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 ...) – Hendrik Vogt Feb 13 '13 at 14:20
  • @AlexG ...apart from operators and text. Your example is text, even when it is in a subscript. – mafp Feb 13 '13 at 14:28
  • @HendrikVogt Very good point! You should post an answer, as it is only very implicit in the other answers. – mafp Feb 13 '13 at 14:34
  • @mafp: \text is only appropriate for insertions that are not part of an equation (as amsldoc.pdf suggests), such as "if and only if" or "where", because you get the main text font (which is not what you want in the case of descriptive indices [and operators, of course]). – AlexG Feb 13 '13 at 14:40
  • @AlexG But I would want it to be a textual item with the font of the surrounding text (modulo the limitation Hendrik pointed out), because it is a little piece of text. But I guess that is a matter of taste. – mafp Feb 13 '13 at 14:46
  • @mafp: I just don't know myself what would be "best practise" for things like \rho_{\text{Water}}. – Hendrik Vogt Feb 13 '13 at 15:14
  • @HendrikVogt: I wouldn't use \text here, because "Water" is part of the equation and should match the current math font (usually a serif font), not the surrounding text font which could be quite different. Textual insertions such as the ones mentioned in my comment above must of course use the surrounding text font and \text would be required for these. – AlexG Feb 13 '13 at 15:20
  • @AlexG: Yeah, of course. But is \mathrm the best choice then? EDIT: Or maybe \textnormal? – Hendrik Vogt Feb 13 '13 at 15:40

3 Answers3

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}
Ulrike Fischer
  • 327,261
  • Nice solution. I guess I had misunderstood \text as more of a "\mathup" solution, but the above definitely seems better. – Thomas Arildsen Feb 13 '13 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 '13 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. – Ulrike Fischer Feb 13 '13 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}
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

  • 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 '13 at 10:44
  • use another package eg \usepackage{mathpazo} –  Feb 13 '13 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 '13 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 '15 at 15:30
  • @rubenvb: That is obvious that it can be changed to another font. –  Nov 30 '15 at 18:30