我正在尝试绘制多个位串,这些位串彼此相邻。不幸的是,我当前的解决方案不符合我的要求。我创建了一个位串,但在它旁边添加另一个位串会导致它在边缘被切断的问题。
我想要的是这样的:例子。因此我想要在右侧有另一个数组,在下面有另外两个数组。
这是我目前所拥有的:
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{chains,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
node distance=0pt,
start chain = A going right,
X/.style = {rectangle, draw,% styles of nodes in string (chain)
minimum width=2ex, minimum height=3ex,
outer sep=0pt, on chain},
]
\foreach \i in {0,0,1,1,0,0,1,0,0}
\node(p1)[X] {\i};
\hspace*{5mm}
\foreach \j in {1,0,0,1,1,0,0,1,0}
\node(p2)[X] {\j};
\end{tikzpicture}
\end{document}
答案1
您只需要启动不同的链。
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{chains,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
node distance=0pt,
X/.style = {rectangle, draw,% styles of nodes in string (chain)
minimum width=2ex, minimum height=3ex,
outer sep=0pt, on chain},
]
\begin{scope}[start chain = A1 going right]
\foreach \i in {0,0,1,1,0,0,1,0,0}
\node[X] {\i};
\end{scope}
%
\begin{scope}[start chain = A2 going right,yshift=-1cm]
\foreach \j in {1,0,0,1,1,0,0,1,0}
\node(p2)[X] {\j};
\end{scope}
%
\begin{scope}[start chain = B1 going right,xshift=5cm]
\foreach \i in {0,0,1,1,0,0,1,0,0}
\node[X] {\i};
\end{scope}
\end{tikzpicture}
\end{document}
为了更结构化地构建这些,您可以使用pic
s,并将它们放在矩阵中。您还可以添加样式,这里我添加了ibold
一个整数形式,边框上会画得较粗。您可以根据需要添加更多这样的键。pic
s 还有一个优点,即里面的节点从 继承名称pic
,因此
\draw[thick,-stealth] (L2-5.south) to[out=-90,in=90,looseness=0.6] (R3-4.north);
将把链的第 5 个节点L2
与链的第 4 个节点连接起来R3
,如图所示
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[ibold/.initial=20,
node distance=0pt,
X/.style = {rectangle, draw,% styles of nodes in string (chain)
minimum width=2ex, minimum height=3ex,
outer sep=0pt, on chain},
pics/.cd,chain/.style={code={
\begin{scope}[start chain =A going right,pic actions]
\foreach \i [count=\j]in {#1}
{\unless\ifnum\j<\pgfkeysvalueof{/tikz/ibold}
\tikzset{X/.append style={thick}}
\fi
\node [X] (-\j) {\i};}
\end{scope}
}}]
\matrix[column sep=1cm,row sep=8mm]{
\pic[ibold=5](L1){chain={0,0,1,1,0,0,1,0,0}};
&
\pic[ibold=6](R1){chain={0,0,1,1,0,0,1,0,0}};\\
\pic[ibold=3](L2){chain={1,0,0,1,1,0,0,1,0}};
&
\pic(R2){chain={0,0,1,1,0,0,1,0,0}};\\
\pic(L3){chain={0,0,1,1,0,0,1,0,0}};
&
\pic(R3){chain={0,0,1,1,0,0,1,0,0}};\\
};
\draw[thick,-stealth] (L1-6.south) -- (R2-3.north);
\draw[thick,-stealth] (L2-5.south) to[out=-90,in=90,looseness=0.6] (R3-4.north);
\end{tikzpicture}
\end{document}