7

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.

Mico
  • 506,678
Andy Grey
  • 115
  • The following is not an answer but a remark. With \text the font will change with the surrounding text, so it'll e.g. be italic inside a theorem environment. This is probably undesirable, so I would recommend using \textnormal instead. – Circumscribe Dec 18 '18 at 18:31
  • It is possible to overload _ to do this, but it might break things. Might defining \newcommand*\subtxt[1]{_{\textnormal{#1}}} and doing a search-and-replace for _\subtxt be a good alternative? – Circumscribe Dec 18 '18 at 18:38
  • see e.g. https://tex.stackexchange.com/a/228606/2388 – Ulrike Fischer Dec 18 '18 at 18:38
  • @Circumscribe thanks! I'll give that a try! – Andy Grey Dec 18 '18 at 18:40
  • @AndyGrey: make sure to include a space in \subtxt (at the end) if your document contains things like X_a without {}. – Circumscribe Dec 18 '18 at 18:42
  • Are you able and willing to compile your documents under LuaLaTeX (if you don't already do so)? – Mico Dec 18 '18 at 19:40
  • 1
    Please also confirm that really all letters in subscript positions should be rendered in upright ("roman") letters. E.g., should the letters i and j in x_i and y_j be typeset using upright letters? Or should only groups of 2 or more, or 3 or more, letters in subscript positions be typeset using roman characters? – Mico Dec 18 '18 at 19:43

4 Answers4

12

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

Circumscribe
  • 10,856
6

enter image description here

\documentclass{article}

\begin{document}
\sbox0{$$}

\scriptfont1=\scriptfont0


$X_{abc}+y_{max}$


\end{document}

Note this affects all math uses of the script size font not just subscripts.

David Carlisle
  • 757,742
  • Would that work for equation, align and other environments? – Andy Grey Dec 18 '18 at 19:22
  • @AndyGrey yes.. – David Carlisle Dec 18 '18 at 19:24
  • (Most notably affected are superscripts and the numerator/denominator of inline fractions.) – Circumscribe Dec 18 '18 at 21:00
  • @Circumscribe yes – David Carlisle Dec 18 '18 at 21:04
  • @Circumscribe also stick to a-z, don't try \alpha\beta\gamma.... – David Carlisle Dec 18 '18 at 21:19
  • 3
    What kind of sorcery is \sbox0{$$} for? Just loading the math fonts? – Werner Dec 18 '18 at 21:35
  • 1
    @Werner latex delays finalizing the math setup until the first use of math at each font size, so that you can switch math fonts. actually for basic math at \normalsize it probably makes no difference as things are pre-loaded, but it's a good habit to make sure... – David Carlisle Dec 18 '18 at 21:48
  • 1
    $x_{max}$ works\footnote{But $X_{max}$ doesn't} and what about $x_{max}$? – egreg Dec 18 '18 at 22:32
  • I couldn't get this to work consistantly. It worked in some places but not in others. I'm also using sfmath which broke it. – Andy Grey Dec 19 '18 at 08:52
  • @AndyGrey yes it's really a sketch to show an approach, because I made a one-line answer just switching the tex-primitive font setting without updating teh highr level latex font tables, there is a chance that latex switches it back (eg a font size changes) it wouldn't take a lot more code to instead declare this roman script font through the latex font declarations, but again if a package like sfmath is re-setting teh same fonts the timing might be package-specific. – David Carlisle Dec 19 '18 at 08:57
6

I suggest to use a different character for those subscripts. One way could be to use ? that's very rarely used in math mode. Another uses (maybe you can find a way to type it easily).

\documentclass{article}
\usepackage{amsmath}
\usepackage{newunicodechar} % for using ↓

% the main command
\newcommand{\uprightsubscript}[1]{_{\textnormal{#1}}}

% this sets up the use of ?
\begingroup\lccode`~=`?\lowercase{\endgroup\let~}\uprightsubscript
\AtBeginDocument{\mathcode`?="8000 }

% this sets up the use of ↓
\newunicodechar{↓}{\uprightsubscript}

\textheight=2cm % just for making a smaller picture

\begin{document}

$x?{max}$ works\footnote{Also here $x?{max}$} and again $x?{max}$?

$x↓{max}$ works\footnote{Also here $x↓{max}$} and again $x↓{max}$?

\end{document}

enter image description here

egreg
  • 1,121,712
5

Here's a LuaLaTeX-based solution. Subscripts enclosed by curly braces are typeset with upright ("roman") letters, as long as there's no space between the _ (underscore) character and the material enclosed in curly braces. If a subscript term is not enclosed in curly braces, e.g., $x_i$, it's not processed by the Lua code.

If, for some reason, you do not want to the Lua function to operate on a subscript term encased in curly braces, just make sure that there are one or more spaces between _ and the subscript term. An obvious reason for wanting to suspend operation of the Lua function would be the fact that the subscript term contains math material which should be processed in math mode.

To activate operations, issue the instruction \upsubOn. To terminate them completely, execute \upsubOff.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{amsmath,unicode-math}
\usepackage{luacode}
%% Lua-side code:
\begin{luacode}
function sub_up ( s )
  return ( s:gsub ( "_(%b{})" , "_{\\textnormal%1}" ) )
end
\end{luacode}
%% LaTeX-side code:
\newcommand\upsubOn{\directlua{luatexbase.add_to_callback( 
    "process_input_buffer" , sub_up , "subup" )}}
\newcommand\upsubOff{\directlua{luatexbase.remove_from_callback( 
    "process_input_buffer" , "subup" )}}
\AtBeginDocument{\upsubOn}  % activate the Lua function by default

\begin{document}
$X_{text goes here}$\quad $x_ {ab cd}$\quad $x_ {i_j},x_k^u$

\upsubOff
$X_{text goes here}$\quad $x_ {ab cd}$\quad $x_ {i_j},x_k^u$
\end{document}
Mico
  • 506,678