TikZ 流程图中沿路径的箭头

TikZ 流程图中沿路径的箭头

我感兴趣的是,以一种更快的方式沿着流程图中的路径绘制箭头,而不是单独绘制每个箭头。

我们先来看一些代码,MWE:

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{pgf,tikz}
\usetikzlibrary{chains,positioning,arrows,shapes.geometric,quotes,fit,calc}

\tikzstyle{startstop} = [%
rectangle,
rounded corners,
%   minimum width=3cm,
%   max width=2cm,
minimum height=1cm,
text centered,
text width=2cm,
line width=1pt,
draw=DarkBlue,
fill=DarkBlue!30
]

\tikzstyle{process} = [%
rectangle,
%   minimum width=3cm,
%   max width=2cm,
minimum height=1cm,
text centered,
text width=2cm,
draw=orange,
fill=orange!30
]

\tikzstyle{arrow} = [%
thick,
->,
>=stealth,
]

\tikzstyle{mainarrow} = [%
ultra thick,
->,
>=stealth,
]

\begin{document}

    \begin{tikzpicture}
    \node (mycenternode) [startstop]                      {Isgrind};
    \node (mynorthnode)  [process, above=of mycenternode] {Sami};
    \node (mysouthnode)  [process, below=of mycenternode] {Romrike};
    \node (mywestnode)   [process, left=of mycenternode]  {Rykinmaa};
    \node (myeastnode)   [process, right=of mycenternode] {Lukinsola};

    \draw [mainarrow] (mynorthnode)  --                           (mycenternode);
    \draw [mainarrow] (mycenternode) -- node[anchor=west]  {kuf}  (mysouthnode);
    \draw [arrow] (mywestnode)   -- node[anchor=south] {ders} (mycenternode);
    \draw [arrow] (mycenternode)  -- node[anchor=south]  {olan} (myeastnode);

    \path [arrow] (mywestnode) |- node[anchor=south] {punrot} (mynorthnode) -| (myeastnode);
    \end{tikzpicture}

\end{document}

而不必说

    \draw [mainarrow] (mynorthnode)  -- (mycenternode);
    \draw [mainarrow] (mycenternode) -- node[anchor=west]  {kuf}  (mysouthnode);
    \draw [arrow] (mywestnode) -- node[anchor=south] {ders} (mycenternode);
    \draw [arrow] (mycenternode) -- node[anchor=south]  {olan} (myeastnode);

我想知道如何将其缩短为类似

\path [mainarrow] (mynorthnode)  -- (mycenternode) -- node[anchor=west]  {kuf}  (mysouthnode);
\path [arrow] (mywestnode) -- node[anchor=south] {ders} (mycenternode) -- node[anchor=south]  {olan} (myeastnode);

额外内容:在最后几行中,你可以看到一个\path。我不明白为什么它没有绘制任何东西。

答案1

我认为您正在寻找edge操作。您已经加载了引号(以及几个您不使用的包),这允许您使用更短的语法将标签附加到箭头。因此,您的代码的相关部分可以压缩为

\draw  (mynorthnode)  edge[mainarrow]  (mycenternode) 
    (mycenternode) edge[mainarrow,"kuf"]    (mysouthnode)
     (mywestnode)   edge[arrow,"ders"] (mycenternode)
  (mycenternode)  edge[arrow,"olan"] (myeastnode);

请注意,\path单独绘制是行不通的。这就是线条不出现的原因。我为带角的边添加了两种样式,BRBL,这样最后的路径也可以用边绘制,

\draw [arrow] (mywestnode)  edge[BR,"punrot"] (mynorthnode)
 (mynorthnode) edge[BL] (myeastnode);

这是 MWE,其中删除了一些未使用的库,并\tikzset用 代替\tikzstyle

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{pgf,tikz}
\usetikzlibrary{positioning,quotes}

\tikzset{startstop/.style={
rectangle,
rounded corners,
%   minimum width=3cm,
%   max width=2cm,
minimum height=1cm,
text centered,
text width=2cm,
line width=1pt,
draw=DarkBlue,
fill=DarkBlue!30},
process/.style={%
rectangle,
%   minimum width=3cm,
%   max width=2cm,
minimum height=1cm,
text centered,
text width=2cm,
draw=orange,
fill=orange!30},
arrow/.style={%
thick,
->,
>=stealth,
},
mainarrow/.style={%
ultra thick,
->,
>=stealth,
},
BR/.style={ to path={|- (\tikztotarget)  \tikztonodes}},
BL/.style={ to path={-| (\tikztotarget)  \tikztonodes}}}

\begin{document}

\begin{tikzpicture}
    \node (mycenternode) [startstop]                      {Isgrind};
    \node (mynorthnode)  [process, above=of mycenternode] {Sami};
    \node (mysouthnode)  [process, below=of mycenternode] {Romrike};
    \node (mywestnode)   [process, left=of mycenternode]  {Rykinmaa};
    \node (myeastnode)   [process, right=of mycenternode] {Lukinsola};

    \draw  (mynorthnode)  edge[mainarrow]  (mycenternode) 
        (mycenternode) edge[mainarrow,"kuf"]    (mysouthnode)
         (mywestnode)   edge[arrow,"ders"] (mycenternode)
      (mycenternode)  edge[arrow,"olan"] (myeastnode);

    \draw [arrow] (mywestnode)  edge[BR,"punrot"] (mynorthnode)
     (mynorthnode) edge[BL] (myeastnode);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用 时,您只会获得一个箭头提示--,但是如果您改用edge,您将获得每个段的箭头提示,因为edge会插入单独的路径(我认为)。无论如何,您实际上只需指定一次中心节点即可:

\path [mainarrow]  (mycenternode) edge[<-]                          (mynorthnode)
                                  edge           node[right] {kuf}  (mysouthnode)
                                  edge[arrow,<-] node[above] {ders} (mywestnode)
                                  edge[arrow]    node[above] {olan} (myeastnode);

相关内容