# Redefine ^ to produce roman superscripts and subscripts

I need to set subscripts and superscripts automatically in mathematical equations in roman font style for a book publication. I do not want to apply \mathrm{...} to each subscript or superscript separately.

Fortunately I found a solution to globally set subscripts in roman font style in Redefine underscore to produce roman subscript. I made a slight adaptation in order to have all indexes in sequence in roman font style:

\catcode_=\active
\newcommand_[1]{\ensuremath{\sb{\mathrm{#1}}}}

Original typeset $x_{abc}^{de}$:

Roman subscripts with code above $x_{abc}^{de}$:

I can, however, not figure out how to apply this code to superscripts. Does anyone have a solution so set both the subscript and superscript font style to roman?

• can't you just replace _ by ^ and \sb by \sp ? – David Carlisle Jul 19 '17 at 7:47
• I assume you never raise to the power of a variable, and instead only use descriptive superscripts in your field? Because in $x \mathrm{e}^{\mathrm{x}}$ the two xs are different entities, while in $x \mathrm{e}^x$ they're not. – Chris H Jul 19 '17 at 8:53
• Correct, I only use descriptive superscripts. – christiankral Jul 19 '17 at 9:52

I fail to see the rationale for this, however here it is:

\documentclass{article}

\newcommand{\upsub}[1]{\sb{\mathrm{#1}}}
\newcommand{\upsup}[1]{\sp{\mathrm{#1}}}

\begingroup\lccode~=_\lowercase{\endgroup\let~\upsub}
\begingroup\lccode~=^\lowercase{\endgroup\let~\upsup}

\AtBeginDocument{%
\catcode_=12 \catcode^=12
\mathcode_="8000
\mathcode`^="8000
}

\begin{document}

$x_{abc}^{de}$

\end{document}

• I really hope this is because it's a book about a field that only uses descriptive text superscripts rather than powers. I've come across this but can;t recall what uses it. – Chris H Jul 19 '17 at 8:56
• @egreg, thanks a lot for your solution. It works very well for me. – christiankral Jul 19 '17 at 9:54
• The (German) publisher insists on roman subscripts and superscripts. So the only choice I have is to either manually re-format (some older) LaTeX code use this trick to globally change the format. – christiankral Jul 19 '17 at 9:58
• @christiankral If the publisher tells me that $x_{n}$ must have the “n” in upright type, I tell them they don't know about mathematics. Of course, this depends on what the subscripts/superscripts contain. – egreg Jul 19 '17 at 10:50

Here's a LuaLaTeX-based solution. It doesn't change the catcodes of _ and ^. Instead, it sets up a Lua function and assigns it to the process_input_buffer callback; that way, all instances of _{...} and ^{...} are converted to _{\mathrm{...}} and ^{\mathrm{...}}, respectively, before LaTeX even starts its usual processing.

Whitespace between _/^ and the material enclosed in curly braces is OK. However, the presence of the curly braces is significant: the Lua function will not modify the appearance of y^x and x_n, say.

% !TeX program = lualatex
\documentclass{article}
\usepackage{amsmath,luacode}

\begin{luacode}
function make_roman ( s )
s = string.gsub ( s , "_%s-(%b{})",  "_{\\mathrm%1}" )
s = string.gsub ( s , "%^%s-(%b{})", "^{\\mathrm%1}" )
return s
end
\end{luacode}
$x_{abc}^ {de}$