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.
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}
-
-
1@daleif Okay. (Btw., I actually found the code in your book years ago if I'm not mistaken.
:)
) Feb 1, 2014 at 9:29 -
3
-
-
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}$
-
2
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}
-
Would it be possible to encapsulate this into a sty file and use it in documents with pdflatex (instead of lualatex)?– studentJan 1, 2019 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.– MicoJan 1, 2019 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.– studentJan 1, 2019 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.– MicoJan 1, 2019 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?– MicoJan 1, 2019 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.
-
1I think this should be posted as a comment to wipet? answer rather than a separate answer. Jun 20, 2019 at 13:26
-
I would have preferred that, but I can't comment yet, due to the reputation system.– RL-SJun 20, 2019 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. Jun 20, 2019 at 14:22
$x_k$
, where the subscript is an index, the “k” should be in italics.R
is the radius of a lake then one should useR_{\textup{lake}}
but the counting index refers to a mathematical variable, i.e., italics