What is the difference between {\rm}, \mathrm {} and \text {} in LaTeX? Can they be mixed?
The primary distinction between `{\rm ...}`, `\mathrm{...}`, and `\text{...}` in LaTeX lies in their intended semantic purpose and their relationship to the surrounding text's font and formatting. `{\rm ...}` is a legacy font switch command from LaTeX 2.09, deprecated in modern LaTeX in favor of the family-specific commands like `\mathrm`. Its use sets the roman (upright) font within mathematical mode, but it does not handle spacing or font size changes intelligently when used outside of math mode, leading to inconsistent typography. The command `\mathrm{...}` is its modern, semantically named replacement, designed explicitly for use within mathematical expressions to typeset its argument in an upright roman font while preserving the mathematical context; it is intended for units, named operators like "sin" or "log," or for multi-letter variable names where an upright shape is required, and it correctly processes hyphenation and spacing for the content within its braces, but only within the confines of a mathematical environment.
In stark contrast, `\text{...}`, provided by the `amsmath` package, is fundamentally different. Its purpose is to temporarily exit mathematics mode and typeset its argument in the normal text font of the surrounding paragraph, respecting its current size, family, and weight. This makes it indispensable for incorporating short textual annotations, phrases, or punctuation within displayed equations—such as "for all x" or a conditional clause—that should match the prose of the document. Unlike `\mathrm`, which remains within the mathematical spacing and font rules, `\text` switches to text mode, meaning spaces are rendered as they would be in a paragraph, and the font will be whatever the surrounding text is (e.g., italic if inside a theorem statement). This semantic difference is critical: `\mathrm` is for mathematical symbols meant to be upright, while `\text` is for actual words or commentary within a formula.
These commands can be mixed, but doing so requires careful consideration of their nesting contexts to avoid errors or unintended formatting. Since `\text` temporarily leaves math mode, you can place `\mathrm{...}` inside `\text{...}` only if the argument of `\mathrm` is itself valid in text mode, which is typically not the case and often redundant, as the text is already in roman. Conversely, placing `\text{...}` inside `\mathrm{...}` is generally nonsensical because `\mathrm` expects math-mode content. A more common and valid mix is using `\mathrm` within the argument of `\text` when that text itself is inside a math environment and needs a specific mathematical upright font for a unit within a textual phrase, though this is a nuanced typographic choice. The key practical implication is that misuse, such as employing `{\rm` or `\mathrm` for textual commentary, breaks the semantic markup of the document, potentially interfering with search tools, accessibility software, and consistent styling across document classes. For robust, maintainable documents, the rule is straightforward: use `\mathrm` for upright mathematical identifiers exclusively within math environments, and use `\text` for any interpolated natural language or punctuation within those same formulas, while avoiding the obsolete `{\rm` construct entirely.