# How to create a table using tikz (or how to make a row in one column to have same text width as widest row in that column, etc.)?

Following conditions apply:

1. width of each column must not exceed 25mm
2. a less-wide row of a column must expand to widest row of same column (not to 25mm if the widest row is less than 25mm)

Note that using varwidth environment inside a tikz node creates extra space on the right of such node (which I couldn't remove, maybe you would) and makes text wrapping worse compared to when text width is used. But text width would expand the node from the get go to 25mm, even when text inside it is less wide than 25mm.

As a bonus, it would be nice to reduce thickness of node borders in places where they touch each other so the border of the table (as a whole) looks uniform in its thickness (and doesn't look more defined in some places compared to others).

\documentclass{standalone}
\usepackage{tikz}
\tikzset
{ every node/.style=
{ anchor=north west
}
}
\begin{document}
\begin{tikzpicture}
% 25mm ruler
\path[draw=red,line width=1pt](0,0)--(25mm,0);

% should span two columns
\path node[draw](h)
};

\path node[draw](r1c1)at(h.south west)
{ row 1, col 1 (abcd)
};

\path node[draw](r1c2)at(r1c1.north east)
{ row 1, col 2 (abcdefg)
};

% width should match that of row 1, col 1
%  (because row 1, col 1 is wider)
\path node[draw](r2c1)at(r1c1.south west)
{ row 2, col 1
};

% width should match that of row 1, col 2
%  (because row 1, col 2 is wider)
\path node[draw](r2c2)at(r2c1.north east)
{ row 2, col 2 (ab)
};
\end{tikzpicture}
\end{document}


# Another take

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset
{ every node/.style=
{ anchor=north west
}
}
\begin{document}
\begin{tikzpicture}
% should span two columns
\path node[draw](h)
};

\path node
[ draw,
anchor=north east
](r1c1)at(h.south)
{ row 1, col 1 (abcd)
};

\path node[draw](r1c2)at(r1c1.north east)
{ row 1, col 2 (abcdefg)
};

% width should match that of row 1, col 1
%  (because row 1, col 1 is wider)
\path node
[ draw,
anchor=north east
](r2c1)at(r1c1.south east)
{ row 2, col 1
};

% width should match that of row 1, col 2
%  (because row 1, col 2 is wider)
\path node[draw](r2c2)at(r2c1.north east)
{ row 2, col 2 (ab)
};

% 25mm ruler
\path[draw=red,line width=1pt](r1c1.north east)--($(r1c1.north east)+(25mm,0)$);

\end{tikzpicture}
\end{document}


• There is \matrix for this, which has its own library. – user121799 May 20 at 18:07

Does this go in the right direction? If not, I will be happy to remove it.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes={inner sep=2pt},
column 1/.style={nodes={align=right,anchor=east}},
column 2/.style={nodes={align=left,anchor=west}},
column sep=-\pgflinewidth,row
sep=-\pgflinewidth,outer sep=0pt,inner sep=0pt,draw] (mat){
row 1, col 1 (abcd) & row 1, col 2 (abcdefg)\\
row 2, col 1 & row 2, col 2 (ab)\\
};
\draw ($(mat-1-1.south)!0.5!(mat-2-1.north)$) coordinate(aux1)
($(mat-1-1.east)!0.5!(mat-1-2.west)$) coordinate(aux2)
(mat.west|-aux1) -- (mat.east|-aux1)
(mat.north-|aux2) -- (mat.south-|aux2);
\path node[draw,anchor=south,outer sep=0pt](h) at (mat.north-|aux2)
($(mat-1-1.north east)+(25mm,0)$);