[Tex/LaTex] Table with alternating row colours and rounded corners

tablestikz-pgf

I am trying to create a table with alternating row colours and rounded corners.

I combined two answers so far to achieve this:

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:

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 whole tikzpicture and use clip as node option. If you want to change the line width you have to set the new value also as an option of the tikzpicture.

Note that the line width also influences the table width if the table including the border should fit \textwidth.

\documentclass[border=10pt,tikz,table]{standalone}
%\documentclass{article}
%\usepackage[table]{xcolor}
%\usepackage{tikz}
\usepackage{booktabs}
\usepackage{tabularx}
\definecolor{tablerow1}{RGB}{225,217,205}
\definecolor{tablerow2}{RGB}{236,229,221}

\begin{document}
  \rowcolors{2}{tablerow1}{tablerow2}
  \noindent\begin{tikzpicture}[line width=.1mm,rounded corners=.5em]
  \node(thetable) [clip,inner sep=.5\pgflinewidth] {
    \begin{tabularx}{\dimexpr\textwidth-2\pgflinewidth\relax}{>{\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 ([xshift=.5*\pgflinewidth,yshift=-.5*\pgflinewidth]thetable.north west) 
    rectangle ([xshift=-.5*\pgflinewidth,yshift=.5*\pgflinewidth]thetable.south east);
  \end{tikzpicture}
\end{document}

enter image description here

Or you can use the even odd rule to fill the area around the table:

\documentclass[border=10pt,tikz,table]{standalone}
%\documentclass{article}
%\usepackage[table]{xcolor}
%\usepackage{tikz}
\usepackage{booktabs}
\usepackage{tabularx}
\definecolor{tablerow1}{RGB}{225,217,205}
\definecolor{tablerow2}{RGB}{236,229,221}

\begin{document}
  \rowcolors{2}{tablerow1}{tablerow2}
  \noindent\begin{tikzpicture}[line width=.1mm]
  \node (thetable) [inner sep=.5\pgflinewidth] {
    \begin{tabularx}{\dimexpr\textwidth-2\pgflinewidth\relax}{>{\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}
  };
  \fill[white,even odd rule]
    (current bounding box.north west)rectangle(current bounding box.south east)
    {[rounded corners=.5em]([xshift=.5*\pgflinewidth,yshift=-.5*\pgflinewidth]thetable.north west) 
      rectangle ([xshift=-.5*\pgflinewidth,yshift=.5*\pgflinewidth]thetable.south east)};
  \draw[rounded corners=.5em] ([xshift=.5*\pgflinewidth,yshift=-.5*\pgflinewidth]thetable.north west) 
    rectangle ([xshift=-.5*\pgflinewidth,yshift=.5*\pgflinewidth]thetable.south east);
  \end{tikzpicture}
\end{document}