If you set row sep
to -\pgflinewidth
, the lines will overlap exactly:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}]
{
0 & 6 \\ 1 & 3 \\ };
\end{tikzpicture}
\end{document}
In order to make sure that the cells align correctly horizontally, you need to make the text height
and text depth
the same across the entire row. By setting nodes in empty cells
, all cells will be drawn, even if no content is provided. Alternatively, you can enter {}
into empty cells to achieve the same effect.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black},
space/.style={minimum height=1.5em,matrix of nodes,row sep=-\pgflinewidth,column sep=-\pgflinewidth,column 1/.style={font=\ttfamily}},text depth=0.5ex,text height=2ex,nodes in empty cells]
\matrix (first) [space, column 1/.style={font=\ttfamily},column 2/.style={nodes={cell,minimum width=2em}}]
{
0 & 6 \\ 1 & 3 \\ 2 & 9 \\ };
\matrix (second) [right=of first, space, column 2/.style={minimum width=3em,nodes={cell,minimum width=3.5em}},column 3/.style={nodes={cell,minimum width=2em}}]
{
0 &a & 6 \\ 1 & & 3 \\ 2 &c & 9 \\ };
\matrix [right=of second, space, column 2/.style={minimum width=3em,nodes={cell,minimum width=3.5em}},column 3/.style={nodes={cell,minimum width=2em}}]
{
0 &a & 6 \\ 1 &b & 3 \\ 2 &c & 9 \\ };
\end{tikzpicture}
\end{document}
\foreach
isn't expandable and the grouping is most likely causing some issues as well. I would recommend to use the loop outside the matrix and accumulate the rows in a macro which then only has to be expanded. The etoolbox
package provides \gappto
(global append to; \xappto
would cause issues with fragile content) which can be used here. There is also \g@addto@macro
provided by the LaTeX kernel, if you don't want to rely on e-TeX and don't mind to have to use \makeatletter
/\makeatother
.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{etoolbox}
\begin{document}
\begin{tikzpicture}
\let\mymatrixcontent\empty
\foreach \c in {a,b,c}{%
\expandafter\gappto\expandafter\mymatrixcontent\expandafter{\c\\}%
% or
%\xappto\mymatrixcontent{\expandonce{\c\\}}
}
\matrix [matrix of nodes] {
\mymatrixcontent
};
\end{tikzpicture}
\end{document}
Best Answer
Remove the extra
\\
after the node closing semicolon and it works