# Difference between \textrm{} and \mathrm{}

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?

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

\documentclass{article}
\usepackage[T1]{fontenc}

\begin{document}
\sffamily\bfseries\itshape abc
\textrm{abc}\textrm{\textup{abc}}
\end{document}

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

\documentclass{article}
\usepackage[T1]{fontenc}

\begin{document}

$\mathit{A} \mathbf{A} \mathit{\mathbf{A}} \mathbf{\mathit{A}}$

\end{document}