Hari's Corner

Humour, comics, tech, law, software, reviews, essays, articles and HOWTOs intermingled with random philosophy now and then

Peculiar LaTeX issue resolved

Filed under: Software and Technology by Hari
Posted on Mon, Oct 31, 2005 at 11:41 IST (last updated: Wed, Oct 29, 2008 @ 22:12 IST)

There was a peculiar LaTeX problem that I encountered recently with the tabular environment. Controlling the appearance of vertical lines between columns can be a real problem. It was particularly baffling me because although there is a \cline command to draw horizontal lines spanning between a few columns, there was no corresponding command to draw a vertical line that spans only a few rows. By default, when you create a tabular, you can draw vertical lines between columns, but only if they span the entire length of the table.

You can see what I mean, by looking at this screenshot.

LaTeX tabular environment As you can see, drawing such a table is not straightforward. It's easy to draw those horizontal lines, but not at all possible to draw vertical lines which don't span the entire table. There was a workaround which I found after asking for help at CQF.info. The solution was to use the \multicolumn command.

Here is the code which produces that effect:
\begin{tabular}{lll}
& X & Y \\
\cline{2-3}
\multicolumn{1}{l|}{Number of bulbs used} & \multicolumn{1}{l|}{100} & \multicolumn{1}{l|}{100} \\
\cline{2-3}
\multicolumn{1}{l|}{Average life (hrs)} & \multicolumn{1}{l|}{1300} & \multicolumn{1}{l|}{1248} \\
\cline{2-3}
\multicolumn{1}{l|}{Standard deviation (hrs)} & \multicolumn{1}{l|}{82} & \multicolumn{1}{l|}{93} \\
\cline{2-3}
\end{tabular}
It's fair to say that LaTeX is not particularly strong when handling tabular material and so you have a lot of additional packages which provide environments such as supertabular, tabularx, longtable and so on. However I am glad that I could resolve this particular issue, although the code involved is much longer since you have to use the \multicolumn command repeatedly. A better solution would definitely be appreciated.

4 comment(s)

  1. It isn't necessary to do all of that. The instructions in the \begin{tabular} command are overwritten for individual cells by subsequent \multicolumn commands. Therefore, the \multicolumn commands are only required in the top row to remove the vertical lines between columns in that row. The following code produces the same result:

    \begin{tabular}{l|l|l|}
    \multicolumn{1}{l}{} & \multicolumn{1}{l}{X} & \multicolumn{1}{l}{Y} \\
    \cline{2-3}
    Number of bulbs used & 100 & 100 \\
    \cline{2-3}
    Average life (hrs) & 1300 & 1248 \\
    \cline{2-3}
    Standard deviation (hrs) & 82 & 93 \\
    \cline{2-3}
    \end{tabular}

    Comment by al2gd (visitor) on Sun, Apr 12, 2009 @ 00:13 IST #
  2. Thanks. I think I got that solution from an online forum by asking the question.

    Comment by Hari (blog owner) on Sun, Apr 12, 2009 @ 20:35 IST #
  3. Thanks, that really helped me.

    Comment by Nirmalya Bandyopadhyay (visitor) on Wed, Mar 10, 2010 @ 00:33 IST #
  4. Great! Thanks!

    Comment by Adria (visitor) on Sat, Mar 10, 2012 @ 12:58 IST #

Comments closed

The blog owner has closed further commenting on this entry.