0

I am using tabularx + booktabs and I am looking for a way to increase the vertical spacing between rows, but at the same time keep the content of each row vertically centered.

I have tried to use arraystretch but, as shown in this answer, the additional space is not evenly distributed; more space tends to be added to the 'top' of the row than to the bottom.

I would like to find a solution that affects all rows in all tables of a document (which has dozens of long tables) without having to manually insert spaces, struts, etc. for each individual row.

More details: I am not using midrules to separate individual rows. I am relying on \rowcolor to differentiate rows.

Here's a MWE taken from egreg's answer:

\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage{lipsum} % just for the example

\usepackage[table]{xcolor}
\rowcolors{2}{gray!10}{white}

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{@{}llX@{}}
\toprule
A & B & \centering C \tabularnewline
\midrule
1 & 2 & \lipsum*[2] \\

1 & 2 & \lipsum*[2] \\
\bottomrule
\end{tabularx}

\bigskip

\noindent
\begin{tabularx}{\textwidth}{@{}llX@{}}
\toprule
A & B & \centering C \tabularnewline
\midrule
1 & 2 & \lipsum*[2] \\

1 & 2 & \lipsum*[2] \\
\bottomrule
\end{tabularx}

\end{document}

Is this possible?

1

A simple solution would be to use the cellspace package, which defines minimal spacing at the top and bottom of cells in columns with specifier prefixed with the letter S. Unfortunately, when applied to the X column type and `colortbl is loaded, it doesn't work any more, for some reason.

So I had to use a work-around: cellspace is used for the first column, and at the end of an X cell, I automatically add an invisible rule under the base line with >{}.

\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage{lipsum} % just for the example
\usepackage{bigstrut}
\usepackage[table, svgnames]{xcolor}
\usepackage{cellspace}
\setlength\cellspacetoplimit{1.5ex}
\setlength\cellspacebottomlimit{1.5ex}
\rowcolors{2}{lightgray!30!Lavender}{white}

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{Sl l X <{\rule[-2ex]{0pt}{0ex}}}%
\toprule
\rowcolor{white}
A & B &\multicolumn{1}{c}{C} \tabularnewline
\midrule
1 & 2 & \lipsum*[2] \\ %
1 & 2 & \lipsum*[2] \\ %
1 & 2 & \lipsum*[2] \\
\bottomrule
\end{tabularx}

\end{document}

enter image description here

  • Works very well. But I didn't need the invisible rule workaround at all. What problem is it fixing ? – Grodriguez Nov 22 '16 at 14:36
  • It replaces \addlinespace and adds some padding under the last line of the X cells. If you don't have it, normally the coloured zone ends very close (too close in my opinion) below the last line. – Bernard Nov 22 '16 at 14:48
  • Ah, I see it now. Indeed this fixes it. – Grodriguez Nov 22 '16 at 15:12
  • It doesn't work with X columns also when colortbl is not used. – yannis Mar 12 at 20:38
  • @yannis: Could you post as anew question a small illustrating the problems you meet? – Bernard Mar 12 at 20:42
3

It seems to me that you could be helped if there was a \extrarowdepth parameter similar to \extrarowheight. The package tabu has that. Unfortunately it doesn't seem to work with multiline cells. So I wrote a small package that does just that: add an \extrarowdepth parameter.

It works even when tabu is loaded, but not for the tabu environment. It just shares the its \extrarowdepth length parameter in that case, just to avoid a conflict.

Here it is:

%% extrarowdepth.sty
%% Copyright (C) 2016 by Piet van Oostrum <piet@vanoostrum.org>
%% This package defines a new length variable \extrarowdepth, similar to
%% \extrarowheight in the `array' package.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{extrarowdepth}%
           [2016/11/225 v1.0 Define \string\extrarowdepth\space for arrays and tabulars]%
% Prevent a clash with tabu, or other packages that also define \extrarowdepth.
\AtBeginDocument{%
  \ifdefdimen\extrarowdepth{} % \extrarowdepth already defined as a length
  {\ifdef\extrarowdepth % if it is defined but not a length, we have an error
    {\PackageError{extrarowdepth}{Cannot define length \string\extrarowdepth}
      {There is already a \string\extrarowdepth\space and it is not a length parameter.}}
    {\newlength\extrarowdepth\setlength\extrarowdepth{0pt}}}%
  \newlength\@temprowdepth}

\RequirePackage{array}
\RequirePackage{etoolbox}

\patchcmd{\@array}{\setbox \@arstrutbox \hbox{\vrule \@height \arraystretch \@tempdima \@depth \arraystretch \dp \strutbox \@width \z@}}
{\@temprowdepth  \dp \strutbox \advance \@temprowdepth by\extrarowdepth \setbox \@arstrutbox \hbox{\vrule \@height \arraystretch \@tempdima \@depth \arraystretch \@temprowdepth \@width \z@}}
{\PackageInfo{extrarowdepth}{\string\extrarowdepth\space added to the array package}}
{\PackageWarning{extrarowdepth}{Failed to include \string\extrarowdepth\space in the array package}}
\let\@@array\@array

And here is an example how to use it.

\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage{lipsum} % just for the example
\usepackage{extrarowdepth}

\usepackage[table, svgnames]{xcolor}
\rowcolors{2}{lightgray!30!Lavender}{white}

\begin{document}

\setlength\extrarowheight{4pt}
\setlength\extrarowdepth{4pt}

\noindent
\begin{tabularx}{\textwidth}{@{}llX@{}}
\toprule
\rowcolor{white}
A & B & \centering C \tabularnewline
\midrule
1 & 2 & \lipsum*[2] \\
1 & 2 & \lipsum*[2] \\
1 & 2 & \lipsum*[2] \\
\bottomrule
\end{tabularx}

\end{document}
  • This is great :) It would be nice if the extrarowdepth could be defiend in the preamble, though; right now it must be defined after begin{document} – Grodriguez Nov 23 '16 at 17:16
  • Well, if you promise not to use the tabu package, or use it before extrarowdepth, you can take out the \AtBeginDocument part. I just want to prevent a clash if tabu comes after extrarowdepth. I'll think about it. – Pieter van Oostrum Nov 23 '16 at 23:19
  • And if you don't use tabu, you can add in the preamble: \newlength\extrarowdepth \setlength\extrarowdepth{...} and the package will be happy at \begin{document} to see that the length is already there. – Pieter van Oostrum Nov 23 '16 at 23:23
1

Act on the parameters \aboverulesep and \belowrulesep:

\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage{lipsum} % just for the example

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{@{}llX@{}}
\toprule
A & B & \centering C \tabularnewline
\midrule
1 & 2 & \lipsum*[2] \\
\midrule
1 & 2 & \lipsum*[2] \\
\bottomrule
\end{tabularx}

\bigskip

\setlength{\aboverulesep}{10pt}
\setlength{\belowrulesep}{10pt}

\noindent
\begin{tabularx}{\textwidth}{@{}llX@{}}
\toprule
A & B & \centering C \tabularnewline
\midrule
1 & 2 & \lipsum*[2] \\
\midrule
1 & 2 & \lipsum*[2] \\
\bottomrule
\end{tabularx}

\end{document}

enter image description here

With colored rows, you can insert \addlinespace:

\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage{lipsum} % just for the example

\usepackage[table]{xcolor}

\rowcolors{2}{gray!10}{white}

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{llX}
\toprule
\rowcolor{white}
A & B & \centering C \tabularnewline
\midrule
1 & 2 & \lipsum*[2] \\ \addlinespace
1 & 2 & \lipsum*[2] \\ \addlinespace
1 & 2 & \lipsum*[2] \\
\bottomrule
\end{tabularx}

\end{document}

enter image description here

  • Good one (+1), it almost does what I need. My only problem is that I am not actually using midrules between regular table rows -- I am relying on \rowcolors to differentiate rows. This introduces two problems: 1/ aboverulesep and belowrulesep don't help if there are no rules, 2/ the spacing introduced by these commands doesn't get allong well with row coloring. I'll update my question to reflect this. – Grodriguez Nov 22 '16 at 12:19
  • 1
    @Grodriguez Row coloring and booktabs don't go along anyway. – egreg Nov 22 '16 at 12:20
  • Why? It seems to work pretty well so far – Grodriguez Nov 22 '16 at 12:21
  • \addlinespace -- yes, that's what I was trying to avoid (as this means I need to modify each individual line of each table). Or is it possible to globally redefine \tabularnewline (which I am using everywhere) to automatically call \addlinespace ? – Grodriguez Nov 22 '16 at 12:36
  • @Grodriguez No, as you also want to cope with rules. – egreg Nov 22 '16 at 12:46

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.