# Is there a preference of when to use \text and \mathrm?

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
\mathrm{lcm}(m,n).


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

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)}
\frac{\lcm(m,n)}{|\langle x\rangle\cap\langle y\rangle|}
\ord(ab)
\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.

• 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. – barbara beeton May 31 '11 at 12:34
• \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 '11 at 10:31
• @Yan: that's a good comment, that shows more differences between the two commands. – egreg Jun 18 '11 at 10:53
• 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 '11 at 11:07
• 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) – Henri Menke Jan 5 '14 at 10:01

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

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

• 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}. – Philippe Goutet Dec 5 '11 at 18:58
• @Philippe: very nice! – Ryan Reich Dec 5 '11 at 19:04

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.

• In particular, this works with MathJax (which I mention because you said you saw the code "in a math post"). – Paul Elfish Jan 5 '14 at 5:17

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.

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

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

a\operatorname{lcm}b


The same is true of this:

\operatorname{ord}
`