What's the best way to vertically center inline text for both math and text modes?
Is there something similar to \textsuperscript for non-math mode?
What's the best way to vertically center inline text for both math and text modes?
Is there something similar to \textsuperscript for non-math mode?
Maybe something like this? (The trailing unary minus sign at the end of the first row is there just to demonstrate that the "middlescript letter" is aligned on the math-centerline.)

\documentclass{article}
\newcommand\mathmiddlescript[1]{\vcenter{\hbox{$\scriptstyle #1$}}}
\newcommand\textmiddlescript[1]{$\vcenter{\hbox{\scriptsize #1}}$}
\begin{document}
$x^{a}$ $x_{a}$ $x\mathmiddlescript{a}-$
x\textsuperscript{a} x\textsubscript{a} x\textmiddlescript{a}
\end{document}
\def\textvcenter{\hbox \bgroup$\everyvbox{\everyvbox{}\aftergroup$\aftergroup\egroup}\vcenter})? I just wonder why the TeX by Topic author chose to publish the latter.
– kgo
Jun 18 '15 at 14:55
\setbox0 just hides text? So Eijkhout's solution just adds a hiding functionality to 'middletext'?
– kgo
Jun 18 '15 at 15:08
It's a good occasion for illustrating a macro by Victor Eijkhout, that can be found in TeX by Topic, section 12.6.8; the advantage over directly using \vcenter is that \setbox<number>=\xvcenter{...} is possible.
The \midscript macro chooses a smaller size in (first level) subscripts or superscripts. It won't give good results in second level ones.
\documentclass{article}
% macro by V. Eijkhout in TeX by Topic
\makeatletter
\protected\def\xvcenter{%
\hbox\bgroup$\everyvbox{\everyvbox{}\aftergroup\m@th\aftergroup$\aftergroup\egroup}%
\vcenter
}
% The \everyvbox token list will be executed
% just after the { that follows \vcenter;
% the closing } will trigger the three \aftergroup tokens
% so $ will balance the formula started in the outer \hbox
% and \egroup will close it.
% I added to Eijkhout's macro also \m@th to neutralize the
% \mathsurround and \protected, just to be on the safe side.
\DeclareRobustCommand{\midscript}[1]{
\mathchoice{\mid@script\scriptstyle{#1}}
{\mid@script\scriptstyle{#1}}
{\mid@script\scriptscriptstyle{#1}}
{\mid@script\scriptscriptstyle{#1}}
}
\newcommand{\mid@script}[2]{
\vcenter{\hbox{$\m@th#1#2$}}
}
\DeclareRobustCommand{\textmidscript}[1]{%
\xvcenter{\hbox{\scriptsize#1}}%
}
\makeatletter
\begin{document}
$x^{a}$ $x_{a}$ $x\midscript{a}-$ $x^{x\midscript{a}-}$
x\textsuperscript{a} x\textsubscript{a} x\textmidscript{a}
\end{document}

\xvcenter{...} in \textmidscript handle anything more robustly than $\vcenter{...}$ does?
– Mico
Jun 18 '15 at 16:16
\vcenter{\hbox{..}}in text mode you have\raisebox{\dimexpr\depth-.5\totalheight+\fontdimen22\textfont2\relax}{..}(this one is from memory, I can't test it right now). Let's hope in the future\vcenteris extended into text-mode so we do not need to play with this kind of things… – Manuel Jun 18 '15 at 14:10\textvcenterin TeX by Topic (12.6.8):\def\textvcenter{\hbox \bgroup$\everyvbox{\everyvbox{}\aftergroup$\aftergroup\egroup}\vcenter}– egreg Jun 18 '15 at 14:14\textvcenter{lipsum}? Is this built in, or do I need to define this macro in the preamble? I can't seem to get it working. – kgo Jun 18 '15 at 14:31\textvcenter{\hbox{lipsum}}. – Manuel Jun 18 '15 at 14:34