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:

17 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
  13. Hi...!
    Very nice post...
    Good personality is a important in human life ayurveda is a best way to Increase

    Your Height
    and make a good looks.

    ReplyDelete
  14. Hi, I recently broke up with bf 5 week ago and i missed him so much, he said he just doesnt have feelings for on anymore but the day we broke up , he kissed me. And i think he is leaving me cause he thinks i have high requirement and that he doesn’t deserve me.
    He is very good at controlling his emotions and just for me he will not get back with me cause he think i can do better. But he didn’t seem to understand that i love him only.
    I understand that I have no chance for get him back in normal way, and I decided to ask for help. I order love spell from website http://magical-rituals.com , I was waiting 3 months, and my lover back to me.

    ReplyDelete