11

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?

6
  • In math mode you have \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 \vcenter is extended into text-mode so we do not need to play with this kind of things…
    – Manuel
    Jun 18, 2015 at 14:10
  • 4
    @Manuel There's \textvcenter in TeX by Topic (12.6.8): \def\textvcenter{\hbox \bgroup$\everyvbox{\everyvbox{}\aftergroup$\aftergroup\egroup}\vcenter}
    – egreg
    Jun 18, 2015 at 14:14
  • @egreg Magicians existed long before I came to this TeX world, I see.
    – Manuel
    Jun 18, 2015 at 14:20
  • Thanks for the quick responses. I found the TeX by Topic resource, but my ignorance is limiting my ability to implement @egreg's solution. Do I just use \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, 2015 at 14:31
  • After you add the definition that egreg posted you use it like the math mode one basically \textvcenter{\hbox{lipsum}}.
    – Manuel
    Jun 18, 2015 at 14:34

2 Answers 2

6

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}

enter image description here

1
  • 1
    Does the use of \xvcenter{...} in \textmidscript handle anything more robustly than $\vcenter{...}$ does?
    – Mico
    Jun 18, 2015 at 16:16
6

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

enter image description here

\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}
4
  • Is there a significant difference between your succinct answer for inline text and the one proposed by @egreg in the comment above (\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, 2015 at 14:55
  • @kgo Eijkhout's macro allows \setbox0=\textvcenter{...}
    – egreg
    Jun 18, 2015 at 15:01
  • @egreg - I couldn't have said it more succinctly. :-)
    – Mico
    Jun 18, 2015 at 15:08
  • 1
    And \setbox0 just hides text? So Eijkhout's solution just adds a hiding functionality to 'middletext'?
    – kgo
    Jun 18, 2015 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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