6

I'd like to create a command which only targets the first "level" of the indexes. I'll illustrate with some examples, using \qq as the command in the first line, and the "real" LaTeX code on the line below:

d\qq{SA}
d_{\mathrm{SA}}

d\qq{SA_j}
d_{\mathrm{SA_{\mathit{j}}}}

d\qq{S_{j,k} A_j}
d_{\mathrm{S_{\mathit{j,k}} A_{\mathit{j}}}}

d\qq{S_{j,k} A_{j,k}}
d_{\mathrm{S_{\mathit{j,k}} A_{\mathit{j,k}}}}

The compiled code should look like this:

picture

I have absolutely no idea on how to de this, or if it can be done just using \newcommand.

Please help, doing this manually drives me nuts :)

AllanLRH
  • 174

3 Answers3

5

enter image description here

\documentclass{article}
\def\qq#1{_{\mathchoice
{\hbox{$\displaystyle#1$}}%
{\hbox{$\textstyle#1$}}%
{\hbox{\scriptfont1=\scriptfont0$\scriptstyle#1$}}%
{\hbox{$\scriptscriptstyle#1$}}%
}}
\usepackage{amsmath}

\begin{document}

$d\qq{SA}$
$d_{\mathrm{SA}}$


$d\qq{SA_j}$
$d_{\mathrm{SA_{\mathit{j}}}}$

$d\qq{S_{j,k} A_j}$
$d_{\mathrm{S_{\mathit{j,k}} A_{\mathit{j}}}}$

$d\qq{S_{j,k} A_{j,k}}$
$d_{\mathrm{S_{\mathit{j,k}} A_{\mathit{j,k}}}}$

\end{document}

The spacing is not exactly the same as you are subscripting a boxed S not the S directly so you lose some fine adjustments to subscript positioning.

David Carlisle
  • 757,742
  • This was exactly what I was looking for – thank you! And it's nice that you point out the details about the subscript spacing. – AllanLRH Apr 24 '13 at 12:41
  • Just noticed it's not working in fractions in inline math mode: snapshot, Code: $\phi = \arctan \left( \frac{d\qq{SA_0}}{d\qq{A_0A_j}} \right)$ – AllanLRH Jun 20 '13 at 13:43
2

The following is adapted from David Carlisle's answer, and from this answer to a related question: make _ a macro unto itself, and use the \sb macro (which retains the old behaviour of _) to define a new behaviour depending on the surrounding math style.

Code:

\documentclass{minimal}

\catcode`_=\active
\def_#1{\mathchoice
  {\sb{\mathrm{#1}}}{\sb{\mathrm{#1}}}{\sb{\mathit{#1}}}{\sb{\mathit{#1}}}%
}

\begin{document}

$d_{SA}  \;\;  d_{SA_j}  \;\;  d_{S_{j,k} A_j}  \;\;  d_{S_{j,k} A_{j,k}}$

\end{document}

Result:

Romanised first-level subscripts

You can switch this behaviour off and on as you like, using macros

\def\romansuboff{\catcode`_=8}
\def\romansubon{\catcode`_=active}

and then using \romansuboff to turn off the custom behaviour, and \romansubon to turn it back on.

2

In my opinion you're starting from a wrong premise. If you say

$d_{\mathrm{SA}_{j}}$

you'll have none of these problems. Maybe you want to use a shorthand for \mathrm:

\newcommand{\qq}[1]{\mathrm{#1}}

and type

$d_{\qq{SA}_{j}}$

Keep properly segregated different semantics.

egreg
  • 1,121,712
  • This actually seems like a good option... can't believe I didn't thought about nesting the subscripts in that way. – AllanLRH Apr 24 '13 at 12:43