Sunday, May 17, 2009

Tabular Row Spacing

We've already covered how to deal with the row height in latex, but what if you want to make only certain rows separated. When designing a table with multiple lines per cell we will use the \hline structure to make the table seem like it has multiple rows.


In this case we need to separate the rows individually, which can be done at the end of the tabular line. Consider the following code:

\begin{tabular}{rl}
{\bf First Column} & {\bf Second Column} \\\hline \\ [-1.5ex]
First Item & First Attribute \\
& Second Attribute \\
& Third Attribute \\ [1ex] \hline \\ [-1.5ex]
Second Item & First Attribute \\
& Second Attribute \\
& Third Attribute \\
& Fourth Attribute \\ [1ex] \hline \\ [-1.5ex]
Third Item & First Attribute \\
& Second Attribute \\ [1ex] \hline \\ [-1.5ex]
Fourth Item & First Attribute \\
& Second Attribute \\
& Third Attribute \\ [1ex] \hline \\ [-1.5ex]
\end{tabular}

Putting [1ex] at the end of a line creates that much extra space after the line. The idea here is to make 1ex before the \hline, and then put a completely blank line after it. the [-1.5ex] moves the line after the \hline up, making the gap less than a full line. The result is in the following image:

15 comments:

  1. Excellent! I've been looking for how to do this for ages. Thanks very much

    ReplyDelete
  2. Good trick. The only thing is that it doesn't look good with vertical lines.

    ReplyDelete
  3. Works great for me. Thanks.

    ReplyDelete
  4. Just what i needed too. It works well with vertical lines too, if you add enough '&'s along with the '\\'. Thank you!

    ReplyDelete
  5. The booktabs package is also perfect for this kind of considerations. It handles space above and below lines very well and provides the keywords toprule, midrule, and bottomrule which make your code more declarative.

    ReplyDelete
  6. very useful. thanks

    ReplyDelete
  7. horrible with vertical lines

    ReplyDelete
  8. V V useful, thanks a lot!

    ReplyDelete
  9. Hi! Have you ever noticed, has your writting style gone any better lately?

    ReplyDelete
  10. Thanks a lot :)) It's really beneficial.

    ReplyDelete
  11. \renewcommand{\arraystretch}{1.5}
    \begin{tabular}
    ....
    \end{tabular}

    this works better

    ReplyDelete
  12. Just use booktabs and be done with it.

    ReplyDelete