I am trying to create a table with alternating row colours and rounded corners.
I combined two answers so far to achieve this:
- Thorsten Donnig's answer about alternating row colours.
- Jake's answer about adding rounded corners to a table.
This works great in general, but the problem is that the alternating row colours method fills a proper square, not a rounded one, and the row colour "leaks" outside the table border. See the top left and bottom left corners of the image below.
Is there an easy way to fix this? Maybe by using the named tikz
node to clip everything outside it?
Issue:
MWE:
\documentclass[border=10pt,10pt,table]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{array}
\usepackage{booktabs}
\usepackage{tabularx}
\definecolor{tablerow1}{RGB}{225,217,205}
\definecolor{tablerow2}{RGB}{236,229,221}
\begin{document}
\rowcolors{2}{tablerow1}{tablerow2}
\begin{tikzpicture}
\node (thetable) [inner sep=0pt] {
\begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash\hsize=1.4\hsize}X>{\raggedright\arraybackslash\hsize=0.6\hsize}X}
\arrayrulecolor{white}
\textbf{Column 1} & \textbf{Column 2} \\ \hline
Some text. & Some text. \\ \hline
Some text. & Some text.
\end{tabularx}
};
\draw [rounded corners=.5em] (thetable.north west) rectangle (thetable.south east);
\end{tikzpicture}
\end{document}
Best Answer
Set
rounded corners=.5em
as a option for the wholetikzpicture
and useclip
as node option. If you want to change theline width
you have to set the new value also as an option of thetikzpicture
.Note that the
line width
also influences the table width if the table including the border should fit\textwidth
.Or you can use the
even odd rule
to fill the area around the table: