40

Example of my final solution based on Werner's suggestion can be found at the end of this question.

I would like to make a table that looks about as this one:

Example table created in excel

I can get most of this to work with the following code:

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{table}\centering
\begin{tabular}{l c c c c}
\toprule
    & \multicolumn{2}{c}{Text1} & \multicolumn{2}{c}{Text2} \\ 
    \cmidrule{2-3} \cmidrule{4-5}
    & A & B & A & B \\
    \midrule
Foo & 1 & 2 & 3 & 4\\
\bottomrule
\end{tabular}
\caption{Foo matter}
\end{table}

\end{document}

which gives me this result:

Latex table

So far the only solution (that kind of works) that I could come up with is to add an empty column between column 3 and 4. I was however hopping that there is an easy solution that allows me to directly add some space between the two columns.

This solution seems to be pretty close to what I would like but causes some problems with the last three columns (they do not appear to be cantered). Adding

\usepackage{array}
\newcolumntype{C}{@{\extracolsep{3cm}}c@{\extracolsep{0pt}}}%

to the preamble and changing begin tabular to

\begin{tabular}{l c c C c}

gives me:

So close

That is close but not quite what I need.

I did also find a few solutions to insert a space between rows but could not transfer them to columns: Row Space 1 and others.

Solution based on answer by Werner

The solution for me is mostly what Werner suggested. I have only added values for how much the lines should be trimmed on either side. To get:

The table I wanted

I used:

\documentclass{article}

\usepackage{booktabs}
\usepackage{array}
\newcolumntype{C}{@{\extracolsep{3cm}}c@{\extracolsep{0pt}}}%

\begin{document}

\begin{table}\centering
\begin{tabular}{l c c c c}
\toprule
    & \multicolumn{2}{c}{Text1} & \multicolumn{2}{c}{Text2} \\ 
    \cmidrule(l{2pt}r{2pt}){2-3} \cmidrule(l{2pt}r{2pt}){4-5}
    & A & B & A & B \\
    \midrule
Foo & 1 & 2 & 3 & 4\\
\bottomrule
\end{tabular}
\caption{Foo matter}
\end{table}

\end{document}

7
  • I am little bit confused: You want extra space between rows but add it at the columns?
    – user31729
    May 26, 2014 at 7:00
  • @ChristianHupfer No, I do want space between columns. In english I always confuse the two. It's terrible. Sorry about that! I corrected now in the question.
    – MatoBehr
    May 26, 2014 at 7:02
  • How large should the space between the relevant columns be? 3cm is quite large!
    – user31729
    May 26, 2014 at 7:04
  • @ChristianHupfer Maybe 1em would be the best. But that will basically depend on the final usage. So I would like to understand the principal behind a solution.
    – MatoBehr
    May 26, 2014 at 7:27
  • 1
    Possible duplicate of Proper centering with cmidrule and multi- row and column
    – Cragfelt
    Dec 6, 2017 at 10:30

2 Answers 2

39

The only way to break the horizontal rule would be to insert a separate column (as you suggest), or to marginally trim the \cmidrules using the optional (..) specification:

enter image description here

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{tabular}{l c c c c}
  \toprule
  & \multicolumn{2}{c}{Text1} & \multicolumn{2}{c}{Text2} \\
  \cmidrule{2-3} \cmidrule{4-5}
  & A & B & A & B \\
  \midrule
  Foo & 1 & 2 & 3 & 4 \\
  \bottomrule
\end{tabular}

\bigskip

\begin{tabular}{l c c c c}
  \toprule
  & \multicolumn{2}{c}{Text1} & \multicolumn{2}{c}{Text2} \\
  \cmidrule(lr){2-3} \cmidrule(lr){4-5}
  & A & B & A & B \\
  \midrule
  Foo & 1 & 2 & 3 & 4 \\
  \bottomrule
\end{tabular}

\bigskip

\begin{tabular}{l c c c c c}
  \toprule
  & \multicolumn{2}{c}{Text1} & & \multicolumn{2}{c}{Text2} \\
  \cmidrule{2-3} \cmidrule{5-6}
  & A & B & & A & B \\
  \midrule
  Foo & 1 & 2 & & 3 & 4 \\
  \bottomrule
\end{tabular}

\end{document}

Adding space in the column specification using

\begin{tabular}{l c c@{\hspace{1cm}} c c}

inserts 1cm between columns 3 and 4, but would require you to correct alignment when using \multicolumn without providing the gap.

I don't think inserting an empty column is that bad. You could actually control the width of the column to provide a finely-tuned spacing of the \cmidrule separation. Something like

\begin{tabular}{l c c c@{\hspace{1cm}} c c}

Would give a gap of exactly 1cm between columns 3 and (now) 5.

2
  • 1
    Adding a blank column to small tables is no problem for smaller tables. If they however do get bigger I feel having a empty column just impairs the readability of the raw latex table even further. Also, I am wondering this approac could be used for tables created from file using pgfplotstables. But maybe there is a way to create an empty column in there somewhere.
    – MatoBehr
    May 26, 2014 at 7:33
  • 2
    I most liked your second table suggestion so I had a look at the booktabs documentation and found that it is possible to pass a value to either l and r. I have now changed the entries for the \cmidrule to \cmidrule(l{2pt}r{2pt}){2-3} \cmidrule(l{2pt}r{2pt}){4-5} which is exactly what I wanted (well, I will have to replace the {2pt} with proper values but that should be ok!
    – MatoBehr
    May 26, 2014 at 7:47
1

See this page - there is an optional argument in cmidrule already!

Proper centering with cmidrule and multi- row and column

You must log in to answer this question.

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