Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 51 down vote accepted

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.

share|improve this answer
    
That makes sense, thanks egreg. –  yunone May 31 '11 at 3:15
4  
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
1  
\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
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
1  
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 at 10:01
show 2 more comments

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.

share|improve this answer
3  
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
add comment

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.

share|improve this answer
    
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
1  
for the differential, a thin space \, is preferable to ~ –  egreg May 31 '11 at 9:13
    
@egreg: Hey Thanks, I will look into that. –  night owl May 31 '11 at 23:47
4  
Also using sin instead of \sin is a sin. :-) –  celtschk Dec 5 '11 at 21:41
show 1 more comment

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.

share|improve this answer
    
In particular, this works with MathJax (which I mention because you said you saw the code "in a math post"). –  Paul Elfish Jan 5 at 5:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.