# Using the height of the box preceding sub- or superscript

When writing formulas regarding relativity, I often need to write tensor symbols where the ordering of the indices (in sub- and superscript) matters. That is, I want to have several sub- and superscripts with matching height. Ideally I would like to type

$T^\mu_\nu^\rho^\sigma_\eta$


and get the same result as for

$T^\mu\vphantom{T}_\nu\vphantom{T}^\rho\vphantom{T}^\sigma\vphantom{T}_\eta$


I imagine a solution where I make ^ and _ (math-)active to accomplish that, however I would need the height of the box preceding the first ^ or _. As I understand it, obtaining this (at least directly) is impossible, because TeX swallows the T before reading the ^ in my example. Is this accurate?

Is it possible to instead determine the height a sub- or superscript is lowered or elevated from within the sub-/superscript? That way I could save this height for the following sub-/superscripts.

Note: I do realize that this problem could easily be circumvented by placing a macro right before the tensor name and that that may be a cleaner TeX style. This question is more about the possibility of tweaking TeX to behave this way.

This is the job of the tensor package:

\documentclass{article}
\usepackage{tensor}

\begin{document}

$\tensor{T}{^\mu_\nu^\rho^\sigma_\eta}$

\end{document}


To make ^ and _ active throughout the document is asking for trouble. So here, I create two macros to be called in math mode when you wish to implement this special script mode:

1. \specialscriptmode is a mode declaration which redefines ^ and _ to add empty groups prior to the scripting. This cures the double-super/sub-script problem. It does have the drawback that the script's vertical locations, while all uniform, are not determined by the height of the base. If that is important to the user, then for that we use

2. \specialscript{<base>} which also will turn on the special script mode, but redefine ^ and _ to set the scripts relative to the proper height of the base. I noted that \vphantom was not sufficient for this purpose, and so had to rely on \left.\vphantom{<base>}\right. with suitable negative pre-kerning.

The macro \restorescriptmode is provided if someone needs to exit the special mode and return to the normal definitions of ^ and _. This will happen by itself upon leaving math mode, but may be needed if you need to restore definitions before leaving math mode.

\documentclass{article}
\let\svss^
\let\svsb_
\catcode^=\active %
\catcode_=\active %
\def^{{}\svss}
\def_{{}\svsb}
\newcommand\specialscript[1]{
\specialscriptmode
\def^{\mkern-7mu\left.\vphantom{#1}\right.\svss}
\def_{\mkern-7mu\left.\vphantom{#1}\right.\svsb}
#1
}
\catcode^=7 %
\catcode_=8 %
\newcommand\specialscriptmode{\catcode^=\active \catcode_=\active }
\newcommand\restorescriptmode{\catcode^=7 \catcode_=8 }
\begin{document}
$\specialscriptmode T^\mu_\nu^\rho^\sigma_\eta$
$\specialscript{T}^\mu_\nu^\rho^\sigma_\eta \quad\textrm{vs.}\quad \specialscript{\left(\frac{a}{b}\right)}^\mu_\nu^\rho^\sigma_\eta$
\end{document}


• I think I will go with something like your suggestion (with some additional tweaking to take care of the spacing drawbacks of your answer). You are right to point out that \vphantom{T} doesn't cut it (I should have checked). {\vphantom{T}} does, however (maybe simpler than \left. and \right.). – schtandard Nov 1 '17 at 19:34

You can use following six lines of macros and then you can put only \tensor prefix before tensor. No braces needed.

\def\tensor#1{\def\tensorA{{\vphantom{#1}}}#1\def\tensorD{\mkern-3mu}\tensorB}
\def\tensorB{\futurelet\next\tensorC}
\def\tensorC{\ifx\next_\tensorD\def\tensorD{}\tensorA
\else\ifx\next^\def\tensorD{\mkern-4mu}\tensorA\else\tensorE\fi\fi\tensorF}
\def\tensorE#1\tensorF{\fi\fi}
\def\tensorF#1#2{#1#2\tensorB}

$\tensor Z^\mu_\nu^\rho^\sigma_\eta_\alpha$

\bye


Judging from the other answers, I guess that the answer to my actual question is that this is indeed impossible.

For my application however, there are possibilities to deal with this, as pointed out by the other answers.

• If this is indeed your thought (I don't think this is the correct answer to your question), you can define this for every possible letter that could be a tensor: \begingroup\lccode~=T \lowercase{\endgroup\def~}{\maybetensor T} and then the character T made math active, and with \maybetensor defined in a way that works. – Manuel Nov 2 '17 at 7:53
• @Manuel: No, I think you misunderstood. I do not want to change the character T (as the tensor could have any name). My question was if it was possible to determine the height and depth of the tensor name after the fact (i.e. within the definition of ^ and _). – schtandard Nov 2 '17 at 8:23