TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

marked as duplicate by Werner, egreg, yo', Loop Space, lockstep Apr 13 '12 at 18:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 15 down vote accepted


$a_i a_i^2$

enter image description here

share|improve this answer

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

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

<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.

share|improve this answer
Actually ^{} is sufficient (no need of \vphantom). And here \ensuremath is very questionable. – egreg Apr 12 '12 at 17:17
@egreg: I've incorporated your first suggestion. :) – Werner Apr 12 '12 at 17:25
Thanks -- elegant but I will keep this in mind as an example of xparse... – crippledlambda Apr 12 '12 at 18:29

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