Table with tikz with custom column settings and rounded corners

tikz-matrixtikz-pgf

I need to make a table, within a tikzpicture environment. So far i used tikz matrix, but i was not able to get what i desired. The table needs to have all contents aligned on the right. Odd column must be about 30mm wide, and even ones must be adjusted to fit the longest content within the corresponding column. Left and right cell margins should be 1 mm. I'd like both with visible and invisible lines. Here i filled even columns to be able to see widths. In my attempt it seems that despite my 'minimum width=3cm' or 'text width=3cm' odd columns width gets as narrow as it gets.

here is my mwe:

\documentclass[a4paper]{article}


\usepackage{tikz}
\usetikzlibrary{positioning, calc, matrix}

\tikzset{
    table/.style = {every even column/.style={nodes={minimum width=2.4cm, fill=gray!20},
                  every odd column/.style={nodes={minimum width=3cm}}},}
}

\begin{document}
\begin{tikzpicture}

\matrix [matrix of nodes, nodes in empty cells,
         nodes={align=right, minimum height=1.5em, anchor=center},
         table] (nombres)
{
1 & un & 11 & onze & 21 & vingt-et-un \\
2 & deux & 12 & douze & 22 & vingt-deux \\
3 & trois & 13 & treize & 23 & vingt-trois \\
4 & quatre & 14 & quatorze & 24 & vingt-quatre \\
5 & cinq & 15 & quinze & 25 & vingt-cinq \\
6 & six & 16 & seize & 26 & vingt-six \\
7 & sept & 17 & dix-sept & 27 & vingt-sept \\
8 & huit & 18 & dix-huit & 28 & vingt-huit \\
9 & neuf & 19 & dix-neuf & 29 & vingt-neuf \\
10 & dix & 20 & vingt & 30 & trente \\
};
\end{tikzpicture}
\end{document}

EDIT — here my improved version, with the answer of another user here. I'm open to simplification of the code and improvements. Especially, isn't there a way to tell at once the style of a custom list of columns i.e. neither odd nor even. Is it possible to get the 2 corners on the left of column 1, 4, and 7, the 2 on the right of column 2, 5, and 8?

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning, calc, matrix}

\tikzset{
  table/.style = {
font=\large, row sep=-\pgflinewidth, column sep=-\pgflinewidth,

nodes={align=right, minimum height=2.1em, anchor=center, outer sep=0pt},

  column 1/.style={
nodes={minimum width=12mm, text width=6mm, fill=red!10}},

  column 2/.style={
nodes={minimum width=24mm, text width=18mm, fill=green!10}},

  column 3/.style={
nodes={minimum width=14mm}},

  column 4/.style={
nodes={minimum width=12mm, text width=6mm, fill=red!10}},

  column 5/.style={
nodes={minimum width=24mm, text width=18mm, fill=green!10}},

  column 6/.style={
nodes={minimum width=14mm}},

  column 7/.style={
nodes={minimum width=12mm, text width=6mm, fill=red!10}},

  column 8/.style={
nodes={minimum width=24mm, text width=18mm, fill=green!10}},}
}

\begin{document}
\begin{tikzpicture}

\matrix at (.5\textwidth,0) [matrix of nodes, nodes in empty cells,
        matrix anchor=north, table] (nombres)
{
1 & un & & 11 & onze & & 21 & vingt-et-un \\
2 & deux & & 12 & douze & & 22 & vingt-deux \\
3 & trois & & 13 & treize & & 23 & vingt-trois \\
4 & quatre & & 14 & quatorze & & 24 & vingt-quatre \\
5 & cinq & & 15 & quinze & & 25 & vingt-cinq \\
6 & six & & 16 & seize & & 26 & vingt-six \\
7 & sept & & 17 & dix-sept & & 27 & vingt-sept \\
8 & huit & & 18 & dix-huit & & 28 & vingt-huit \\
9 & neuf & & 19 & dix-neuf & & 29 & vingt-neuf \\
10 & dix & & 20 & vingt & & 30 & trente \\
};
\end{tikzpicture}
\end{document}

Best Answer

  • in your second document example you need to remove all empty lines in table style definitions. Doing this, it gives red column with numbers and green columns with their names. Before green columns is white space.
  • It is not enterally clear to me, how your matrix should look like
  • I guess the matrix is supposed to be similar to the following:

enter image description here

  • For above matrix is not need to define table style, sufficient is defining odd and even column differences from common definition of nodes stale and add to odd columns column sep of desired width:

Edit: now the width of green columns is defined by widest text in those columns. By this all Underfull \hbox... warnings will vanish. This changes in code are marked by %%% <---

%   \documentclass[a4paper]{article}
\documentclass[margin=3.141592]{standalone}% that display only the matrix
\usepackage{tikz}
\usetikzlibrary{matrix} 
\newlength\greencolwidth                %%% <---
\settowidth\greencolwidth{vingt-quatre} %%% <---
                
\begin{document}
\begin{tikzpicture}

\matrix [matrix of nodes, 
         nodes in empty cells, 
         row sep=-\pgflinewidth, 
         nodes={minimum height=4ex, outer sep=0pt,
                anchor=center, align=right},
         every odd column/.style  = {nodes={text width=2em, fill=red!30}},
         every even column/.style = {nodes={text width=\greencolwidth, %%% <--- 
                                     fill=green!30},
                                     column sep=2em,
                                     }
         ] (nombres)
{
1 & un      & 11 & onze     & 21 & vingt-et-un  \\
2 & deux    & 12 & douze    & 22 & vingt-deux   \\
3 & trois   & 13 & treize   & 23 & vingt-trois  \\
4 & quatre  & 14 & quatorze & 24 & vingt-quatre \\
5 & cinq    & 15 & quinze   & 25 & vingt-cinq   \\
6 & six     & 16 & seize    & 26 & vingt-six    \\
7 & sept    & 17 & dix-sept & 27 & vingt-sept   \\
8 & huit    & 18 & dix-huit & 28 & vingt-huit   \\
9 & neuf    & 19 & dix-neuf & 29 & vingt-neuf   \\
10 & dix    & 20 & vingt    & 30 & trente       \\
};
\end{tikzpicture}
\end{document}

Addendum:

  • regarding rounded corners:
    • should have them any each in matrix like this:

enter image description here

In this case the matrix options are:

\matrix [matrix of nodes, 
         nodes in empty cells, 
         row sep=1pt, column sep=1.5pt,
         nodes={minimum height=3.4ex, rounded corners,
                anchor=center, align=right},
         every odd column/.style  = {nodes={text width=2em, fill=red!30}},
         every even column/.style = {nodes={text width=\greencolwidth, %%% <--- 
                                     fill=green!30},
                                     column sep=2em,
                                     }
         ] (nombres)
  • or like this?

enter image description here

where MWE is:

\usepackage{tikz}
\usetikzlibrary{fit,
                matrix,
                babel,      % if needed
                }
\newlength\greencolwidth                %%% <---
\settowidth\greencolwidth{vingt-quatre} %%% <---

\begin{document}
    \begin{tikzpicture}[
FIT/.style = {draw=white, line width=2mm, rounded corners=4mm,
              inner sep=0.5\pgflinewidth, fit=#1}
                        ]
\matrix [matrix of nodes,
         nodes in empty cells,
         row sep=-\pgflinewidth,
         nodes={minimum height=4ex, inner xsep=2mm,
                anchor=center, align=right},
         every odd column/.style  = {nodes={text width=2em, fill=red!30}},
         every even column/.style = {nodes={text width=\greencolwidth, %%% <---
                                     fill=green!30},
                                     column sep=2em,
                                     }
         ] (nombres)
{
1 & un      & 11 & onze     & 21 & vingt-et-un  \\
2 & deux    & 12 & douze    & 22 & vingt-deux   \\
3 & trois   & 13 & treize   & 23 & vingt-trois  \\
4 & quatre  & 14 & quatorze & 24 & vingt-quatre \\
5 & cinq    & 15 & quinze   & 25 & vingt-cinq   \\
6 & six     & 16 & seize    & 26 & vingt-six    \\
7 & sept    & 17 & dix-sept & 27 & vingt-sept   \\
8 & huit    & 18 & dix-huit & 28 & vingt-huit   \\
9 & neuf    & 19 & dix-neuf & 29 & vingt-neuf   \\
10 & dix    & 20 & vingt    & 30 & trente       \\
};
\node[FIT= (nombres-1-1) (nombres-10-2)] {};
\node[FIT= (nombres-1-3) (nombres-10-4)] {};
\node[FIT= (nombres-1-5) (nombres-10-6)] {};
    \end{tikzpicture}
\end{document}

Note: In the first solution rectangles with white rounded borders lie on red/green columns (because only on this way can cover outer columns corners), consequently half of border line is inside of columns. For this reason the inner xsep of matrix nodes was increased to 2mm.

The new solution use nodes which fit each each pair of columns. The settings of their style is such, that they cover only outer corners of column pairs. Consequently, they enable to define large radius of corners, however doing this the inner xsep should be increased accordingly too.