我正在尝试使用矩阵来绘制单元格,但是示例代码没有从左到右正确对齐。并且上下单元格之间也有一些空白!
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\def\adot{\tikz{\node[circle,fill=black,minimum size=5pt,inner sep=0] {}}}
\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 \\
1 & \adot \\
};
\end{tikzpicture}
\end{document}
我的代码有什么问题?
答案1
我认为有两个问题。首先,正如 @egreg 提到的,它已被弃用为 nest tikzpictures
,但这实际上不是问题的原因。相反,正如解释的那样这里,节点锚点也存在问题,@BambOo 的答案通过添加幻影解决了这个问题。只要矩阵保持与您的示例一样简单,此技巧可能有效。但是,如果矩阵要扩展,我想争辩说以下解决方案更简洁。
\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
remember picture] %<-added
\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}
它没有嵌套的tikzpicture
,也没有幻像。
答案2
一个简单的解决方案,也许你会喜欢:
- 简单的数学节点矩阵,
- 矩阵选项中的节点定义
- 对于“点”和“冒号”使用数学表达式
\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}
给出
附录: 带粗体冒号:
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix}
\usepackage{bm} % <--- added
\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}
答案3
答案4
为框添加anchor=center。如果要绘制边框,请添加-\pgflinewidth以删除重复的网格。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\def\adot{\tikz{\node[circle,fill=black,minimum size=5pt,inner sep=0] {}}}
\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,
anchor=center}, %<-- add anchor
]
\matrix (n) [matrix of nodes,
row sep=-\pgflinewidth, column sep = -\pgflinewidth, %<-- remove duplicate grid line
nodes={box1}] {
0 & \bdot \\
1 & \adot \\
};
\end{tikzpicture}
\end{document}
输出: