# Typeset subscript material automatically in upright font shape

As discussed for example here: Should subscripts in math mode be upright? subscripts should usually be typeset upright. Is there a possibility to this automatically e.g. inserting something to the document-header? Or, if not possible in LaTeX, is there an option in LyX? Until now I do it like this: Q _ Alt Z R test which is quite slow.

• The conclusion you draw is wrong: in $x_k$, where the subscript is an index, the “k” should be in italics. – egreg Jan 30 '14 at 10:49
• Agree with @egreg, if the index is say a counting index, then it should be in italics, if on the other hand the index is rather a name, say if R is the radius of a lake then one should use R_{\textup{lake}} but the counting index refers to a mathematical variable, i.e., italics – daleif Jan 30 '14 at 12:01
• I think for this problem it would be great just to have a package. Maybe one that supports the different syntaxe suggested in the different answers here. My own latex knowledge is to limited to make one. So if someone makes a packages or finds an already existing one, please post it in this thread. – student Jan 1 at 14:43

If you have a lot of subscripts, it might be helpful to put the following code in your preamble:

\makeatletter
\begingroup
\catcode\_=\active
\protected\gdef_{\@ifnextchar|\subtextup\sb}
\endgroup
\def\subtextup|#1|{\sb{\textup{#1}}}
\AtBeginDocument{\catcode\_=12 \mathcode\_=32768}
\makeatother


Then you can write, say, $A_|p|$ and get p in upright text mode.

MWE

\documentclass{article}

\makeatletter
\begingroup
\catcode\_=\active
\protected\gdef_{\@ifnextchar|\subtextup\sb}
\endgroup
\def\subtextup|#1|{\sb{\textup{#1}}}
\AtBeginDocument{\catcode\_=12 \mathcode\_=32768}
\makeatother

\begin{document}

$A_|p|$

\end{document}


• Not sure if that is that easy to use in lyx – daleif Feb 1 '14 at 9:27
• @daleif Okay. (Btw., I actually found the code in your book years ago if I'm not mistaken. :)) – Svend Tveskæg Feb 1 '14 at 9:29
• AFAIR Morten made it – daleif Feb 1 '14 at 9:49

Here is TeX based solution.

\def\subinrm#1{\sb{\rm#1}}
{\catcode\_=13 \global\let_=\subinrm}
\mathcode_="8000
\def\upsubscripts{\catcode\_=12 } \def\normalsubscripts{\catcode\_=8 }

\upsubscripts

$A_{lake}$, $\normalsubscripts \sum_{i=1}^\infty {1\over n}$

• Could you explain your code? – Lucademicus Feb 20 '17 at 22:19

Here's a LuaLaTeX-based solution. The solution assumes that subscript material that's encased in curly braces should be rendered in the upright font shape. The main input syntax requirement is that there mustn't be whitespace between the _ and { characters. $R_{lake}$ works, but $R_ {lake}$ does not. (Whitespace inside the curly braces is OK, though.)

For the convenience of entering single-character subscripts -- a frequently occurring situation! -- it is further assumed that single-character subscripts that are not encased in curly braces should not be rendered in the upright font shape.

Finally, if there is subscript material which is encased in curly braces but should not be rendered with upright letters, you will either need to place the material into math mode explicitly, as is done in the fourth term shown below, or leave a gap between _ and {, as mentioned earlier. I.e., one would have to write $\sum_ {i=0}^\infty$.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath,luacode}
\begin{luacode}

function up_subs ( buff )
return ( string.gsub ( buff , "_(%b{})" , "_{\\textnormal%1}" ) )
end

\end{luacode}
"process_input_buffer", up_subs, "up_subs")}}
\begin{document}

$R_t$ $R_{lake}$ $\sum_i$ $\sum_{$i{=}0$}^\infty$ % or: \sum_ {i=0}^\infty

\end{document}

• Would it be possible to encapsulate this into a sty file and use it in documents with pdflatex (instead of lualatex)? – student Jan 1 at 12:20
• @student - I’m not sure if you’re using the term encapsulation the same way I do. Sticking a code chunk that requires LuaLaTeX into a style file does not make a document that accesses a document that accesses the code chunk via a \usepackage directive compiksbke under pdfLaTeX. – Mico Jan 1 at 12:34
• yes that's clear. I should better say: Encapsulate the code and make it accessible to pdflatex such that then (if you want) you could additionally separate it in a sty file. – student Jan 1 at 14:04
• @student - I'm not aware of such forms of encapsulation being available to LaTeX documents. I.e., I don't think it's possible to have have the overall compilation of the document being handled by pdfLaTeX while "farming out" some specific operations to LuaLaTeX. – Mico Jan 1 at 14:06
• @student - Just out of curiosity: Is there a constraint that's keeping you from switching from pdfLaTeX to LuaLaTeX to compile your LaTeX documents? – Mico Jan 1 at 14:12

As posted here, the answer above by wipet does not work with script classes like scrartcl, but can easily be updated to do so:

\documentclass{scrartcl}
\usepackage{amsmath}

% typesetting indexes upright by default
\def\subinrm#1{\sb{\textnormal{#1}}}
{\catcode\_=13 \global\let_=\subinrm}
\mathcode_="8000
\def\upsubscripts{\catcode\_=12 } \def\normalsubscripts{\catcode\_=8 }
% the toggle for upright subscripts
\upsubscripts
% the toggle for italic subscripts
%\normalsubscripts

\begin{document}
$A_{lake}$, $\normalsubscripts \sum_{i=1}^\infty {1\over n}$
\end{document}


If you do not need accented characters, such as umlauts, you should use \mathrm instead of \textnormal, as David Carlisle pointed out. Then, the amsmath package is not strictly required either.

• I think this should be posted as a comment to wipet? answer rather than a separate answer. – schtandard Jun 20 at 13:26
• I would have preferred that, but I can't comment yet, due to the reputation system. – RL-S Jun 20 at 14:09
• In that case, just wait until you have enough reputation. If you are active on this site and make an effort to compose good questions (or answers, when you can), you'll be there in no time. – schtandard Jun 20 at 14:22