8

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.

3
  • 8
    The conclusion you draw is wrong: in $x_k$, where the subscript is an index, the “k” should be in italics.
    – egreg
    Commented Jan 30, 2014 at 10:49
  • 6
    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
    Commented Jan 30, 2014 at 12:01
  • 1
    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
    Commented Jan 1, 2019 at 14:43

4 Answers 4

8

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}

output

5
  • Not sure if that is that easy to use in lyx
    – daleif
    Commented Feb 1, 2014 at 9:27
  • 1
    @daleif Okay. (Btw., I actually found the code in your book years ago if I'm not mistaken. :)) Commented Feb 1, 2014 at 9:29
  • 3
    AFAIR Morten made it
    – daleif
    Commented Feb 1, 2014 at 9:49
  • @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 like v_ {x,\max} should typeset as usual (i.e. the x italic).
    – Julia
    Commented Oct 15, 2022 at 14:52
  • @SvendTveskæg I posted a follow up question here
    – Julia
    Commented Oct 15, 2022 at 15:32
6

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}$
1
  • 3
    Could you explain your code? Commented Feb 20, 2017 at 22:19
5

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

enter image description here

% !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}
9
  • Would it be possible to encapsulate this into a sty file and use it in documents with pdflatex (instead of lualatex)?
    – student
    Commented Jan 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.
    – Mico
    Commented Jan 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.
    – student
    Commented Jan 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.
    – Mico
    Commented Jan 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?
    – Mico
    Commented Jan 1, 2019 at 14:12
1

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.

3
  • 1
    I think this should be posted as a comment to wipet? answer rather than a separate answer.
    – schtandard
    Commented Jun 20, 2019 at 13:26
  • I would have preferred that, but I can't comment yet, due to the reputation system.
    – RL-S
    Commented Jun 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.
    – schtandard
    Commented Jun 20, 2019 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .