[Tex/LaTex] Typesetting multi-letter variable names in math mode

math-mode

In many applications variable names in formulas have more than one letter. In order not to mess up kerning, one cannot simply write $nameff=123$. People seem to use different approaches: \DeclareMathOperator, \text, \mathop, \mathit, \mathrm.

Some of them seem—by name—to be intended for operators, rather than variable names, which makes me worried that there could be unintended effects in some context that I might not foresee now, when using it for variable names.

Hence my question: What is the correct way to typeset multi-letter variable names, i.e., which command is intended for this use.

Best Answer

The command intended for this purpose is \mathrm.

The \DeclareMathOperator and \operatorname commands are intended for log-like operators such as log or cos. The \text command is intended for short passages of text in math mode, such as “$x$ is even” and “otherwise” next to cases. The \mathop command does not change the font or its kerning, although you can wrap \mathop{\mathrm #1}} to get operator-like spacing. The \mathit command is usually not visually distinct from the default math font, but it could be a valid stylistic choice.

Sans-serif fonts frequently have a different meaning:

Sans-serif upright characters may be used in technical and/or physical texts in order to mark objects that cannot be confused with mathematical symbols, for example for the names of points in the description of geometrical figures, technical objects, experimental setups, and the like. Therefore sans-serif upright letters never appear in a mathematical formula of a physicist or an engineer, while mathematicians use sans-serif fonts to represent certain structures in category theory. As a partial exception, sans-serif sloping uppercase letters are allowed to indicate tensors of the second rank,but this is the only exception mentioned [....]

Since that article appeared in TUGboat more than twenty years ago, things have changed, but most documents that would use sans-serif fonts for full-word variable names also use them as the main font throughout. Therefore, it’s semantically misleading to declare that you want to use the mathematical sans-serif font for this; you really want to use the mathematical upright font. If you copied and pasted the formula into another document, you would want it to be set in that document’s mathematical upright font, to match the rest of the document.

The unicode-math package defines one other option, \mathup. If backward-compatibility with legacy 8-bit fonts isn’t a concern, I prefer that over the other alternatives because it means, “The default upright font for mathematics, which might or might not be Roman, and can be distinct from the main text font you get with \text, the operator font, or the upright individual math symbols you get with \symup.”

In practice, I would typically declare a \newcomamnd for \mathop{\mathup{#1}} or \mathop{\mathrm{#1}}. Sometimes I’ve declared it as \mathop{\text{\scshape #1}} to get small caps. That lets me declare the semantics I want, within the body of the document, not a particular appearance that might change between documents. Editing a bunch of \mathrm commands to \mathsf or \text or \operatorname is tedious and error-prone. Much better to choose a self-explanatory command and define its appearance in the preamble.