2

I have a table that looks somewhat like this:

Group 1        1  2
  Sub point    1  3
  Subpoint     2  3
Group 2        3  4
  Sub point    4  5
  Pie!         3  1
Group 3        4  7
  Cake?        6  8
  Imma bored   4  7

The table is generated with an R-script, but the rows are outputted in a iterative fashion. (I tried using xtable, but there is a lot more parsing and string concatenation going on than shown here.)

I want to add a small vertical spacing between the Group-rows and the previous row, so I get the following format:

Group 1        1  2
  Sub point    1  3
  Subpoint     2  3

Group 2        3  4
  Sub point    4  5
  Pie!         3  1

Group 3        4  7
  Cake?        6  8
  Imma bored   4  7

Usually, adding vertical spacing would be done with \vspace, but this adds spacing after the row. Due to my script (and me being lazy not wanting to implement a look-ahead), I am looking for a command that increases spacing to the previous row. E.g. something like

...
&  Subpoint   &  2  & 3 \\
\multicolumn{2}{l}{\addVspaceAbove{1em} Group 2 } &   3 &  4 \\
&  Sub point  &  4 &  5 \\
...

Thanks in advance!


Solution

Thanks to Qrrbrbirlbel, a solution with both before and after:

\begin{tabular}[ht]{llcc}
\multicolumn{2}{l}{Group} & Value 1 & Value 2 \\
\hline
\multicolumn{2}{l}{Group 1 }    & 1 & 2 \\
& Sub point  & 1 & 3 \\
& Subpoint   & 2 & 3 \\
\multicolumn{2}{l}{Group 2 }       & 3 & 4 \\
& Sub point  & 4 & 5 \\
& Pie!       & 3 & 1 \\
\multicolumn{2}{l}{\rule{0pt}{\dimexpr.7em+0.5em}Group 3 }       & 4 & 7 \\
& Cake?      & 6 & 8 \\
& Imma bored & 4 & 7 \\
\end{tabular}
  • 1
    You could do \\[1em] \multicolumn{2}{l}Group 2} without an \addVspaceAbove macro. Otherwise \newcommand*{\addVspaceAbove}[1]{\rule{0pt}{\dimexpr.7em+#1}} could help (untested). Can you add minimal working example (MWE)? – Qrrbrbirlbel Sep 2 '13 at 11:30
  • 2
    \\[<length>] adds the space after the row it ends. But also \noalign{\vspace{<length>}} between rows does, although it's not documented. – egreg Sep 2 '13 at 11:30
  • What is the difference between \vspace{<length>} and \\[<length>]?? – MrGumble Sep 3 '13 at 9:20
  • Unrelated note: The optional argument to the tabular should have one letter, either t, c or b, writing ht has no effect. This argument defines the 'anchor' of the tabular, if the top, center or bottom should be placed on the baseline of the text. Hence, this is not the same as the float specifiers used for a table environment (e.g. \begin{table}[ht]). – Torbjørn T. Sep 8 '13 at 11:01
  • @MrGumble -- since the solution provided in the amswer meets your requirements, you can accept it by clicking on the check mark below the vote count. that is the recommended method in this group. – barbara beeton Sep 8 '13 at 12:30
3

You can add vertical space between rows with

  • the optional argument of \\ as in \\[.5em],
  • a macro \addVspacebefore{.5em} defined as

    \newcommand*{\addVspaceabove}[1]{\rule{0pt}{\dimexpr.7em+#1}\ignorespaces}
    

    which builds a strut that extends the usual \strut, or

  • \noalign{\vspace{.5em}} which is very similar to \\[.5em].

In the example below, I also used \midrule from the booktabs package that provides nice horizontal rules.

Code

\documentclass{standalone}
\usepackage{booktabs}
\newcommand*{\addVspaceabove}[1]{\rule{0pt}{\dimexpr.7em+#1}\ignorespaces}
\begin{document}
\begin{tabular}{llcc}
    \multicolumn{2}{l}{Group}                         & Value 1 & Value 2 \\ \midrule
    \multicolumn{2}{l}{Group 1}                       &    1    &    2    \\
     & Sub point                                      &    1    &    3    \\
     & Subpoint                                       &    2    &    3    \\[.5em]
    \multicolumn{2}{l}{Group 2}                       &    3    &    4    \\
     & Sub point                                      &    4    &    5    \\
     & Pie!                                           &    3    &    1    \\
    \multicolumn{2}{l}{\addVspaceabove{.5em} Group 3} &    4    &    7    \\
     & Cake?                                          &    6    &    8    \\
     & Imma bored                                     &    4    &    7    \\\noalign{\vspace{.5em}}
    \multicolumn{2}{l}{Group 4}                       &    3    &    1    \\
     & \TeX                                           &    4    &    1    \\
     & \LaTeX                                         &    5    &    9
\end{tabular}
\end{document}

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.