[Tex/LaTex] Balanced vertical padding in table & Why does \vphantom has width

spacingtables

I think I have a simpler & better solution to these questions on how to add vertical padding to tabular cells so they looks less crammed. However, my solution which make use of \vphantom somehow produces unwanted horizontal space. So, I am asking for how to remove the horizontal space.

\documentclass{standalone}
\newcommand{\balancedVPhantom}[1]{ % gives minimum vertical size
    \vphantom{$\vcenter{\hbox{\rule{0pt}{#1}}}$}
}
\newcommand{\newrow}{\balancedVPhantom{2em}\\ \hline}
\begin{document}
\begin{tabular}{|l|l|}
    \hline
    This row has normal space. \\ \hline
    This row has balanced increased space.\newrow
    \balancedVPhantom{2em}But why does vphantom has width? \\ \hline
\end{tabular}
\end{document}

output of the solution to balanced vertical padding in table

As you can see, the last row is shifted to the right even though I use \vphantom.

Best Answer

There are a couple of errors in your macro; the most obvious is that a % is missing at the end of the line and there is moreover a space after the opening brace, the second is in the fact that \vphantom doesn't start horizontal mode, so if you use it at the beginning of a paragraph the result will not be what's expected, because you'd get an empty line.

I propose a simpler macro:

\documentclass[border=4]{standalone}

\newcommand{\balancedVPhantom}[1]{% gives minimum vertical size
  $\mathsurround=0pt \vcenter{\hrule width0pt height #1}$\ignorespaces
}

\begin{document}
\begin{tabular}{|l|l|}
\hline
This row has normal space. \\
\hline
\balancedVPhantom{2em} This row has balanced increased space. \\
\hline
\end{tabular}
\end{document}

With \ignorespaces it doesn't matter if you have a space after the argument. The setting of \mathsurround=0pt is needed when math mode is used for any purpose not directly involved into producing a math formula. (Although forgetting it is usually not important, because the parameter is rarely set to a nonzero value.)

enter image description here

Related Question