8

Ok, I've looked at this code too long and need some fresh eyes and ideas.

Question: Why do I get too long vertical lines on my LaTeX table?

Please note that I've not experiencing any problems with the above code on online compilers such as http://docs.latexlab.org/docs

I'm using the texi2pdf in order to compile my document on Mac OSX Snow Leopard.

Code:

\begin{table}[h]
    \begin{tabular}{|l|l|l|l|l|l|l|l|l|l|} 
    \hline
        ContainsPrize & \multicolumn{3}{|c|}{A} & \multicolumn{3}{|c|}{B} & \multicolumn{3}{|c|}{C} \\ \hline
        MyChoice      & A   & B & C & A & B   & C & A & B & C   \\ \hline
        openA         & 0   & 0 & 0 & 0 & 0.5 & 1 & 0 & 1 & 0.5 \\
        openB         & 0.5 & 0 & 1 & 0 & 0   & 0 & 1 & 0 & 0.5 \\
        openC         & 0.5 & 1 & 0 & 1 & 0.5 & 0 & 0 & 0 & 0   \\
    \hline
    \end{tabular}
    \caption{A nice caption text here...}
 \end{table}

Which generates:

table with oversized lines

Any ideas? Buggy compiler? Alternatives?

13
  • 3
    I get the same result when adding \\ after \hline.
    – egreg
    Commented Jan 24, 2012 at 15:34
  • 1
    Are you sure you the code you posted produces the image you posted? It works fine for me and does not produce those extra vertical lines. However I can reproduce your buggy image if I replace the last \hline with \hline\\ . But that seems to be what @egreg tried but he did not get the same results?? Commented Jan 24, 2012 at 15:53
  • 6
    Someone has to say it; vertical lines in tables are bad. Do not use them. It is very rarely the case that they add anything but clutter.
    – qubyte
    Commented Jan 24, 2012 at 15:56
  • 1
    @lordlarm Remove the left most | from \multicolumn{3}{|c|}{A} & \multicolumn{3}{|c|}{B} & \multicolumn{3}{|c|}{C} so that it looks like \multicolumn{3}{c|}{A} & \multicolumn{3}{c|}{B} & \multicolumn{3}{c|}{C} just for aesthetics. But other than that your code works fine.
    – azetina
    Commented Jan 24, 2012 at 16:01
  • 2
    more or less anything after that final \hline would produce the result you show, for example \ or ~ or even just \relax (which does nothing but is enough to mess up TeX's rather delicate end-of-alignment processing. Commented Jan 24, 2012 at 17:36

3 Answers 3

8

This is tricky. What you have is actually a 3-dimensional table that you want to represent as a 2-dimensional image on the page. There are 3 input values and 3³ = 27 output values.

I think what you want here is Occam's Razor...

  • Right-align first column
  • Pad inner columns (groups of 3) with extra horizontal space
  • Write 0.5 as fraction ½
  • Throw in some double-stroked rules (I know that sounds crazy, since it's a reverse-simplification)
  • Then remove as much as possible and see where that leads...

For example:

You could even play around with removing the vertical rules entirely and replacing them with vertical whitespace:

Maybe even rewrite the zeros as dashes, if the data still happens to make sense that way...

Note: I'm not sure that the latter two forms are actually clearer, even though they are less complex for the eyes to process. The reason is that, as mentioned earlier, this is inherently a 3-dimensional table with 27 values. The final two forms — which use horizontal rules only — muddy the dimensionality as they lead the eye to scan left-to-right. Compare and contrast this with the second form, in which the double-stroked rules serve to separate the three 3x3 grids from the labels above and to the left.

As much as I dislike vertical rules and double-stroked rules in general, I think the second form might actually be the clearest of the five shown here. I'd be curious to hear your thoughts.

In any event, below is the LaTeX source for the samples above.

 \documentclass{article}
 \usepackage{booktabs}
 \usepackage{nicefrac}
 \newcommand{\half}{\nicefrac{1}{2}}
 \begin{document}

 \begin{tabular}{|r||ccccc|ccccc|ccccc|}
 \hline
 ContainsPrize &&& A &&&&& B &&&&& C &&\\
 \hline
 MyChoice && A & B & C &&& A & B & C &&& A & B & C &\\
 \hline\hline
 openA &&     0 & 0 & 0 &&& 0 & \half & 1 &&& 0 & 1 & \half &\\
 openB && \half & 0 & 1 &&& 0 &     0 & 0 &&& 1 & 0 & \half &\\
 openC && \half & 1 & 0 &&& 1 & \half & 0 &&& 0 & 0 &     0 &\\
 \hline
 \end{tabular}

 \vskip 3em

 \begin{tabular}{r||ccccc|ccccc|ccccc}
 ContainsPrize &&& A &&&&& B &&&&& C &&\\
 \hline
 MyChoice && A & B & C &&& A & B & C &&& A & B & C &\\
 \hline\hline
 openA &&     0 & 0 & 0 &&& 0 & \half & 1 &&& 0 & 1 & \half &\\
 openB && \half & 0 & 1 &&& 0 &     0 & 0 &&& 1 & 0 & \half &\\
 openC && \half & 1 & 0 &&& 1 & \half & 0 &&& 0 & 0 &     0 &\\
 \end{tabular}

 \vskip 3em

 \begin{tabular}{r|ccccc|ccccc|ccccc}
 ContainsPrize &&& A &&&&& B &&&&& C &&\\
 \hline
 MyChoice && A & B & C &&& A & B & C &&& A & B & C &\\
 \hline
 openA &&     0 & 0 & 0 &&& 0 & \half & 1 &&& 0 & 1 & \half &\\
 openB && \half & 0 & 1 &&& 0 &     0 & 0 &&& 1 & 0 & \half &\\
 openC && \half & 1 & 0 &&& 1 & \half & 0 &&& 0 & 0 &     0 &\\
 \end{tabular}

 \vskip 4em

 \setlength{\tabcolsep}{.5em}
 \begin{tabular}{r c ccc c ccc c ccc}
 \toprule
 ContainsPrize &~~~~~~&~ & A & ~&~~~& ~ &     B & ~&~~~& ~ & C &     ~\\
 \midrule
 MyChoice      &&      A & B & C  &&  A &     B & C  &&  A & B &     C\\
 \toprule
 openA         &&      0 & 0 & 0  &&  0 & \half & 1  &&  0 & 1 & \half\\
 openB         &&  \half & 0 & 1  &&  0 &     0 & 0  &&  1 & 0 & \half\\
 openC         &&  \half & 1 & 0  &&  1 & \half & 0  &&  0 & 0 &     0\\
 \bottomrule
 \end{tabular}

 \vskip 4em

 \setlength{\tabcolsep}{.5em}
 \begin{tabular}{r c ccc c ccc c ccc}
 \toprule
 ContainsPrize &~~~~~~&~ & A & ~&~~~& ~ &     B & ~&~~~& ~ & C &     ~\\
 \midrule
 MyChoice      &&      A & B & C  &&  A &     B & C  &&  A & B &     C\\
 \toprule
 openA         &&     -- & -- & --  && -- & \half &  1 && -- &  1 & \half\\
 openB         &&  \half & -- &  1  && -- &    -- & -- &&  1 & -- & \half\\
 openC         &&  \half &  1 & --  &&  1 & \half & -- && -- & -- &    --\\
 \bottomrule
 \end{tabular}

 \end{document}
7

I had the exact same issue and solved it using

\hline \end{tabular}

instead of

\hline
\end{tabular}

I also had to use

\begin{tabular}{|c|c|c|c|c|c|} \hline

at the beginning of the environment.

2
  • This is the correct way to go about this. It's a simple problem and an overly complicated solution is unnecessary. When you have a newline after \hline and before \end{tabular} TeX assumes you want to add a new row to the table, but upon detecting no data, ends the table. Just remove the newline and you should be set.
    – roboguy12
    Commented Mar 18, 2013 at 3:51
  • @roboguy12 this answer does not apply in almost all contexts. a newline in the source only makes a difference if \obeylines or \verbatim or similar special construct is in force at this point. Commented Nov 9, 2015 at 7:42
1

I had a similar issue, caused by drawing, the last horizontal line, with: \hline{}.

I just solved it by dropping the brackets (i.e. by typing \hline instead of \hline{}).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .