[Tex/LaTex] \phantom inside tabularx

horizontal alignmentindentationtabularx

I tried to improve the alignment in the following table using a \hphantom:

\documentclass{article}

\usepackage{tabularx}

\begin{document}
\begin{tabularx}{\linewidth}{p{2.8cm} X}
    1/00\,--\,2/00 & text text \\
   \hphantom{0}1/00\,--\,2/00 & text text \\
   01/00\,--\,2/00  & text text \\
   10/00\,--\,11/00 & text text
\end{tabularx}
\end{document}

but somehow this adds a lineskip in the cell containing the \hphantom:
sample output

Being fully aware that the error is on my side, and not in the tabularx package, my two questions are:

  1. Can I make the \hphantom work somehow inside the the tabularx?

  2. Even without the offending row containing the \hphantom I get an Overfull \hbox (15.0pt too wide), which should not be there. What can I do to get rid of this warning?

Best Answer

Your row is defined as a \parbox. The \parbox always starts in vertical mode. This can be shown by the following example.

\documentclass{article}
\begin{document}
\parbox{8cm}{\ifvmode vmode \else hmode \fi}
\end{document}

That means a simple next box will continue the previous vertical list without switching modes. You need to start the paragraph explicitly in horizontal mode to use a box e.g. \hphantom. This is explained in the previous answer Function and usage of \leavevmode. More information are given in the TeX Book:

Simply saying \hbox{...} won’t work, since that box will just continue the previous vertical list without switching modes. You need to start the paragraph explicitly, and the straightforward way to do that is to say \indent\hbox{...}. But suppose you want to define a macro that expands to an hbox, where this macro is to be used in the midst of a paragraph as well as at the beginning; then you don’t want to force users to type \indent before calling your macro at the beginning of a paragraph, nor do you want to say \indent in the macro itself (since that might insert unwanted indentations). One solution to this more general problem is to say ‘\␣\unskip\hbox{...}’, since \␣ makes the mode horizontal while \unskip removes the unwanted space. Plain TEX provides a \leavevmode macro, which solves this problem in what is probably the most efficient way: \leavevmode is an abbreviation for ‘\unhbox\voidbox’, where \voidbox is a permanently empty box register.

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\parbox{8cm}{\ifvmode vmode \else hmode \fi}

\noindent\begin{tabularx}{\linewidth}{p{2.8cm} X}
    1/00\,--\,2/00 & text text \\
   \leavevmode\hphantom{0}1/00\,--\,2/00 & text text \\
   01/00\,--\,2/00  & text text \\
   10/00\,--\,11/00 & text text
\end{tabularx}
\end{document}

As mentioned by Werner: Use \noindent before you start the tabularx. The 15pt is the regular paragraph indent that tabularx is pushed to the right. This avoids the 15pt overfull \hbox.