6

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}$: original

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

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?

3
  • can't you just replace _ by ^ and \sb by \sp ? Commented Jul 19, 2017 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
    Commented Jul 19, 2017 at 8:53
  • 1
    Correct, I only use descriptive superscripts. Commented Jul 19, 2017 at 9:52

3 Answers 3

7

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}

enter image description here

4
  • 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
    Commented Jul 19, 2017 at 8:56
  • @egreg, thanks a lot for your solution. It works very well for me. Commented Jul 19, 2017 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. Commented Jul 19, 2017 at 9:58
  • 2
    @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
    Commented Jul 19, 2017 at 10:50
4

I wrote the package altsubsup to implement the solution given by egreg as bracket forms of _ and ^. See:

\documentclass{article}

\usepackage{altsubsup}

\begin{document}

$ x_[roman]^{italic} \quad x_{italic}^[roman] \quad
  x_[roman]^[roman]  \quad x_{italic}^{italic}  $

\end{document}

code with altsubsup package

Bracket forms are displayed using \text, but this can be customized.

3

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.

enter image description here

% !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}
\AtBeginDocument{\directlua{luatexbase.add_to_callback (
  "process_input_buffer", make_roman , "make_roman" )}}

\begin{document}
$x_{abc}^ {de}$ 
\end{document}
0

You must log in to answer this question.

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