On a case-by-case basis, it is possible to typeset text within math mode using \textrm{...}
or \mathrm{...}
, the latter being used predominantly for typesetting units or symbols and not pure text (since it gobbles spaces that are not escaped). \mbox{...}
is another alternative to \textrm{...}
, since it resets its contents to text mode by default. Here are some examples:
\documentclass{article}
\begin{document}
Here is a formula: $x=\exp(\log \mathrm{x})$
Here is another: $\sin^2 t+\cos^2 t = \textrm{famous identity}$
\end{document}
The above font changes do not work that well in general, since switching to a different font when using sub- and superscripts, say, does not always scale as expected. There are ways around it though. For example, using \text{...}
from the amstext
package (automatically loaded by amsmath
- see the AMS package dependencies), which switches to the appropriate font size via \mathchoice
:
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
% amsmath loads the amstext package by default
\begin{document}
Here is a formula: $x=y^{abc}$
Here it is again: $x=y^{\mbox{abc}}$
Compare that to: $x=y^{\text{abc}}$
\end{document}
First of all, the problem presents for textual subscripts, such as those used in physics to distinguish between vectors with the same name (say a force) by a subscripted label that should go in upright type. Textual subscripts are used in many other fields.
In what follows, amsmath
is assumed.
$W_{\rm total}$
is totally wrong as it relies on a deprecated command that classes don't need to define (and indeed some don't).
$W_{\mathrm{total}}$
is the correct form of the above. Limitations: spaces are gobbled and hyphens become minus signs.
$W_{\textnormal{total}}$
uses the main roman font of the document, no matter the context; the argument is typeset as text at the correct size.
$W_{\mathup{total}}$
(with Ulrike Fischer's definition) has one advantage over \mathrm
, since it uses \familydefault
, but the same limitations.
$W_{\operatorname{total}}$
is like using a sledgehammer for killing a fly. It's the same as \mathrm
, but hyphens don't become minus signs.
$W_{\text{total}}$
might seem ideal, but it changes font according to the context, so the subscript would appear in italics in a theorem statement.
Therefore, form 3 seems the most natural. Notice that braces are not really necessary, except in case 5.
To be honest, for single words \mathrm
(or \mathup
) is more efficient, as \textnormal
uses \mathchoice
and typesets four times the subscript in different sizes. However, the overhead is almost negligible with modern machines and uniformity is to be preferred to efficiency, when it doesn't slow the workflow in a significant way.
If, for some reasons, one wants that textual subscripts are typeset in upright type, but keeping the current font family, for instance because some parts of the document use sans serif type also for math (which I don't agree with), a modified version of \textnormal
can be used:
\makeatletter
\DeclareRobustCommand{\textnormalf}[1]{% f for "keep the family
\text{\usefont{\f@encoding}{\f@family}{m}{n}#1}%
}
\makeatother
Here \f@encoding
and \f@family
are the current output font encoding and font family, as stored by LaTeX at each (text) font change; with font series m
and font shape n
we're choosing upright medium type.
Of course, a more meaningful name for \textnormalf
should be chosen according to its usage and semantics.
Best Answer
It is possible to overload
_
to set all subscripts in roman type, but that seems like a bad idea since it might break something unexpected.You may instead want to consider defining a macro that produces an upright subscript. I've defined such a macro (
\subtxt
) below. Since it seems unlikely that you'll need underscores in math mode, I've also redefined\_
to expand to\subtxt
whenever it is used in math mode (and to produce an underscore otherwise, like normal).Notes:
I'm using
\textnormal
instead of\text
because the font of subscripts created with the latter command changes based on the surrounding text. You for instance probably wouldn't want all of your subscripts inside theorem environments to be in italics. See e.g. this answer for more info.I'm using
\DeclareRobustCommand
to redefine\_
because the original version of this macro is also defined like that. It isn't too important, but more information can be found here.Unless you're using underscores for some other purpose in your document, you can now do a search-and-replace to change every
_
into\_
.