# Specific-length vertical padding in table (with text wrapping, alternating row colors, and specified widths)

I'm trying to create fixed-width (i.e. spanning the whole \textwidth) tables, with alternating row colors and arbitrary alignments (horizontal and vertical), that have evenly-spaced specific-length (i.e. I can specify an absolute length) vertical padding on the rows.

To begin, a MWE (it does have quite a lot of features, I suppose, but they should all represent something I need in the final result):

\documentclass{book}

\usepackage{lipsum}

\usepackage[table]{xcolor}
\definecolor{mycolor}{gray}{0.85}

\begin{document}

\begin{table}
\rowcolors{1}{}{mycolor}
\begin{tabular}{>{\centering\arraybackslash}p{1cm}>{\raggedright\arraybackslash}p{\dimexpr\the\textwidth-1cm-4\tabcolsep\relax}}
\textbf{Name} & \textbf{Column 2} \\
1 & \lipsum*[66] \\
2 & \lipsum*[75] \\
3 & \lipsum*[66] \\
4 & \lipsum*[75]
\end{tabular}
\end{table}

\end{document}


So, I can specify the table widths, color the rows, and justify/align the text. All is good so far. Now I would like to add the vertical spacing.

I've tried a few combinations already:

• \arraystretch, produces uneven vertical padding when used with p/m/etc column types.

• booktabs doesn't play well with the row colors, as the increased vertical spacing isn't colored (and any solutions I've seen are best suited to coloring single rows, not alternating rows - i.e. by redefining the behaviour above and below the \midrule command)

My hope is that I can get cellspace to work, but when I include the following code:

\usepackage{cellspace}
\setlength{\cellspacetoplimit}{10pt}
\setlength{\cellspacebottomlimit}{10pt}

[...]

\begin{tabular}{S{>{\centering\arraybackslash}p{1cm}}
S{>{\raggedright\arraybackslash}p{\dimexpr\the\textwidth-1cm-4\tabcolsep\relax}}}


I get the following:

Where the text isn't wrapping properly, but the resulting lines are evenly spaced (and the row colors are the correct width, actually).

Any help on getting the cellspace padding to work with text-wrapped lines would be greatly appreciated.

• Ideally I would like to avoid importing dozens of libraries (they're already giving me a headache in the full document), but I understand this may be unavoidable.

• I would also prefer an extensible solution (i.e. one where I don't have to add code to every row of the table), as my document has a large number of these tables.

• And if the solution worked with \multicolumn, that would be sweet.

P.S. I've been trying to make this work with tabularx so far, and I very much enjoy the convenience of combining X and c columns, so if your solution involves tabularx, that would be nice. I didn't include it because I wanted to avoid overcomplicating matters with lots of packages. However, the error in tabularx does seem to be the same (i.e. the absence of text-wrapping when using cellspace columns).

• Welcome to TeX SX! Did you try with tabularx (which would use a simpler code)? – Bernard Jun 29 '19 at 20:06
• Why hello. Yes, I did, as I mentioned in the PS down the bottom. However, I ran into exactly the same problems. – Gebodal Jun 29 '19 at 20:14
• However, I can edit the question to use tabularx, if it would simplify matters? (I was worried it would complicate them) – Gebodal Jun 29 '19 at 20:18
• Never mind, I worked with your initial code and modified it so that it works, and added some improvements – Bernard Jun 29 '19 at 20:45

## 1 Answer

The >{...] should be outside the S pre-column specifier. Herer is a simple code with tabularx.

I also took the liberty to change your definition of mycolor to a nicer one (for my taste). Of course, feel free to return to your initial version if you preferit.

\documentclass{book}
\usepackage[latin]{babel}
\usepackage[showframe]{geometry}
\usepackage{lipsum}
\usepackage{ragged2e}
\usepackage{tabularx}
\usepackage[table, svgnames]{xcolor}
\colorlet{mycolor}{Gainsboro!70!Lavender}%
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{10pt}
\setlength{\cellspacebottomlimit}{10pt}
\addparagraphcolumntypes{X}

\begin{document}

\begin{table}
\centering
\rowcolors{1}{}{mycolor}
\begin{tabularx}{\linewidth}{Sc >{\RaggedRight\arraybackslash}S{X}}
\textbf{Name} & \textbf{Column 2} \\
1 & \lipsum*[66] \\
2 & \lipsum*[75] \\
3 & \lipsum*[66] \\
4 & \lipsum*[75]
\end{tabularx}
\end{table}

\end{document}


• Thank you very much for the response. I'm having a little troubling reproducing your image, though. When I copy-paste your LaTeX into Overleaf (I tried it with all compilers, just in case, but I'm primarily using XeLaTeX) I only get spacing at the top of the cells - with the exception of the first row, which also has bottom spacing. Do you have any additional setup I'm missing? – Gebodal Jun 29 '19 at 21:02
• I only used what I posted. To be sure, I've just compiled with xelatex, which gives the same result. Did you compile exactly the code I posted, and nothing more? – Bernard Jun 29 '19 at 21:48
• Yes, exactly the same. Which is confusing. Overleaf example project: v2.overleaf.com/read/sqwvdhmkxbsc – Gebodal Jun 29 '19 at 21:52
• Could you check the version of cell space which is used? Mine is 1.8.1 (19/03/2019). – Bernard Jun 29 '19 at 21:55
• Yep, that was it. Overleaf has v1.6 (2009/07/31) - lord knows why. Thank you so much! (tested with manually uploaded v1.8 and all was well) – Gebodal Jun 29 '19 at 22:03