14

Possible Duplicate:
Subscripts for primed variables

If I have a variable which is always subscripted but only sometimes superscripted, the vertical position of the subscript varies. For instance, $s^*_i$ and $s^{}_i$ are different from $s_i$. Can I define the subscript such that it is always in the same position regardless of the presence of a superscript?

hatmatrix
  • 2,913
  • 6
  • 31
  • 41

2 Answers2

20
\usepackage{subdepth}

Example

\documentclass{article}
\usepackage{subdepth}
\begin{document}
$a_i a_i^2$
\end{document}

enter image description here

egreg
  • 1,121,712
8

I would define a macro to handle this in a consistent way. The following minimal example defines \var[<var>]{<sub>}[<sup>] that sets <var>_{<sub>}^{<sup>} (<var> and <sup> are optional). The addition of an empty <sup> ensures a consistent height for your superscripts. If your optional <sup> is something other than a "normal superscript" (let's call it "bizarre"), adding \vphantom{<bizarre sup>} before #3 should take care of the vertical alignment:

enter image description here

\documentclass{article}
\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\var}{O{s} m O{}}{%
  \ensuremath{#1_{#2}^{#3}}% add \vphantom{<bizarre sup>}
}
\begin{document}
$s_i\ s_i^*$\ \var{i}\ \var{i}[*]\ \var[a]{k}\ \var[a]{k}[*]
\end{document}​

<var> has a default of s. If <sup> contains other (more complex) elements, you could use \mathstrut. That is, with some knowledge of the input, you can optimise the output.

It would also be possible to modify the macro to allow you to use superscript notation ^{..} as needed, rather than an optional <sup> argument, but this enforce the use of math mode, rather than using \ensuremath.

xparse provides the macro definition interface.

Werner
  • 603,163