5

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.

2 Answers2

3

Because it adds a space. The % at the end of the third line corrects this.

\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|}
    \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}

enter image description here

  • Thank you. I noticed that you also remove the space between { and % on the second line, and the space in front of the third line. Even though I tested that these don't have any effect, I don't understand why they don't. Shouldn't they also add space in front of the \vphantom? – Apiwat Chantawibul Jan 02 '14 at 08:19
  • Only as a general rule that additional spaces MAY cause problems. – Przemysław Scherwentke Jan 02 '14 at 08:21
  • There goes beautiful indentation... I'm sad. – Apiwat Chantawibul Jan 02 '14 at 08:23
  • 3
    There is no need to remove the spaces at the beginning of a line, which are ignored by TeX (unless in very special situations when \obeylines is in force, which is of course not the case). – egreg Jan 02 '14 at 09:09
  • 1
    @Billiska the space before your % on the first line does add a space token and would add a space to the output if the phantom was used mid-line, it didn't as used because a space token in vertical mode is ignored, it does not start a paragraph. The indentation spaces at the next line do not form a space token at all. – David Carlisle Jan 02 '14 at 10:43
3

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

egreg
  • 1,121,712