# tikz matrix cells not aligned correctly

I am trying to use matrix to draw cells, but the example code not aligned correctly from left to right. and some white space between the up/down cells also!

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\def\bdot{\tikz{
\node[circle,fill=black,minimum size=2pt,inner sep=0] (x) {};
\node[circle,fill=black,minimum size=2pt,inner sep=0,below=1pt of x] (y) {}}}
\begin{document}
\begin{tikzpicture}[>=latex,
box1/.style={fill={rgb,255:red,3; green,220; blue,128},minimum width=2em,minimum height=2em},
]
\matrix (n) [matrix of nodes,row sep=0,column sep=0,
nodes={box1}] {
0 & \bdot \\
};
\end{tikzpicture}
\end{document}


What's wrong with my code?

• Nesting TikZ pictures is deprecated. – egreg Apr 4 '18 at 20:38

I think there are two issues. First of all, as @egreg mentioned, it is deprecated to nest tikzpictures, but this was actually not the reason for the problem. Rather, as explained here, there is also an issue with the node anchors, which @BambOo's answer solves by adding the phantoms. As long as the matrix stays as simple as in your example, this trick may work. However, if the matrix is meant to get extended, I'd like to argue that the following solution is cleaner.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[>=latex,
box1/.style={fill={rgb,255:red,3; green,220; blue,128},minimum
width=2em,minimum height=2em,anchor=center}, % see https://tex.stackexchange.com/a/79031/121799
\matrix (n) [matrix of nodes,row sep=0,column sep=0,
nodes={box1},nodes in empty cells] {%<-added nodes in empty cells
0 &  \\
1 &  \\
};
\end{tikzpicture}
\tikz[overlay,remember picture]{%
\draw[fill=black] (n-1-2.center) circle (2.5pt);}
\tikz[overlay,remember picture]{%
\draw[fill=black] ([yshift=2pt]n-2-2.center) circle (1pt);
\draw[fill=black] ([yshift=-2pt]n-2-2.center) circle (1pt);}
\end{document}


It does not have nested tikzpictures, nor phantoms.

a simple solution, maybe you liked:

• simple matrix of math nodes,
• definition of nodes in matrix options
• for "dot" and >"colon" are used math expression used \bullet

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix,positioning}

\begin{document}
\begin{tikzpicture}
\matrix (n) [matrix of math nodes,
column sep=0pt,
row sep=0pt,
nodes={fill=green,
minimum size=2.2em,
anchor=center
}
]
{
0 & \bullet\atop\bullet \\
1 & \bullet             \\
};
\end{tikzpicture}
\end{document}


gives

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (n) [matrix of math nodes,
column sep=0pt,
row sep=0pt,
nodes={fill=green,
minimum size=2em, % <--- changed/reduced
anchor=center
}
]
{
0 & \bm{:}          \\ % <--- changed
1 & \bullet         \\
};
\end{tikzpicture}
\end{document}


• You like the matrices, the tables, apparently ahahah. :-) I upvote you. – Sebastiano Apr 4 '18 at 22:11
• Good idea! How can I adjust the distance of \atop? I wish to shrink the distance between the two dot but "node distance" option doesn't work. – lucky1928 Apr 5 '18 at 1:33
• @lucky1928, for \atop i need to search site if here is something about this. meanwhile i provide solution without \atop. – Zarko Apr 5 '18 at 6:56

Adding a \vphantom{0} before your \adot and \bdot gives

add anchor=center to box. If draw border, add -\pgflinewidth to remove duplicate grids.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\def\bdot{\tikz{
\node[circle,fill=black,minimum size=2pt,inner sep=0] (x) {};
\node[circle,fill=black,minimum size=2pt,inner sep=0,below=1pt of x] (y) {}}}
\begin{document}
\begin{tikzpicture}[>=latex,
box1/.style={draw,fill=gray!20,minimum width=2em,minimum height=2em,