![如何使用 tikz 在节点旁边绘制垂直箭头?](https://linux22.com/image/409202/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20tikz%20%E5%9C%A8%E8%8A%82%E7%82%B9%E6%97%81%E8%BE%B9%E7%BB%98%E5%88%B6%E5%9E%82%E7%9B%B4%E7%AE%AD%E5%A4%B4%EF%BC%9F.png)
我正在尝试使用 Tikz 重新创建以下框图。
我使用以下代码创建了框图:
\tikzstyle{node}=[draw, fill=pink, minimum width=4.2cm]
\tikzstyle{init} = [pin edge={to-,thin,black}]
\tikzstyle{end} = [pin edge={-to,thin,black}]
\begin{figure}
\begin{tikzpicture}[node distance=0.35cm, auto,>=latex']
\node [node, align=center, pin={[init, align=center, pin distance=0.35cm]above:{\footnotesize data}}] (a) {\small a};
\node [node, align=center, below= of a] (b) {\small b};
\node [node, align=center, below= of b] (c) {\small c};
\node [node, align=center, below= of c] (d) {\small d};
\node [node, align=center, below= of d] (e) {\small e};
\node [node, align=center, below= of e] (f) {\small f};
\node [node, align=center, below= of f, pin={[end, align=center, pin distance=0.35cm]below:{\footnotesize data}}] (g) {g};
\path[->] (a) edge (b);
\path[->] (b) edge (c);
\path[->] (c) edge (d);
\path[->] (d) edge (e);
\path[->] (e) edge (f);
\path[->] (f) edge (g);
\end{tikzpicture}
\caption{Block diagram.}
\label{fig:diagram}
\end{figure}
结果是:
但是,我想在节点旁边画几个带有描述的箭头,但不知道如何实现。我的想法是这样的:
我该怎么做?非常感谢。
答案1
也许是这样的:
我通过使用循环稍微合理化了 OP 中的代码\foreach
。如果节点的内容比a
,... 更令人兴奋,g
那么您可以使用我在描述中使用的相同技巧将这些额外信息添加到循环中\foreach
。
为了添加描述,我使用了“粉色”节点的东部边界以及一个小节点xshift=4mm
和一个用于描述文本的节点。唯一的其他变化是使用\tikzset
现已弃用的`\tikzstyle。
以下是更新后的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\tikzset{
node/.style={draw, fill=pink, minimum width=4.2cm},
data/.style={align=center, minimum width=4.2cm}
}
\begin{tikzpicture}[node distance=0.35cm, auto,>=latex]
\node[data](data) {\footnotesize data};
\foreach \nd [remember=\nd as \Nd (initially data)] in {a,...,g} {
\node [node, align=center, below=of \Nd] (\nd) {\small \nd};
\draw[->](\Nd)--(\nd);
}
\node[data, below=of g] (Data) {\footnotesize data};
\draw[->](g)--(Data);
\foreach \nd/\desc [remember=\nd as \Nd (initially data)]
in {a/description 1,c/description 2,e/description 3,g/description 4, Data/description 5} {
\draw[blue, <->]([xshift=4mm]\Nd.east)--node[right]{\desc}([xshift=4mm]\nd.east);
}
\end{tikzpicture}
\end{document}
编辑
托马斯的回答如下如何使用 TikZ 在节点内添加换行符?,如果您想要有多行描述,那么只需向描述节点添加对齐即可。我通过添加额外的desc
样式来实现这一点:
以下是更新后的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\tikzset{
node/.style={draw, fill=pink, minimum width=4.2cm},
data/.style={align=center, minimum width=4.2cm},
desc/.style={align=left, right}
}
\begin{tikzpicture}[node distance=0.35cm, auto,>=latex]
\node[data](data) {\footnotesize data};
\foreach \nd [remember=\nd as \Nd (initially data)] in {a,...,g} {
\node [node, align=center, below=of \Nd] (\nd) {\small \nd};
\draw[->](\Nd)--(\nd);
}
\node[data, below=of g] (Data) {\footnotesize data};
\draw[->](g)--(Data);
\foreach \nd/\desc [remember=\nd as \Nd (initially data)]
in {a/description 1,c/description 2,e/description 3,g/description 4\\that is a little longer, Data/description 5} {
\draw[blue, <->]([xshift=4mm]\Nd.east)--node[desc]{\desc}([xshift=4mm]\nd.east);
}
\end{tikzpicture}
\end{document}
答案2
@Abdrew 的回答有点(题外话)变化:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
chains, % new
positioning,
quotes % new
}
\begin{document}
\begin{tikzpicture}[auto,
node distance = 4mm and 3mm,
> = {Triangle[angle=45:1pt 3]},
start chain = A going below,
base/.style = {text width=42mm, align=center, font=\small,
on chain=A, join=by -Straight Barb},
box/.style = {base, draw, fill=pink},
every edge quotes/.style = {align=left, font=\scriptsize}
]
% nodes in chain
\node [base,font=\footnotesize] {data};
\foreach \i in {a,...,g}
{
\node [box] {\i};
}
\node [base,font=\footnotesize] {data};
% arrows
\def\dscr{2/Description 1,
4/Description 2 \\ has two lines,
6/Description 3,
8/Description 4,
9/Description 5}
\foreach \i/\j [remember=\i as \k (initially 1)] in \dscr
{
\draw[blue,<->, semithick] ([xshift=4mm] A-\k.east) edge ["\j"] ([xshift=4mm] A-\i.east);
} \end{tikzpicture}
\end{document}
注意:如果链中节点的实际内容有更长的描述,您可以像对箭头标签所做的那样定义节点内容列表并在\forach
循环中读取其包含的内容:
% ...
\node [base,font=\footnotesize] {data};
\def\task{first task,
description of the second task has,
task c,
after task c\\ follows task d,
e,
f,
g}
\foreach \i in \task
{
\node [box] {\i};
}
% ...
这使: