# Super(sub)scripts at half level

Is there a way to achieve that the superscript or subscript for that matter, appear at half of a variable? either at left or right..

I found this answer, but doesn't seem to apply How do I adjust the height of a superscript?

Thanks!

If you want the baseline of the superscript to be at half the height of what appears before, you can use the code:

\documentclass{article}

\newcommand{\halfscript}[2]{\settoheight{\hght}{#1}{#1}\raisebox{.5\hght}{$\scriptstyle{#2}$}}

\newlength{\hght}

\begin{document}
$\halfscript{A}{x}\halfscript{a}{x}$
\end{document}


This produces the output

On the other hand, if you want the script symbol to be centered with respect to the previous character, use the code

\documentclass{article}
\usepackage{calc}

\newcommand{\halfscript}[2]{\settoheight{\oneheight}{#1}\settoheight{\twoheight}{$\scriptstyle{#2}$}{#1}\raisebox{.5\oneheight-.5\twoheight}{$\scriptstyle{#2}$}}

\newlength{\oneheight}
\newlength{\twoheight}

\begin{document}
$\halfscript{A}{x}\halfscript{a}{x}$
\end{document}


This will produce the output

Note that the calc package is needed to subtract heights.

To get the script on the left, use the macro

\newcommand{\prehalfscript}[2]{\settoheight{\oneheight}{#1}\settoheight{\twoheight}%
{$\scriptstyle{#2}$}\raisebox{.5\oneheight-.5\twoheight}{$\scriptstyle{#2}$}{#1}}


Then the command \prehalfscript{B}{x}\prehalfscript{a}{x} will produce

• It works nice! how can the script appear on the left? – Oliver Amundsen Dec 15 '17 at 15:21
• Edited my response to include the left scripts. – Sandy G Dec 15 '17 at 15:28
• You might consider using \mathchoice if you want this to work in nested scripts: A^{\halfscript{B}{x}} renders x too large. – wchargin Dec 15 '17 at 19:14

There's \valign. Enjoy and don't ask.

\documentclass{article}

\newcommand{\halfscript}[2]{%
\mathord{\hbox{% ensure math mode
\valign{%
\vfil##\vfil\cr
\hbox{$#1$}\cr
\hbox{$\scriptstyle#2$}\cr
}%
\kern\scriptspace
}}%
}
\newcommand{\prehalfscript}[2]{%
\mathord{\hbox{% ensure math mode
\kern\scriptspace
\valign{%
\vfil##\vfil\cr
\hbox{$\scriptstyle#2$}\cr
\hbox{$#1$}\cr
}%
}}%
}

\begin{document}

X $\halfscript{A}{x} \halfscript{a}{x}$ X

X $\prehalfscript{A}{x} \prehalfscript{a}{x}$ X

\end{document}


The \valign command is the “transpose” of \halign, so we are doing an aligment of vertical boxes, centered with respect to the largest one; the reference point will be at the bottom of the box, which will be the final glue.

An improved version will center the “halfscript” with respect to the height of the nucleus, rather than height and depth, which is similar to what happens to subscripts and superscripts.

\documentclass{article}

\makeatletter
\newcommand{\halfscript}[2]{%
\mathord{%
\setbox\z@=\hbox{% ensure math mode
\valign{%
\vfil##\vfil\cr
\hbox{$#1$}%
\xdef\halfscript@dp{\the\prevdepth}%
\kern-\prevdepth
\cr
\hbox{$\scriptstyle#2$}\cr
}%
\kern\scriptspace
}%
\dp\z@=\halfscript@dp\box\z@
}%
}
\newcommand{\prehalfscript}[2]{%
\mathord{%
\setbox\z@=\hbox{% ensure math mode
\kern\scriptspace
\valign{%
\vfil##\vfil\cr
\hbox{$\scriptstyle#2$}\cr
\hbox{$#1$}%
\xdef\halfscript@dp{\the\prevdepth}%
\kern-\prevdepth
\cr
}%
}%
\dp\z@=\halfscript@dp\box\z@
}%
}
\makeatother

\begin{document}

X $\halfscript{A}{x} \halfscript{a}{x}$ X

X $\prehalfscript{A}{x} \prehalfscript{a}{x}$ X

X $\halfscript{p}{x}$ $\prehalfscript{d}{x}$

\end{document}


• May I ask, is this all official expl3 syntax? – David Carlisle Dec 15 '17 at 17:56
• @DavidCarlisle Ask Frank. – egreg Dec 15 '17 at 18:08