# Problem with tabularx's vertical spacing

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
\cline{2-6}
\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:

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.

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
\cmidrule(l){2-6}

• 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