1

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}

2 Answers 2

3
  • 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.

6
  • for the border isn't it possible to set it so that it does not eat the table sides? do you know if there's something to do to avoid the "Underfull \hbox..." warnings Feb 3 at 9:46
  • (i) which of presented solutions is what you after? (ii) regarding "eating": not so simple, actually approach with use of matrix is not possible. Is this so disturbing? You need to take another way to obtain desired solution (iii) Underfill hbox` is just warning, caused by defining of text width. They will vanish if you set it text width=5.4em or better, consider solution added in edited answer, will appear ASAP.
    – Zarko
    Feb 3 at 10:30
  • the one using "\draw[Border] ..." Feb 3 at 10:33
  • @user1850133, I must correct myself. I find simple solution for rounded corners of column pairs too. See edited answer. Now my answer deserve at least to be upvoted 😎.
    – Zarko
    Feb 3 at 11:15
  • I suppose "Border" is actually what you define as FIT style, is it? Feb 13 at 6:42
1

The problem with odd columns width was a } missing after every even column which makes every odd column style to be defined inside even columns.

For the alignment you need a text width

\documentclass[a4paper, landscape]{article}

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

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

\begin{document}
\centering
\begin{tikzpicture}

\matrix [matrix of nodes, nodes in empty cells, row sep=-\pgflinewidth, column sep=-\pgflinewidth,
         nodes={align=right, minimum height=1.5em, anchor=center, outer sep=0pt},
         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}

\begin{tikzpicture}

\matrix [matrix of nodes, nodes in empty cells, row sep=-\pgflinewidth, column sep=-\pgflinewidth,
         nodes={draw, align=right, minimum height=1.5em, anchor=center, outer sep=0pt},
         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}

enter image description here

2
  • did not you get lots of "Underfull \hbox (badness 3271) in paragraph at lines 103--103 [][]\TU/FreeSerif(0)/m/n/10 30" Feb 3 at 3:45
  • the "underfull \hbox (badness ..." seems to occur on all cells which content is much smaller than the cell. Feb 3 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.