% \iffalse
%<*driver>
\documentclass{l3doc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{uprightsubs.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{uprightsubs.sty}
%
% \title{The \textsf{uprightsubs} package}
% \author{Moritz R. Schäfer}
% \date{\today}
%
% \maketitle
%
% \section{Introduction}
% In physics, descriptive subscripts are conventionally set upright.
% A common way to achieve this is to use \verb|E_{\mathrm{sub}}|.
% The \textsf{uprightsubs} package provides a shorter notation
% and supports accented text via \cs{textnormal}.
%
% A leading |!| selects \cs{mathrm}, which uses the upright math alphabet.
% A leading |!!| selects \cs{textnormal}, which uses the document's normal
% text font and supports accented letters. Subscripts without either prefix
% retain their usual formatting.
%
% Load the package with \verb|\usepackage{uprightsubs}|. The shorthand becomes
% active at the start of the document.
%
% \begin{quote}
% \begin{verbatim}
%   $E_{!kin}$ % -> $E_{\mathrm{kin}}$
%   
%   $E_{!!cinétique}$ % -> $E_{\textnormal{cinétique}}$
%   
%   $E_{x}$ % -> $E_{x}$ (no change)
% \end{verbatim}
% \end{quote}
%
% \section{Acknowledgements}
% The implementation is based on Enrico Gregorio's (egreg's)
% \href{https://tex.stackexchange.com/a/742891}{answer on TeX Stack Exchange}.
% Thanks to Enrico Gregorio for the original implementation and for his
% permission to distribute the adapted code under the LaTeX Project Public
% License (LPPL).
%
% \StopEventually{}
%
% \section{Implementation}
%
%    \begin{macrocode}
%<*package>
\ProvidesExplPackage
  {uprightsubs}
  {2026/09/13}
  {0.1.0}
  {Upright~descriptive~subscripts}
%    \end{macrocode}
%
% \subsection{Subscript processing}
%
% \begin{macro}[int]{\uprightsubs_subscript:}
% Inspect explicit groups for the shorthand. Otherwise leave the input
% untouched for TeX's subscript scanner, so commands such as
% |E_\frac12| can still read their arguments. Ignore intervening spaces,
% just as TeX does when scanning a subscript.
%    \begin{macrocode}
\cs_new_protected:Nn \uprightsubs_subscript:
 {
  \peek_remove_spaces:n
   {
    \peek_catcode:NTF \c_group_begin_token
     { \uprightsubs_subscript:n }
     { \sb }
   }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\uprightsubs_subscript:n}
% Inspect the subscript for a leading |!| and dispatch to the
% formatting helper; otherwise use the ordinary subscript.
%    \begin{macrocode}
\cs_new_protected:Nn \uprightsubs_subscript:n
 {
  \str_if_eq:eeTF { \str_head:n {#1} } { ! }
   {
    \__uprightsubs_subscript_text:e { \tl_tail:n {#1} }
   }
   {
    \sb{ #1 }
   }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\__uprightsubs_subscript_text:n,\__uprightsubs_subscript_text:e}
% After removing the first |!|, a second |!| selects
% \cs{textnormal}; otherwise use \cs{mathrm}.
%    \begin{macrocode}
\cs_new_protected:Nn \__uprightsubs_subscript_text:n
 {
  \str_if_eq:eeTF { \str_head:n {#1} } { ! }
   {
    \sb{ \textnormal{ \tl_tail:n {#1} } }
   }
   {
    \sb{ \mathrm{#1} }
   }
 }
\cs_generate_variant:Nn \__uprightsubs_subscript_text:n { e }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Activation}
%
% At the start of the document, give |_| category code 12 and an active
% mathcode. In mathematics it then invokes the subscript handler; in text
% it is an ordinary character. Subscripts already tokenized in the preamble
% retain their original meaning.
%    \begin{macrocode}
\AtBeginDocument
 {
  \char_set_active_eq:nN { `_ } \uprightsubs_subscript:
  \char_set_catcode:nn { `_ } { 12 }
  \char_set_mathcode:nn { `_ } { "8000 }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\endinput
%</package>
%    \end{macrocode}
% \Finale
