142

I'm unsure of when it is preferable to use \text versus \mathrm, is there some unwritten rule about the use of these?

For example, I saw the following code in a math post:

\frac{\mathrm{lcm}(m,n)}{\mathrm{gcd}(m,n)}\quad\text{divides}\quad 
\frac{\mathrm{lcm}(m,n)}{|\langle x\rangle\cap\langle y\rangle|}\quad
\text{divides}\quad \mathrm{ord}(ab)\quad\text{divides}\quad
\mathrm{lcm}(m,n).

and there is a mix of both. Is there a general reason why the author would choose to do so?

0

5 Answers 5

120

Caution: the following discussion assumes that the package amsmath has been loaded.

In general \mathrm should be used for "symbols" and \text for, yes, text. :)

However, it's best to use operators for clusters of Roman letters that represent functions: the commands \lcm and \gcd are predefined; for "ord" there's not a predefined command, but it suffices to put in the preamble

\DeclareMathOperator{\ord}{ord}

(the command's name can be what one wants). The input before would become

\frac{\lcm(m,n)}{\gcd(m,n)}
\quad \text{divides} \quad
\frac{\lcm(m,n)}{|\langle x\rangle\cap\langle y\rangle|}
\quad \text{divides} \quad
\ord(ab)
\quad \text{divides} \quad
\lcm(m,n)

In this case \text{divides} and \mathrm{divides} might give the same result, but they are conceptually different (and can actually be printed in different ways, depending on the math fonts used). Spaces in the argument of \mathrm are ignored, for example. Moreover, \text honors the font of the surrounding environment: it will print in italics in the statement of a theorem.

Particular attention should be paid to units such as "m/s"; it's best not to do them "by hand", but employ a package like siunitx that takes care of all the subtleties, while being very flexible.

14
  • 6
    i suggest also putting any surrounding spaces within the scope of \text{ ... } -- it's easier to see that they're present when you do that. May 31, 2011 at 12:34
  • 6
    \text{divides} and \mathrm{divides} are not always the same. Using unicode-math with OpenType math fonts, the math alphabets come from the math font instead of the text fonts, by default. Anyway, it is a good answer and I voted it up -:)
    – Yan Zhou
    Jun 18, 2011 at 10:31
  • 2
    @Yan: that's a good comment, that shows more differences between the two commands.
    – egreg
    Jun 18, 2011 at 10:53
  • 1
    Maybe you’d like to mention siunitx in your answer to typeset numbers and/or units as a third kind of text (operators, units, text). Which are also often set with \mathrm{m}/\mathrm{s} instead of \si{\meter\per\second}, which I prefer becuase it’s more flexible and able to be changed globally.
    – Tobi
    Jun 18, 2011 at 11:07
  • 2
    Another issue of \mathrm is, that you "can't" use accentes, like umlauts (in principle, you can, but you will get warnings about …is invalid in math-mode) Jan 5, 2014 at 10:01
24

You should use \text if you are writing text (i.e. not math, but words) and \mathrm if you are writing math, but with Roman letters. In fact, \mathrm is of extremely limited utility because of the feature egreg described: the \DeclareMathOperator command, which covers 99% of the cases where non-variable letters appear in math (namely, as operators like lcm or gcd).

In fact, the code you are asking about is really quite bad, being overly wordy and careless about what the markup denotes. In addition to egreg's improvements, I'd advocate doing something like

\newcommand{\genby}[1]{\langle #1\rangle}
\newcommand{\card}[1]{\lvert #1\rvert}

and replacing the second quantity with

\frac{\lcm(m,n)}{\card{\genby{x} \cap \genby{y} } }

I mention this although it does not concern \mathrm because it is a symptom of the problem afflicting the entire sample: one must infer the author's intent in constructing the output to have a particular appearance rather than be told unambiguously by the way they wrote the input to have a particular meaning. The use of \mathrm is the most widespread manifestation of this issue.

The result is that the author is making it harder on themselves, in the end, by forcing their mind to envision this piece of mathematics not as math but as the collection of its constituent symbols. Consequently, it is slower to construct, lengthier to type, and also more prone to irritating inconsistencies when they forget their own conventions later.

2
  • 12
    The mathtools package has an equivalent of \DeclareMathOperator but for delimiters, \DeclarePairedDelimiter which allows a greater control over the size of the delimiters. Your examples would become \DeclarePairedDelimiter{\genby}{\langle}{\rangle} \DeclarePairedDelimiter{\card}{\lvert}{\rvert}. Dec 5, 2011 at 18:58
  • 2
    @Philippe: very nice!
    – Ryan Reich
    Dec 5, 2011 at 19:04
11

If you aren't going to be using the operator often and don't want to use \DeclareMathOperator in the preamble, you can use \operatorname instead. Here is some documentation in the Wikibook.

1
  • 2
    In particular, this works with MathJax (which I mention because you said you saw the code "in a math post"). Jan 5, 2014 at 5:17
9

I do not use \mathrm very much, but I do tend to use it for differentiable operators such as dx or dy or dz, \ldots etc.

Something like this.

\[ \int_0^{\pi} \! \sin(t)~\mathrm{d}t \]

it makes the d not a italicized letter when used in math mode. Hope that is a little helper of which one could use it for. I still want to find out more of it uses.

6
  • You can use a pair of backticks ` to enclose your inline codes. For displayed codes, highlight your codes and press the 1010 button. May 31, 2011 at 5:51
  • @xport: Thanks for the insight. Will try to incorporate these things on a regular.
    – night owl
    May 31, 2011 at 7:34
  • 4
    for the differential, a thin space \, is preferable to ~
    – egreg
    May 31, 2011 at 9:13
  • 21
    Also using sin instead of \sin is a sin. :-)
    – celtschk
    Dec 5, 2011 at 21:41
  • 1
    @celtschk: Thanks for catching that, must of overlooked. Fixed now. :)
    – night owl
    Dec 11, 2011 at 2:35
2

Using \operatorname{lcm} results in proper spacing in things like this:

a\operatorname{lcm}b

The same is true of this:

\operatorname{ord}

You must log in to answer this question.

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