Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I often use shortcuts defined via \newcommand. However sometimes it causes a "double subscript" problem if a shortcut contains a subscript and I try to add one more subscript when using it.

I am aware of all kinds of workarounds, as illustrated in the example below. But what is the most robust way to define such shortcuts?

Example of the code:

% define some shortcuts

\def\cmpto{\quad\textrm{compare to}\quad}


% this produces a "double subscript" error
%\[  \xa_i   \]

This is a workaround, which produces a good result
\[    \xa{}_i \cmpto x_{ai}    \]

But still not ideal: watch the position of the superscript
\[    \xa{}_i^2 \cmpto x_{ai}^2    \]

The \verb+{x_b}+ below produces no error, but does not look good 
(watch the baseline of the subscripts)
\[    \xb_i \cmpto x_{bi}    \]

Also superscript does not look good
\[    \xb^2 \cmpto x_b^2    \]


enter image description here

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

This works so long as you put always subscripts next to the newly defined commands:





$\xa_i x_{ai}\xa^2 x_a^2$

It might be extended to support \xa^2_i, but it doesn't seem a good idea to add complications.

The command \newsubcommand defines an abbreviation where the first argument is the command to define, the second is the main letter and the third is the fixed subscript.

The macro \xa defined in the example will look whether the following character is _ and, if so, it will merge the subscripts.

share|improve this answer
Thanks, it works. The only modification I would suggest is \newcommand#1{\ensuremath#2\sc@sub{#3}} –  amorua Nov 1 '11 at 19:46
You won't have that from me. :) I've started a campaign agaist the abuse of \ensuremath. :) Seriously: you gain nothing from it, in these cases. –  egreg Nov 1 '11 at 20:20
Could you explain your statement a bit more? I thnk typing \xa instead of \(\xa\) saves my time and improves readability. –  amorua Nov 1 '11 at 20:38
It doesn't improve readability, in my opinion. It just saves you some characters, but at the expense of clarity: math is math, text is text. For instance, my editor colors math differently: this helps readability. –  egreg Nov 1 '11 at 20:44
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.