[Tex/LaTex] Typeset subscript material automatically in upright font shape

lyxmath-mode

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.

Best Answer

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