# Center a \parbox or \minipage vertically

I'm not sure if I understand it correctly, but with pos=c either a parbox or a minipage should vertically align the baseline of the center line with the baseline of the text outside? However, what actually happens is that the text within the parbox or minipage is lower than the text outside.

\documentclass{article}
\begin{document}

llll\parbox[c]{2em}{llll}llll

llll\begin{minipage}[c]{2em}llll\end{minipage}llll

\end{document}


https://www.latex4technics.com/?note=zzvouv

• Vertically centering stuff relies on the \vcenter primitive (for math mode) which doesn't align at baselines. Try with a longer text (to get some line breaking). – Phelype Oleinik Aug 4 '20 at 21:19
• @PhelypeOleinik It doesn't align at baselines? This is what I am reading: en.wikibooks.org/wiki/LaTeX/Boxes#parbox,_minipage,_and_pbox and it says The \parbox is placed so the baseline of that chosen line of text is aligned to the baseline of the box that the \parbox is glued to. And it even gives a picture showing that: upload.wikimedia.org/wikipedia/commons/thumb/b/ba/… Problem is it doesn't come with source code and I can't reproduce its result. – Cyker Aug 4 '20 at 21:22
• Wikibooks :-) That's true for t and b alignments, which use \vtop and \vbox internally, which do align at baselines. See this example (minipage is a \parbox in disguise, so the effect is the same). In that picture the alignment looks to be on the baseline due to the size of the text. Remove consequat from my example and it will also appear to be on the baseline. – Phelype Oleinik Aug 4 '20 at 21:27
• @Cyker the description of the position argument is completely wrong at that wikibooks page, it happens... – David Carlisle Aug 4 '20 at 22:07

The c option (which is the default for both \parbox and minipage) doesn't center with respect to the baseline, but with respect to the formula axis, which is where fraction lines are, a bit above the baseline.

This is somewhat masked when the \parbox has more than one line, but becomes evident when it has just one line.

Let's do an experiment.

\documentclass{article}

\newsavebox{\testbox}

\begin{document}

 % to activate math

\sbox{\testbox}{\parbox{2em}{ll}}

Height: \the\ht\testbox

Depth: \the\dp\testbox

Formula axis: \the\fontdimen22\textfont2

\end{document}


This will produce

Indeed, 5.97223pt minus 2.5pt is 3.47223pt and 0.97221pt plus 2.5pt is 3.47221. The difference in the fifth decimal digit is negligible and results from the roundings made by TeX to achieve machine independence.

You can get centering with respect to the baseline by lowering the resulting box by the height of the formula axis:

\documentclass{article}

\makeatletter
\newcommand{\cparbox}[2]{%
\check@mathfonts
\raisebox{-\fontdimen22\textfont2}{\parbox{#1}{#2}}%
}
\makeatother

\begin{document}

ll\parbox{2em}{ll}ll

ll\cparbox{2em}{ll}ll

\end{document}


• The \cparbox in the last example centers the entire box. As you can see, the texts themselves are not aligned (as if you use \mbox instead). Do you think it's possible to vertical align the baseline of text inside the box to the baseline outside the box? Ironically, despite the completely wrong description in the wikibook, this description is exactly what I'm looking for... – Cyker Aug 5 '20 at 18:05
• I mean, \cparbox aligns inside middle point with outside baseline, while what I'm looking for is aligning inside baseline with outside baseline. Specifically when there are an odd number of lines in the box, it should align the baseline of the middle line (not the middle of the entire box) with the outside baseline. When there are an even number of lines, adding a 0.5\baselineskip offset is acceptable. – Cyker Aug 5 '20 at 18:10