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 withp
/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).
tabularx
(which would use a simpler code)?