Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

A table with the centered rule appearing well above the center of the pseudocode

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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}

Result

share|improve this answer
    
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 at 19:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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