# Why is a varwidth incorrectly vertically centered by \raisebox{-0.5\height}?

I was creating a pseudocode/JavaScript comparison table, and wanted to typeset algorithms written with the algorithm2e package vertically centered in table cells. Out-of-the box, algorithm2e can't do this, but I modded the code, and everything but one point seems to be working. That's this: algorithm2e normally places pseudocode lines in boxes out to \hsize, but in the table context I want their natural size, so I used the varwidth environment. And that has broken the vertical centering.

MWE:

\documentclass{article}

\usepackage{varwidth}

\newcommand{\verticalcenter}[1]{\raisebox{-0.5\height}{#1}}

\begin{document}

\begin{tabular}{ll}
\hline
\verticalcenter{%
\begin{varwidth}{\hsize}%
text\par
text\par
text\par
\end{varwidth}%
}&\verticalcenter{\rule{1cm}{1pt}}\\
\hline
\end{tabular}

\end{document}


This produces:

where the centerline is well above the center of the text.

I tried to dive in the varwidth code, but nothing is jumping out at me. How can I fix the varwidth's dimensions?

-

A box also has a depth, taking it into account yields:

\documentclass{article}

\usepackage{varwidth}

\newcommand{\verticalcenter}[1]{%
\raisebox{-0.5\dimexpr\height-\depth\relax}{#1}}

\begin{document}

\begin{tabular}{ll}
\hline
\verticalcenter{%
\begin{varwidth}{\hsize}%
text\par
text\par
text\par
\end{varwidth}%
}&\verticalcenter{\rule{1cm}{1pt}}\\
\hline
\end{tabular}

\end{document}


-
Aha. I had tried accounting for depth with another \raisebox (not knowing about \dimexpr), but of course I tried it in the original document, not in the MWE, where things still looked off. Thanks for the fix; I now have some unintended whitespace to hunt down. –  forritari Jul 19 '14 at 19:08