如何在 TikZ 中连接两条线?

如何在 TikZ 中连接两条线?

我终于设法用 TikZ 绘制了以下路径图。然而,现在我面临一项具有挑战性的任务。我想连接两条边/线(这里用红色绘制;对于社会科学家来说,我想说明一种相互作用)。我该怎么做?

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,mindmap,trees,shapes,shadows,calc,arrows,automata,decorations.text}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[grow=left,
  line/.style={>=latex},
  coef/.style={font=\tiny},
  rect/.style={font=\tiny, rectangle, draw, text width=.49cm, align=center, node distance = 1.8cm},
  mycircle/.style={font=\tiny, circle, draw,  node distance = 0.2cm},
  beschrif/.style={font=\tiny}]

  \node[rect] (stab) {$D$};

  \node[rect] (zuf_m) [above left = of stab] {$S_m$};
  \node[rect] (zuf_f) [below left = of stab] {$S_f$};

  \node[rect] (konf_m) [above left = of zuf_m] {$FoC_m$};
  \node[rect] (konf_f) [below left = of zuf_f] {$FoC_f$};

  \node[rect] (unemp_m) [below left= of konf_m] {$U_m$};
  \node[rect] (unemp_f) [above left= of konf_f] {$U_f$};

  \node[rect] (behav_m) [above = 0.6cm of zuf_m] {$CB_m$};
  \node[rect] (behav_f) [below = 0.6cm of zuf_f] {$CB_f$};

  \node (error_sm) [mycircle, right = 1.3cm of zuf_m] {$\epsilon_{Sm}$};
  \node (error_cm) [mycircle, right = 2.8cm of konf_m] {$\epsilon_{FoCm}$};

  \node (error_sf) [mycircle, right = 1.3cm of zuf_f] {$\epsilon_{Sf}$};
  \node (error_cf) [mycircle, right = 2.8cm of konf_f] {$\epsilon_{FocCf}$};

  \path (zuf_m.east) edge[->,line] (stab.west);
  \path (zuf_f.east) edge[->,line] (stab.west);

  \path (unemp_m.east) edge[->,line] (zuf_m.west);
  \path (unemp_f.east) edge[->,line] (zuf_f.west);
  \path (unemp_m.east) edge[->,line] (zuf_f.north west);
  \path (unemp_f.east) edge[->,line] (zuf_m.south west);

  \path (unemp_m.east) edge[->,line] (konf_m.west);
  \path (unemp_f.east) edge[->,line] (konf_f.west);
  \path (unemp_m.east) edge[->,line] (konf_f.north west);
  \path (unemp_f.east) edge[->,line] (konf_m.south west);

  \path (konf_m.east) edge[->,line] (zuf_m.north west);
  \path (konf_f.east) edge[->,line] (zuf_f.south west);

  \path (error_sm) edge[->, line] (zuf_m);
  \path (error_sf) edge[->, line] (zuf_f);

  \path (error_cm) edge[->, line] (konf_m);
  \path (error_cf) edge[->, line] (konf_f);

  \path (error_sm) edge[<->, bend left=90, line] (error_sf);
  \path (error_cm) edge[<->, bend left=90, line, looseness=1] (error_cf);

\end{tikzpicture}

\end{document}

答案1

您可以使用calctikzlibrary(您已经包含)来计算线上的一个点,即两个坐标之间的点:

\draw[-latex,red] (behav_f.west) -- ($(konf_f.east)!0.6!(zuf_f.south west)$);

值 0.6 给出了两个坐标之间的相对位置。您可以根据需要进行调整。

在此处输入图片描述

答案2

取决于你想要什么,

  1. 只需沿路径放置一个坐标,即可使用键pos(或其包装器之一,如at start)来指定位置;或者
  2. 如果您想要一个特定的角度,您可以使用intersection of(或intersections库)。(left:1)是极坐标的一个特例,其含义与 相同(180:1),当然您可以使用任何角度。对于使用intersections库的解决方案,您首先需要命名两个路径:

    \draw[->,name path=pathA] (a.east) -- (c.west);
    \path[name path=pathB] (b.west) -- ++ (<angle>:2);
    

    然后你就可以找到与

    \draw[->, name intersections={of=pathA and pathB}] (b.west) -- (intersection-1);
    

    如果您不想猜测所需的距离(使用时intersections,路径实际上必须交叉,这与intersection of解决方案不同),您可以使用相当大的距离,10但将其包装在pgfinterruptboundingbox环境内以确保路径不会干扰图片的边界框。

代码

\documentclass[tikz,convert=false]{standalone}
\tikzset{nodes=draw,every edge/.append style={thick}}
\begin{document}
\begin{tikzpicture}
\node (a) {A};
\node (b) at (2,-1) {B};
\node (c) at (2,-2) {C};
\path[->] (a.east) edge coordinate[pos=.7] (ac) (c.west)
          (b.west) edge [shorten >=2\pgflinewidth] (ac);
\end{tikzpicture}

\begin{tikzpicture}
\node (a) {A};
\node (b) at (2,-1) {B};
\node (c) at (2,-2) {C};
\path[->] (a.east) edge (c.west)
          (b.west) edge [shorten >=2\pgflinewidth]
                      (intersection of a.east--c.west and b.west--[shift={(left:1)}]b.west);
\end{tikzpicture}
\end{document}

答案3

除了前面的答案之外,如果您希望连接具有一定的角度,则可以使用intersections库。您需要两个named path:一个是edge

\path (konf_m.east) edge[->,line,name path=a] (zuf_m.north west);

另一个定义为

\path[name path=aa] (behav_m.west)--++(-135:3cm);

从上一条路径到下一条路径的箭头behav_m.west

  \draw[red,name intersections={of=a and aa},->,line] (behav_m.west)--(intersection-1);

完整代码及结果如下:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections,positioning,mindmap,trees,shapes,shadows,calc,arrows,automata,decorations.text}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[grow=left,
  line/.style={>=latex},
  coef/.style={font=\tiny},
  rect/.style={font=\tiny, rectangle, draw, text width=.49cm, align=center, node distance = 1.8cm},
  mycircle/.style={font=\tiny, circle, draw,  node distance = 0.2cm},
  beschrif/.style={font=\tiny}]

  \node[rect] (stab) {$D$};

  \node[rect] (zuf_m) [above left = of stab] {$S_m$};
  \node[rect] (zuf_f) [below left = of stab] {$S_f$};

  \node[rect] (konf_m) [above left = of zuf_m] {$FoC_m$};
  \node[rect] (konf_f) [below left = of zuf_f] {$FoC_f$};

  \node[rect] (unemp_m) [below left= of konf_m] {$U_m$};
  \node[rect] (unemp_f) [above left= of konf_f] {$U_f$};

  \node[rect] (behav_m) [above = 0.6cm of zuf_m] {$CB_m$};
  \node[rect] (behav_f) [below = 0.6cm of zuf_f] {$CB_f$};

  \node (error_sm) [mycircle, right = 1.3cm of zuf_m] {$\epsilon_{Sm}$};
  \node (error_cm) [mycircle, right = 2.8cm of konf_m] {$\epsilon_{FoCm}$};

  \node (error_sf) [mycircle, right = 1.3cm of zuf_f] {$\epsilon_{Sf}$};
  \node (error_cf) [mycircle, right = 2.8cm of konf_f] {$\epsilon_{FocCf}$};

  \path (zuf_m.east) edge[->,line] (stab.west);
  \path (zuf_f.east) edge[->,line] (stab.west);

  \path (unemp_m.east) edge[->,line] (zuf_m.west);
  \path (unemp_f.east) edge[->,line] (zuf_f.west);
  \path (unemp_m.east) edge[->,line] (zuf_f.north west);
  \path (unemp_f.east) edge[->,line] (zuf_m.south west);

  \path (unemp_m.east) edge[->,line] (konf_m.west);
  \path (unemp_f.east) edge[->,line] (konf_f.west);
  \path (unemp_m.east) edge[->,line] (konf_f.north west);
  \path (unemp_f.east) edge[->,line] (konf_m.south west);

  \path (konf_m.east) edge[->,line,name path=a] (zuf_m.north west);
  \path (konf_f.east) edge[->,line,name path=b] (zuf_f.south west);

  \path (error_sm) edge[->, line] (zuf_m);
  \path (error_sf) edge[->, line] (zuf_f);

  \path (error_cm) edge[->, line] (konf_m);
  \path (error_cf) edge[->, line] (konf_f);

  \path (error_sm) edge[<->, bend left=90, line] (error_sf);
  \path (error_cm) edge[<->, bend left=90, line, looseness=1] (error_cf);

  \path[name path=aa] (behav_m.west)--++(-135:3cm);
  \path[name path=bb] (behav_f.west)--++(135:3cm);
  \draw[red,name intersections={of=a and aa},->,line] (behav_m.west)--(intersection-1);
  \draw[red,name intersections={of=b and bb},->,line] (behav_f.west)--(intersection-1);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容