节点距离命令不起作用

节点距离命令不起作用

我正在尝试通过指定使表更接近节点node distance

代码:

\documentclass[12pt]{article}
\usepackage{array}
\newcolumntype{C}{>{$}c<{$}}
\usepackage{tikz}
\usepackage{float}
\usetikzlibrary{shapes, arrows.meta, positioning}
\tikzset{
    events/.style={ellipse, draw, align=center},
}
\begin{document}
    \begin{figure}[H]\centering
        \begin{tikzpicture}[node distance=2cm, >= Stealth, shorten >= 1pt]
        \node [events] (A) {$A$};
        \node [events, below left = of A] (C) {$C$};
        \node [events, below right = of A] (D) {$D$};
        \node [events, above right = of D] (B) {$B$};
        
        \draw [->] (A) -- (C);
        \draw [->] (A) -- (D);
        \draw [->] (B) -- (D);
        
        \node [above left = of A, node distance = 1cm] {
            \begin{tabular}{CC}
            \mathrm{P(A=F)} & \mathrm{P(A=T)}\\
            \hline
            0.9 & 0.1\\
            \end{tabular}
        };
        
        \node [above right = of B,node distance = 1cm] {
            \begin{tabular}{CC}
            \mathrm{P(B=F)} & \mathrm{P(B=T)}\\
            \hline
            0.5 & 0.5\\
            \end{tabular}
        };
        
        \node [below left = of C,node distance = 1cm] {
            \begin{tabular}{c|CC}
            A & \mathrm{P(C=F)} & \mathrm{P(C=T)}\\
            \hline
            F & 0.8 & 0.2\\
            T & 0.3 & 0.7\\
            \end{tabular}
        };
        
        \node [below right = of D, node distance = 1cm] {
            \begin{tabular}{cc|CC}
            A & B & \mathrm{P(D=F)} & \mathrm{P(D=T)}\\
            \hline
            F & F & 0.7 & 0.3\\
            T & F & 0.3 & 0.7\\
            F & T & 0.4 & 0.6\\
            T & T & 0.1 & 0.9\\
            \end{tabular}
        }; 
        \end{tikzpicture}
    \end{figure}
\end{document}

输出:

在此处输入图片描述

node distance命令对表相对于节点的位置没有任何影响。

答案1

如果您设置了该键,则该node distance键有效below right=。您还可以说类似

\node [below right =1cm and 0.5cm of D]...

其中可以设置两个距离,垂直和水平。

但是,当您开始放置表格时,您似乎希望将所有节点距离设置为较小的值。然后,只需放置一个

 \tikzset{node distance = 0.6cm and 0.5cm}

在里面tikzpicture

\documentclass[tikz,border=3mm]{standalone}
\usepackage{array}
\newcolumntype{C}{>{$}c<{$}}
\usetikzlibrary{shapes.geometric, arrows.meta, positioning}
\tikzset{
    events/.style={ellipse, draw, align=center},
}
\begin{document}
\begin{tikzpicture}[node distance=2cm, >= Stealth, shorten >= 1pt]
     \node [events] (A) {$A$};
     \node [events, below left = of A] (C) {$C$};
     \node [events, below right = of A] (D) {$D$};
     \node [events, above right = of D] (B) {$B$};

     \draw [->] (A) -- (C);
     \draw [->] (A) -- (D);
     \draw [->] (B) -- (D);
     \tikzset{node distance = 0.6cm and 0.5cm}
     \node [above left = of A] {
         \begin{tabular}{CC}
         \mathrm{P(A=F)} & \mathrm{P(A=T)}\\
         \hline
         0.9 & 0.1\\
         \end{tabular}
     };

     \node [above right = of B] {
         \begin{tabular}{CC}
         \mathrm{P(B=F)} & \mathrm{P(B=T)}\\
         \hline
         0.5 & 0.5\\
         \end{tabular}
     };

     \node [below left = of C] {
         \begin{tabular}{c|CC}
         A & \mathrm{P(C=F)} & \mathrm{P(C=T)}\\
         \hline
         F & 0.8 & 0.2\\
         T & 0.3 & 0.7\\
         \end{tabular}
     };

     \node [below right = of D] {
         \begin{tabular}{cc|CC}
         A & B & \mathrm{P(D=F)} & \mathrm{P(D=T)}\\
         \hline
         F & F & 0.7 & 0.3\\
         T & F & 0.3 & 0.7\\
         F & T & 0.4 & 0.6\\
         T & T & 0.1 & 0.9\\
         \end{tabular}
     }; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容