0

I have a problem with the way tabularx (or is it booktabs?) is handling vertical spacing between horizontal lines and the cell content / background color.

MWE:

\documentclass[paper=a4,fontsize=12pt]{scrartcl}

\usepackage[usenames,svgnames,table]{xcolor}
\usepackage{booktabs}
\usepackage{tabularx}

\renewcommand{\arraystretch}{1.2}
\newcolumntype{Z}{>{\raggedleft\arraybackslash}X}

\begin{document}

\begin{table}[h!]
    \caption{This table has a layout problem.}
    \centering
    \scriptsize
    \begin{tabularx}{\textwidth}{p{2.5cm}ZZZZZ}
        \toprule
        & \multicolumn{5}{l}{\textbf{Heading Top}} \\
        \cline{2-6}
        \textbf{Heading Left} & Heading Sub 1 & Heading Sub 2 & Heading Sub 3 & Heading Sub 4 & Total \\
        \midrule
        \rowcolor{gray!10}Amount & 10 & 10 & 10 & 10 & 40 \\
        \rowcolor{gray!10}\% in Total & 25,00\,\% & 25,00\,\% & 25,00\,\% & 25,00\,\% & 100,00\,\% \\
        \bottomrule
    \end{tabularx}
\end{table}

\end{document}

Output: enter image description here

I want to:

  1. Have equal vertical spacing between \toprule, \midrule and \cline.
  2. Have no spacing between the background color (\rowcolor) and the horizontal lines.

I have been looking through numerous suggestions on the web on how to adjust vertical spacing with no luck, especially as no one seems to encounter this particular problems.

1

The problem comes from one of the advantages brought by booktabs: it introduces some vertical padding around rules (\aboverulesep and \belowrulesep). So I set them to 0pt and replaced them with a minimal vertical padding at the top and bottom of cells, defined by the cellspacepackage. This works with standard l,r, c column types, prefixed with the letter S (or C if you use siunitx). I changed the last column type to Sl, and made it the same width as the first column with the eqparbox package:

\documentclass[paper=a4,fontsize=12pt]{scrartcl}

\usepackage[svgnames,table]{xcolor}
\usepackage{booktabs}
\usepackage{tabularx}
\renewcommand{\arraystretch}{1.2}
\newcolumntype{Z}{>{\raggedleft\arraybackslash}X}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\usepackage{eqparbox} \usepackage{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}

\begin{document}

\begin{table}[h!]
    \caption{This table has a layout problem.}
    \centering
    \scriptsize
    \begin{tabularx}{\textwidth}{Sl *{4}{Z}Sr}
        \toprule
        & \multicolumn{5}{Sl}{\textbf{Heading Top}} \\
        \cmidrule(l){2-6}
        \eqmakebox[H][l]{\textbf{Heading Left}} & Heading Sub 1 & Heading Sub 2 & Heading Sub 3 & Heading Sub 4 & \eqmakebox[H][r]{Total} \\
         \midrule%
        \rowcolor{gray!10}Amount & 10 & 10 & 10 & 10 & 40 \\
        \rowcolor{gray!10}\% in Total & 25,00\,\% & 25,00\,\% & 25,00\,\% & 25,00\,\% & 100,00\,\% \\
        \bottomrule
    \end{tabularx}

\end{table}
\end{document} 

enter image description here

  • Thank you, that basically works like a charm! Do you see any possibility to make the last column (Total) the same width as the "Heading Sub X" columns? I tried "adding" my custom column types to cellspace with \addparagraphcolumntypes{Z} etc., but SZ still does not work. I even tried S{p{2cm}} for each of the Z-columns, but even that does not work. Both solutions are actually mentioned in the cellspace manual though... – manidu Apr 2 '16 at 1:02
  • It sometimes works, but I never found when it works and when it doesn't. Maybe an interaction with colortbl. Anyway, for the Z as well as for the X, you should use a pair of braces (S{Z}) and declare them via \addparagraphcolumntypes{X,Z}. You can have the same width for the last column, replacing Sr with >{\raggedleft\arraybackslash}Z, but to the eye, it does not seem to have the same length as the others, because the column heading is shorter. One might obtain a correct optical effect via some programming, but is it worth complicating the code? – Bernard Apr 2 '16 at 1:24
  • You are right, colortbl obviously interferes with cellspace, preventing it from working with column types other than c, l and r... Unfortunately, I can not find a way to achieve the intended layout (with a p{2.5cm} as first column, followed by 5 columns with equal width), as the S from cellspace does not work with any of these types. I guess I could achieve this with a fixed-width eqparbox for the column headers, but I found another solution in the meantime, using tabu instead of tabularx... – manidu Apr 2 '16 at 10:49

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.