我需要绘制类似于图片中所示的块,但其中许多是连续的。下面的代码生成一个块,但节点 A 和 B 彼此重叠。如何将它们分开以使其看起来平行?
\begin{tikzpicture}[node distance=2.5cm,auto,>=latex']
\node [int] (a) {half-adder};
\node (b) [above of=a,node distance=1.5cm, coordinate] {$A_1$};
\node (c) [above of=a,node distance=1.5cm, coordinate] {$B_1$};
\node (d) [left of=a,node distance=1.5cm, coordinate] {$C_i$};
\node (e) [below of=a,node distance=1.5cm, coordinate] {$C_i$};
\node (f) [right of=a,node distance=1.5cm, coordinate] {$C_o$};
\path[->] (d) edge node {$C_i$} (a);
\path[->] (b) edge node {$A_1$} (a);
\path[->] (c) edge node {$B_1$} (a);
\path[->] (a) edge node {$S_1$} (e);
\path[->] (a) edge node {$C_o$} (f);
\end{tikzpicture}
答案1
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
quotes,
}
\begin{document}
\begin{tikzpicture}[auto,
> = Latex,
node distance = 12mm and 3mm,
every edge/.style = {draw, ->},
int/.style = {draw, minimum height=7mm, fill=blue!30}
]
\node (a) [int] {half-adder};
\coordinate[above left=of a.north, label=below left:$A_1$] (b);
\coordinate[above right=of a.north, label=below right:$B_1$] (c);
\draw (b) edge (b |- a.north)
(c) edge (c |- a.north)
([xshift=-12mm] a.west) edge ["$C_i$"] (a)
(a) edge ["$C_o$"] ([xshift=+12mm] a.east)
(a) edge ["$S_1$"] ([yshift=-12mm] a.south);
\end{tikzpicture}
\end{document}
或者如果你想要在箭头的开始或结束处定义坐标:
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
quotes,
}
\begin{document}
\begin{tikzpicture}[auto,
> = Latex,
node distance = 12mm and 3mm,
every edge/.style = {draw, ->},
int/.style = {draw, minimum height=7mm, fill=blue!30}
]
\node (a) [int] {half-adder};
\coordinate[above left=of a.north, label=below left:$A_1$] (b);
\coordinate[above right=of a.north, label=below right:$B_1$] (c);
\draw (b) edge (b |- a.north)
(c) edge (c |- a.north);
\coordinate[ left=12mm of a] (in);
\coordinate[right=12mm of a] (out);
\draw (in) edge ["$C_i$"] (a)
(a) edge ["$C_i$"] (out);
\coordinate[below=of a] (s);
\draw (a) edge ["$S_1$"] (s);
\end{tikzpicture}
\end{document}
答案2
以@Zarko 的回答为例,我修改了上面的代码以产生所需的结果:
\begin{tikzpicture}[node distance=2.5cm,auto,>=latex']
\node [int] (a) {half-adder};
\node (b) [above of=a,node distance=1.5cm, coordinate] [xshift=-4mm] {$A_1$};
\node (c) [above of=a,node distance=1.5cm, coordinate] [xshift=4mm] {label=below right:$B_1$};
\node (d) [left of=a,node distance=1.5cm, coordinate] {$C_i$};
\node (e) [below of=a,node distance=1.5cm, coordinate] {$C_i$};
\node (f) [right of=a,node distance=1.5cm, coordinate] {$C_o$};
\path[->] (d) edge node {$C_i$} (a);
\path[->] (b) edge node {$A_0$} (b |- a.north);
\path[->] (c) edge node {$B_0$} (c |- a.north);
\path[->] (a) edge node {$S_1$} (e);
\path[->] (a) edge node {$C_o$} (f);
\end{tikzpicture}