Can you explain me what is the difference between `\textrm{}`

and `\mathrm{}`

when they are used in math mode. In what context one is preferred over the other?

# [Tex/LaTex] Difference between \textrm{} and \mathrm{}

math-mode

#### Related Solutions

From the name, `\mathbin`

modifies the spacing around something so that it adheres to that of a binary *operator*, while `\mathrel`

modifies the spacing to denote that of a binary *relation*. Here is an elementary approach at showcasing the difference:

```
\documentclass{article}
\begin{document}
\begin{tabular}{clc}
\multicolumn{3}{c}{Relations} \\[5pt]
\LaTeX & Typeset & width \\ \hline
\verb|$x=x$| & $x=x$ & \setbox0=\hbox{$x=x$} \the\wd0 \\
\verb|$x\mathbin{=}x$| & $x\mathbin{=}x$ & \setbox0=\hbox{$x\mathbin{=}x$} \the\wd0 \\
\verb|$x\mathrel{=}x$| & $x\mathrel{=}x$ & \setbox0=\hbox{$x\mathrel{=}x$} \the\wd0 \\[10pt]
\multicolumn{3}{c}{Binary operators} \\[5pt]
\LaTeX & Typeset & width \\ \hline
\verb|$x+x$| & $x+x$ & \setbox0=\hbox{$x+x$} \the\wd0 \\
\verb|$x\mathbin{+}x$| & $x\mathbin{+}x$ & \setbox0=\hbox{$x\mathbin{+}x$} \the\wd0 \\
\verb|$x\mathrel{+}x$| & $x\mathrel{+}x$ & \setbox0=\hbox{$x\mathrel{+}x$} \the\wd0
\end{tabular}
\end{document}
```

Note the spacing around `+`

matching that of `\mathbin`

, while the spacing around `=`

matches that of `\mathrel`

.

From the TeXBook (**Chapter 17: More about Math**, p 154-):

Every math character is given an identifying code number between 0 and 4095, obtained by adding 256 times the family number to the position number. This is easily expressed in hexadecimal notation, using one hexadecimal digit for the family and two for the character; for example,

`\hex{24A}`

stands for character`\hex{4A}`

in family 2. Each character is also assigned to one of eight classes, numbered 0 to 7, as follows:Class 0: Ordinary (eg.,

`/`

)

Class 1: Large operator (eg.,`\sum`

)

Class 2: Binary operation (eg.,`+`

)

Class 3: Relation (eg.,`=`

)

Class 4: Opening (eg.,`(`

)

Class 5: Closing (eg.,`)`

)

Class 6: Punctuation (eg.,`,`

)

Class 7: Variable family (eg.,`x`

)Classes 0 to 6 tell what "part of speech" the character belongs to, in math-printing language; class 7 is a special case [...]. The class number is multiplied by 4096 and added to the character number, and this is the same as making it the leading digit of a four-digit hexadecimal number.

...

TeX associates classes with subformulas as well as with individual characters. Thus, for example, you can treat a complex construction as if it were a binary operation or a relation, etc., if you want to. The commands`\mathord`

,`\mathop`

,`\mathbin`

,`\mathrel`

,`\mathopen`

,`\mathclose`

, and`\mathpunct`

are used for this purpose; each of them is followed either by a single character or by a subformula in braces. For example,`\mathopen\mathchar"1234`

is equivalent to`\mathchar"4234`

, because`\mathopen`

forces class 4 (opening). In the formula`$G\mathbin:H$`

, the colon is treated as a binary operation.

...

There's also an eighth classification,`\mathinner`

, which is not normally used for individual symbols; fractions and`\left...\right`

constructions are treated as "inner" subformulas, which means that they will be surrounded by additional space in certain circumstances. All other subformulas are generally treated as ordinary symbols, whether they are formed by`\overline`

or`\hbox`

or`\vcenter`

or by simply being enclosed in braces. Thus,`\mathord`

isn't really a necessary part of the TeX language; instead of typing`$1\mathord,234$`

you can get the same effect from`$1{,}234$`

.

There are many differences. The main one is in the fact that `\mathrm{xyz}`

behaves like an ordinary letter, while `\operatorname{xyz}`

behaves like function names such as `\sin`

. Here's an illustration

```
$\sin x + \sin(x+y) + a\sin z$
$\mathrm{sin} x + \mathrm{sin}(x+y) + a\mathrm{sin}z$
```

where it's clear that the second line is typeset wrong. Even if your "operator" requires parentheses after it, it should be `\operatorname`

, as the third summand shows, where a thin space separates the coefficient from the operator.

Another subtle difference is in how some characters are interpreted in `\mathrm`

and in `\operatorname`

. Suppose you have an operator to be called "pre-norm", with a hyphen. Here's the example

```
$\operatorname{pre-norm}(\mathbf{v})$
$\mathrm{pre-norm}(\mathbf{v})$
```

and now it's clear what is to be used. Indeed `\operatorname`

(and the same holds for macros defined with `\DeclareMathOperator`

) treats punctuation symbols in a special way; `\mathrm`

, instead, treats them as math symbols.

## Best Answer

`\textrm`

(and the corresponding switch`\rmfamily`

) changes an aspect of the font – in this case the family. If possible other aspects – like the series (bold or not) or the shape (italic) – are preserved.`\text...`

-commands can be nested and their "aspect changes" are combined:`\mathrm`

and the other math alphabets don't change only an aspect of a font. They switch to specific fonts. They make it possible to print symbols in special fonts. It is quite possible to setup things so that e.g.`\mathrm`

use times,`\mathbf`

is a bold palatino,`\mathit`

a slanted Arial etc. You can also confuse everyone by defining`\mathbf`

to be light itshape palatino and`\mathit`

a bold Arial. You can define your own special math fonts e.g.`\mathvector`

or`\mathmatrix`

or`\mathfancy`

. As all these commands switch to dedicated font they can't be nested. The last command (the inner one) always wins: