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.
- 141
4 Answers
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}

- 40,848
- 3
- 64
- 125
- 31,033
-
-
1@daleif Okay. (Btw., I actually found the code in your book years ago if I'm not mistaken.
:)) – Svend Tveskæg Feb 01 '14 at 09:29 -
3
-
@SvendTveskæg: Is it possible to modify it that it show the same behavior as Micos lualatex solution? Just replacing the
|#1|by{#1}doesn't seem to work. Also using a gap likev_ {x,\max}should typeset as usual (i.e. thexitalic). – Julia Oct 15 '22 at 14:52 -
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}$
- 74,238
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}
\AtBeginDocument{\directlua{luatexbase.add_to_callback(
"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}
- 506,678
-
Would it be possible to encapsulate this into a sty file and use it in documents with pdflatex (instead of lualatex)? – student Jan 01 '19 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
\usepackagedirective compiksbke under pdfLaTeX. – Mico Jan 01 '19 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 01 '19 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 01 '19 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 01 '19 at 14:12
-
I am not quite sure, I remember that I already switched some years ago to lualatex, however I got the impression that the compilation is much slower than in pdflatex, see also https://tex.stackexchange.com/questions/261472/lualatex-vs-pdflatex-speed-in-texlive-2015 then I switched back to pdflatex. Today I had to typeset a complex document (wich already slow compilation times) with a lot of upright indices. Then I wished to have a elegant solution for now and for the future of this problem and stumbled over your nice solution. – student Jan 01 '19 at 14:33
-
At the end I wish to have a package which I could just load (working both with pdflatex or lualtex) to solve the index problem as you suggested. Out of curiosity I wanted to know how your luacode may be made accessible to pdflatex as well. – student Jan 01 '19 at 14:37
-
You you have an idea for an elegant way to handle mixed cases in your approach, for example v_{x,max}, where x should be italic and max upright? – student Nov 16 '21 at 11:31
-
@student - How about
$v_ {x,\max}$? Note the gap between_and{-- it's essential. – Mico Nov 16 '21 at 11:58
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.
- 229
-
1I think this should be posted as a comment to wipet? answer rather than a separate answer. – schtandard Jun 20 '19 at 13:26
-
I would have preferred that, but I can't comment yet, due to the reputation system. – RL-S Jun 20 '19 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 '19 at 14:22

$x_k$, where the subscript is an index, the “k” should be in italics. – egreg Jan 30 '14 at 10:49Ris the radius of a lake then one should useR_{\textup{lake}}but the counting index refers to a mathematical variable, i.e., italics – daleif Jan 30 '14 at 12:01