[Tex/LaTex] Non-italic (roman) subscripts in math mode

math-modesubscripts

Is there a way to set indices (subscripts) to non-italic globally? I'm aware of the case by case

X_{\text{text goes here}}

But I'm having to change that across a whole document is becoming a pain.

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).

\documentclass{article}

\usepackage{amsmath} %% <- necessary for correct scaling of subscripts

\begin{document}

\newcommand*\subtxt[1]{_{\textnormal{#1}}}
\DeclareRobustCommand\_{\ifmmode\expandafter\subtxt\else\textunderscore\fi}

\[
    X_i + X\subtxt{i} + X\_i + X\_{text goes here}
\]

\end{document}

output

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 \_.