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 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?

share|improve this question
1  
Use \leavevmode\hphantom{0} –  Marco Daniel Apr 2 '13 at 17:57
    
See last sentences here: Function and usage of \leavevmode –  Marco Daniel Apr 2 '13 at 18:00
1  
Use \noindent before you start the tabularx. The 15pt is the regular paragraph indent that tabularx is pushed to the right. Also, you could use \mbox{}\hphantom{0}. –  Werner Apr 2 '13 at 18:16
    
@Werner Drat, that 15pt should have ring a bell... –  mafp Apr 2 '13 at 20:52

3 Answers 3

up vote 8 down vote accepted

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.

share|improve this answer

Marco has given the correct way to use \hphantom in LaTeX but there shouldn't be any need to fake alignment with phantoms in an alignment structure like a tabular.

enter image description here

\documentclass{article}

\usepackage{tabularx}

\begin{document}
\noindent
\begin{tabularx}{\linewidth}{r@{\,--\,}lp{2.8cm} X}
    1/00&2/00 & text text \\
   1/00&2/00 & text text \\
   01/00&2/00  & text text \\
   10/00&11/00 & text text
\end{tabularx}
\end{document}
share|improve this answer
    
Yes, the cooked down MWE looks like this. But in the real document there is other, quite different stuff in that column, and I would need to resort to \multicolumn and other tricks. –  mafp Apr 2 '13 at 23:19
    
@mafp well that's OK you should accept Marco's then (I see you already have:-) –  David Carlisle Apr 2 '13 at 23:21

Insert noindent ...but I do not know if it is good practice

\documentclass{article}

\usepackage{tabularx}

\begin{document}
\begin{tabularx}{\linewidth}{p{2.8cm} X}
    1/00\,--\,2/00 & text text \\
\noindent   \hphantom{0}1/00\,--\,2/00  & text text  \\ 
   01/00\,--\,2/00  & text text \\
   10/00\,--\,11/00 & text text
\end{tabularx}
\end{document}
share|improve this answer
1  
noindent tells TeX explicitly to go into horizontal mode. So nearly equal to leavevmode –  Marco Daniel Apr 2 '13 at 18:02
    
@MarcoDaniel just all I know is that it works.... and the truth is without understanding why! :) –  karathan Apr 2 '13 at 18:07

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.