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
table
style definitions. Doing this, it gives red column with numbers and green columns with their names. Before green columns is white space.table
style, sufficient is defining odd and even column differences from common definition of nodes stale and add to odd columnscolumn 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%%% <---
Addendum:
In this case the matrix options are:
where MWE is:
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.