For some time I used Micos solution to the problem of automatically typesetting subscripts upright based on lualatex. Now I want to switch back to pdflatex (because of compile times and the better preformat/mylatexformat support) but preserve the syntax provided by this solution.
In this this answer @Svend Tveskæg showed a solution which works also with pdflatex, but with a slightly different syntax. Now I want to modify this solution to behave like Micos solutions but works with pdflatex.
In particular it should use {
and }
as delimiters (instead of |
) and also switches back to italic if I leave a gap like $v_ {x,\max}$
.
I tried to replace the delimiters in @Svend Tveskæg like this, but it doesn't work:
\documentclass{article}
\makeatletter
\begingroup
\catcode`\_=\active
\protected\gdef_{\@ifnextchar{\subtextup\sb}
\endgroup
\def\subtextup\catcode{#1}{\sb{\textnormal{#1}}}
\AtBeginDocument{\catcode`\_=12 \mathcode`\_=32768}
\makeatother
\begin{document}
$A_1 A_p A_{p} A_ {p} $
\end{document}
Error message:
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./Upright.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2022-02-21>
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/size10.clo))
! Illegal parameter number in definition of _.
<to be read again>
1
l.8 \def\subtextup\catcode{#1
}{\sb{\textnormal{#1}}}
Here is what I used so far:
% Automatically upright subscripts with lualatex
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{uprightsub}
[2021/09/06 v1.0 upright math via lualatex]
\RequirePackage{amsmath,luacode}
%%See https://tex.stackexchange.com/questions/156641/typeset-subscript-material-automatically-in-upright-font-shape
\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")}}
\endinput
%%
%% End of file `uprightsub.sty'.
If there is another pdflatex solution which shows the desired behavior it would be also welcome.
Edit
If I compile use the lualatex solution like this
\documentclass{article}
\usepackage{uprightsub}
\begin{document}
$A_1 A_p A_{p} A_ {p} $
\end{document}
The output is
which would be the desired output for this example.