146

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?

doncherry
  • 54,637
yunone
  • 3,259

5 Answers5

123

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.

egreg
  • 1,121,712
  • 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. – barbara beeton May 31 '11 at 12:34
  • 7
    \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
  • 2
    @Yan: that's a good comment, that shows more differences between the two commands. – egreg Jun 18 '11 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 '11 at 11:07
  • Furthermore, I prefer \text{ divides } to \quad \text{divides} \quad. The former gives it a more "text-like" spacing. – kahen Feb 28 '13 at 22:48
  • 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) – Henri Menke Jan 05 '14 at 10:01
  • I have been thinking for some time about the best practices wen it comes to notations like Aut(G), Hom(V,W), Matₙ(ℂ) etc. Mathematically, they are not really “operators”, so should I use \DeclareMathOperator\Aut{Aut} or \newcommand\Aut{\mathrm{Aut}}? – Gaussler May 22 '16 at 19:15
  • @Gaussler They are operators just like “log” and “sin”. – egreg May 22 '16 at 19:34
  • @egreg As mentioned, I agree on the LaTeX side, but not on the mathematical side. How about the identity map when denoted by upright “id”? Operator or \mathrm? – Gaussler May 22 '16 at 19:36
  • @Gaussler It's debatable for the identity map, for technical reasons, because it's usually used by itself. But I see no problem with "Hom” or “Aut”, just like “ker”. – egreg May 22 '16 at 19:38
  • If one uses unicode-math is \symup acceptable for math mode text, as in text sub/superscripts? – LaTeXereXeTaL Nov 23 '20 at 01:06
  • @LaTeXereXeTaL No: you want, say, “eff” (for “efficient”) to have the ligature. – egreg Nov 23 '20 at 08:31
  • So are units considered "symbols" that should use mathrm instead of text? (Assuming units packages aren't available) – endolith Feb 16 '21 at 16:25
  • 1
    @endolith Yes, they’re symbols. – egreg Feb 16 '21 at 17:02
  • Define "symbols" here. Is this in a programmatic sense, as in a variable name, or in a typographical sense, as in special characters? – starbeamrainbowlabs Nov 24 '23 at 18:14
  • @starbeamrainbowlabs In mathematical sense – egreg Nov 24 '23 at 20:27
  • @egreg Define mathematical sense. – starbeamrainbowlabs Nov 25 '23 at 21:53
  • @starbeamrainbowlabs Why should I? Have I got any obligation towards you? You're being rude, do you know? Ask the BIPM, which says that the units are symbols. You know, mathematics, not programming. – egreg Nov 25 '23 at 22:31
  • @egreg I apologise if I come off as rude. But I really don't understand what was meant by Symbols here. – starbeamrainbowlabs Nov 25 '23 at 23:25
  • @starbeamrainbowlabs To egreg: Just that you know: viewing from the sidewalk, you seem as being a bit more rude than SBRBL. This is in addition to being wrong… (First, BIPM has zero authority in a mathematical context. Second, ME authoritatively shows that SI units are not symbols “in mathematical sense” — this is the M.Shubin’s article in т.4,кол.1133–1135.) Probably you mean that they behave as “special characters in an alphabet” — as Chinese tried to do in Chinese computer encodings (so now we are forced to live with the corresponding “compatibility characters” of Unicode…). – Ilya Zakharevich Nov 27 '23 at 06:12
  • @IlyaZakharevich So, you decide who's authoritative? Maybe I'm wrong according to Shubin, but I'm not according to BIPM, which is the authority in this field. – egreg Nov 27 '23 at 09:01
  • Hmm?! Can you point out at least one confirmation that BIMP is an authority on what is the meaning of the word “symbol” in mathematics? And: it is not me: it is the editors of the Mathematical Encyclopedia who decided on who's authoritative… – Ilya Zakharevich Nov 27 '23 at 10:10
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.

Ryan Reich
  • 37,958
  • 13
    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 05 '11 at 18:58
  • 2
    @Philippe: very nice! – Ryan Reich Dec 05 '11 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.

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.

night owl
  • 7,593
  • You can use a pair of backticks ` to enclose your inline codes. For displayed codes, highlight your codes and press the 1010 button. – Display Name May 31 '11 at 05:51
  • @xport: Thanks for the insight. Will try to incorporate these things on a regular. – night owl May 31 '11 at 07:34
  • 4
    for the differential, a thin space \, is preferable to ~ – egreg May 31 '11 at 09:13
  • 23
    Also using sin instead of \sin is a sin. :-) – celtschk Dec 05 '11 at 21:41
  • 1
    @celtschk: Thanks for catching that, must of overlooked. Fixed now. :) – night owl Dec 11 '11 at 02:35
  • On a related note: I used \text{} for defining a differential operator command and getting straight ds. The problem was that, within a theorem environment, the behaviour would get overridden and I would get an italicized d. Using \mathrm{} instead of \text{} (or \textrm{}) to define my differential command solved the problem. – Daneel Olivaw Feb 20 '21 at 16:59
2

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

a\operatorname{lcm}b

The same is true of this:

\operatorname{ord}
Sigur
  • 37,330