13

I'm often apply the subscript environment in math-mode to assign e.g. materials to material properties, $\eta_{oil}$, $\lambda_{air}$, etc. Not very mathematical but hey, I'm an engineer ;-)

I'm applying the amsmath package, so this puts the subscript in italics as most other letters. I don't want that. Of course there are numerous ways to change this locally ($\eta_{\mathrm{oil}}$ feels most appropriate), but this is cumbersome.

So is there a nice, slick TeXy way to address this? Possibly applying anything like \DeclareMathSomething{}{}{}{}?

I'd like to do that globally since 99% of my usage of $_{}$ is not related to variables. And in the very few situations the subscript is an variable, I still could use $_{\mathnormal{}}$...

I'm grateful for all suggestions.

2 Answers 2

13

I'd use a different character for this. Here I use ! for upright subscripts (and define \exclam if you need ! in math mode as itself.

\documentclass{article}
\usepackage{amsmath}

\begingroup\lccode`~=`!
\lowercase{\endgroup\def~}#1{_{\mathrm{#1}}}
\mathchardef\exclam=\mathcode`!
\AtBeginDocument{\mathcode`!=\string"8000 }

\begin{document}
$a_{x}+b!{eff}=n\exclam$
\end{document}

enter image description here

When a character is given \mathcode 32768 ("8000 is the hexadecimal for it and \string is a precaution against babel doing strange things) it has a peculiar behavior. It performs normally in text mode, but when found in math mode it becomes like a macro, more precisely like an active character.

A definition of ! as active character must be present; the trick

\begingroup\lccode`~=`!
\lowercase{\endgroup\def~}#1{_{\mathrm{#1}}}

is the same as

{\catcode`!=\active \gdef!#1{_{\mathrm{#1}}}

but I prefer avoiding \gdef whenever it's possible. So when TeX finds ! in math mode it looks for an argument and transforms it into _{\mathrm{<argument>}}, which is precisely what you're after.

Finally, \mathchardef\exclam=\mathcode`! defines \exclam to produce the same that would be produced by ! in math mode if we hadn't redefined it.


If you don't plan using only ASCII letters in your subscripts, then you should change \mathrm into \textnormal, so also accented letters are allowed (and spaces are respected).

5
  • Could you explain what the single commands do?
    – Florian
    Commented Feb 17, 2015 at 22:15
  • Wouldn't it be better to use \textup instead of \mathrm, then national chars are allowed, for example R_{\textup{sø}} in Danish might stand for the radius of a (circular) lake. ø is not allowed in \mathrm.
    – daleif
    Commented Feb 18, 2015 at 9:50
  • @daleif Perhaps, it depends on what the OP is after.
    – egreg
    Commented Feb 18, 2015 at 9:51
  • Thank you very much, this is a beautiful way to handle that issue. Again, great help, thank you so much.
    – nici2go
    Commented Feb 18, 2015 at 9:52
  • @egreg, it is more for the archives. Such that others coming by this answer don't get nasty surprises.
    – daleif
    Commented Feb 18, 2015 at 10:02
1

I usually use something like the following:

\newcommand{\mstr}[1]   {\ensuremath{\mathit{#1}}}
\newcommand{\subs}[2]   {#1\mstr{_#2}}

Of course, you could e.g. change \mathit to \mathrm. Then use it like

\subs{x}{y}

You must log in to answer this question.

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