如何使用 TikZ 创建简单的因果链

如何使用 TikZ 创建简单的因果链

我在尝试创建一个简单的因果链时遇到了问题,如附件所示:可用的 TikZ 库列表及简短介绍

我有两个主要问题:

  1. 我想从 X 和 Y 中去除“云”。
  2. 来自 Z 的箭头应垂直击中从 X 到 Y 的箭头。

我花了几个小时尝试解决它们,但没有成功。

以下是我目前的 MWE:

\documentclass{article} 
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{shapes,arrows}
%
\begin{document} 
%   
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse, node distance=3cm, minimum height=2em]
 \begin{tikzpicture}[node distance = 3cm, auto]
  \node [cloud] (init) {Z1};      
  \node [blank, below of=init] (sup) {};
  \node [cloud, left of=X1] (X1) {X1};
  \node [cloud, right of=Y1] (Y1) {Y1};    
  \path [line] (X1) -- (Y1);
  \path [line] (init) -- (sup);
\end{tikzpicture}
%
\end{document}

输出如下:

在此处输入图片描述

想要的图像应该如下所示:

手绘图像

答案1

其他解决方案,也使用蒂克兹

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
    \begin{tikzpicture}[%
        >=latex',
        circ/.style={draw, shape=circle, node distance=1cm, line width=1pt}]%Define the arrow type and style for circled nodes
        \draw[->] (0,0) node[left] (X) {X} -- (2,0) node[right] (Y) {Y}; %Line between X and Y
        \path (X) -- coordinate (middle) (Y);%Defining the middle between X and Y
        \node[above of=middle,circ] (Z) {Z};%Draw Z, a circled node
        \draw[->] (Z) -- (middle);%Line between Z and X-Y
    \end{tikzpicture}
\end{document}

结果

在此处输入图片描述

如果您想使用椭圆或其他形状:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\begin{document}

    %With a ellipse
    \begin{tikzpicture}[%
        >=latex',
        cloud/.style={draw, thick, shape=ellipse, node distance=1cm, minimum width=0.8cm}]%Define the arrow type and style for ellipse nodes
        \draw[->,thick] (0,0) node[left] (X) {X} -- (2,0) node[right] (Y) {Y}; %Line between X and Y
        \path (X) -- coordinate (middle) (Y);%Defining the middle between X and Y
        \node[above of=middle,cloud] (Z) {Z};%Draw Z, a circled node
        \draw[->,thick] (Z) -- (middle);%Line between Z and X-Y
    \end{tikzpicture}

    %With a rectangle
    \begin{tikzpicture}[%
        >=latex',
        block/.style={rectangle,draw,thick,node distance=1cm,rounded corners}]%Define the arrow type and style for retangular nodes
        \draw[->,thick] (0,0) node[left] (X) {X} -- (2,0) node[right] (Y) {Y}; %Line between X and Y
        \path (X) -- coordinate (middle) (Y);%Defining the middle between X and Y
        \node[above of=middle,block] (Z) {Z};%Draw Z, a circled node
        \draw[->,thick] (Z) -- (middle);%Line between Z and X-Y
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

你有一些错误。

  1. 没有blank节点选项。
  2. 在 中\node [cloud, left of=X1] (X1) {X1};left of=X1是错误的,因为X1此时 是未知的。此外,您说的是left of因此X1以相同的高度向左移动。应该是below left = of init。 同样的道理\node [cloud, right of=Y1] (Y1) {Y1};您可以参考这个问题
  3. 使用库,您可以找到连接和的calc线的中点。因此您不需要。X1Y1\node [coordinate,below of=init] (sup) {};
  4. 不要使用\tikstyle\tikzset而应使用 。请参阅这个问题。

完整代码及修正:

\documentclass[a4paper,12pt,oneside]{article}
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{shapes,arrows,positioning,calc}  %% Added last two libraries
\begin{document}
%% Use tikzset instead of tikzstyle which is deprecated
\tikzset{line/.style = {draw, -latex'},
        cloud/.style = {draw, ellipse, node distance=3cm,
                        minimum height=2em}
}
 \begin{tikzpicture}[node distance = 3cm, auto]
    \node [cloud] (init) {Z1};
    %\node [coordinate,below of=init] (sup) {}; not needed
    \node [cloud, below left = of init] (X1) {X1};  %% note below left syntax from positioning library 
    \node [cloud, below right = of init] (Y1) {Y1};

     \path [line] (X1) -- (Y1);   %% (($(X1)!0.5!(Y1)$) is mdpoint of this line
     \path [line] (init) -- ($(X1)!0.5!(Y1)$);  %% $expression$ possible with calc library
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果您不想要ellipse周围的X1Y1,请cloud从它们中删除选项:

\node [below left = of init] (X1) {X1};  
\node [below right = of init] (Y1) {Y1};

要得到

在此处输入图片描述

另外,如果X1Y1是数学符号/变量,请不要忘记用$→将它们括起来{$X1$};

正如马克 (Marc) 在评论中指出的那样,您可以calc使用 来避免使用库\path[line] (init) -- (init -| X1);

答案3

您的代码相当混乱。这是您想要的吗?

\documentclass[tikz]{standalone} 
\usepackage{tikz}
\usepackage{pgf}
\begin{document}
\usetikzlibrary{shapes, calc, positioning}


\begin{tikzpicture}
    %create X and Y node
    \node (X) {X}; 
    \node [right = 1.5 of X] (Y) {Y};
    %create a coordinate in the middle of (X) and (Y)
    \coordinate (middle) at ($(X)!0.5!(Y)$);
    %draw Z above this middle
    %add "\," before and after Z to add some space, so that you get a nice ellipse
    \node[ellipse, draw, above = .5 of middle] (Z) {\,Z\,};
    %draw arrow between (X) and (Y)
    \draw (X) edge[->] (Y);
    %draw arrow from (Z) to 1pt above (middle)
    \draw(Z) edge[->] ($(middle)+(0,1pt)$);
\end{tikzpicture}

\end{document}

结果是:

结果

相关内容