15

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}
6
  • 1
    Is this a duplicate tex.stackexchange.com/questions/67586/…?
    – percusse
    Jun 9, 2014 at 23:30
  • It might be a simplified version of the problem in that question? (Because it doesn't involve the first-column-out-of-pattern problem.) But given the comments on the accepted answer there, I wonder if a different solution might be preferable here.
    – cfr
    Jun 9, 2014 at 23:55
  • @percusse Looks similar, but in my defence, there is no way I could have found that through searching. :-) Seems like I don't need something that complicated - all I need is to discard everything outside the node that draws the rounded rectangle. Plus, I'd prefer to stick with tabularx instead of tikz's matrix.
    – sudosensei
    Jun 10, 2014 at 0:02
  • You can put your table and clip that too. I meant the clipping part by the duplicate anyway :)
    – percusse
    Jun 10, 2014 at 0:22
  • maybe the tcolorbox package?
    – cmhughes
    Jun 10, 2014 at 0:33

3 Answers 3

15

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}
5
  • (+1) This is much better than my answer.
    – cfr
    Jun 10, 2014 at 0:52
  • 1
    Exactly the simple answer I had in mind! Thanks! Just to perfect this: do you know how I make it so the white background is not visible in the table when I use, for example, line width=0.1mm in the second node? The black border and clipped rounded rectangle do not merge seamlessly (especially at the corners).
    – sudosensei
    Jun 10, 2014 at 1:10
  • Notice that, clip has no effect in the node options in the current state.
    – percusse
    Jun 10, 2014 at 6:39
  • @sudosensei I have changed my answer. line width is now taken into account.
    – esdd
    Jun 10, 2014 at 7:08
  • This is great – it works perfectly! Thanks a lot!
    – sudosensei
    Jun 10, 2014 at 13:27
10

If you just need a framed tabular, tcolorbox can help you (as cmhughes suggested).

It has a tabularx option which accepts a tabularx header definition and also accepts colortbl or xcolor commands for coloring cells, rows or columns.

array, tabularx and xcolor (or colortbl) must be loaded in addition to tcolorbox.

If rowcolors or rowcolor are used, it's convinient to include clip upper option in tcolorbox definition (look at figures below).

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

\usepackage[most]{tcolorbox}

\begin{document}
  \rowcolors{2}{tablerow1}{tablerow2}

  \begin{tcolorbox}[enhanced, notitle, clip upper, 
         tabularx={>{\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{tcolorbox}    
\end{document}

enter image description here

Without clip upper option the result is:

enter image description here

Update

In case some code has to be applied before tabular starts, use option tabularx*={<code>}{<preamble>} instead of tabularx:

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

\usepackage[most]{tcolorbox}

\begin{document}
\rowcolors{2}{tablerow1}{tablerow2}
\begin{tcolorbox}[enhanced, notitle, clip upper,
       tabularx*={\renewcommand*{\arraystretch}{1.4}}%
                 {>{\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{tcolorbox}
\end{document}

enter image description here

3
  • Oh, I'm torn now! This looks lovely and incredibly elegant as well! May I ask what the equivalent of \renewcommand*{\arraystretch}{1.4} would be in this case? It doesn't seem to have any effect when I use it.
    – sudosensei
    Jun 10, 2014 at 13:18
  • @sudosensei All tabular changes can be applied with tabularx* option. See my updated answer.
    – Ignasi
    Jun 10, 2014 at 14:13
  • Great. I had tried that, but I apparently I had forgotten to add a % after the first parameter, resulting in a spurious whitespace character (and compilation error). Thanks!
    – sudosensei
    Jun 10, 2014 at 17:17
0

With {NiceTabular} of nicematrix (≥ 6.18). You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

\documentclass[border=10pt,10pt,table]{standalone}

\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{X[14,l]X[6,l]}[rounded-corners]
\CodeBefore
  \rowcolors[RGB]{1}{225,217,205}{236,229,221}
\Body
  \Block[rounded-corners,draw,transparent]{*-*}{}
  \RowStyle{\bfseries}
  Column 1 & Column 2 \\ \Hline[color=white]
  Some text. & Some text. \\ \Hline[color=white]
  Some text. & Some text.
\end{NiceTabular}

\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .