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

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.


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.


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.