为什么尽管使用定位,我的 tikz 绘图中的线条并没有按预期连接?

为什么尽管使用定位,我的 tikz 绘图中的线条并没有按预期连接?
\documentclass{beamer}
\usetheme{Boadilla}

\usepackage{tikz}
\usetikzlibrary{shapes, arrows, arrows.meta, positioning, calc}

\begin{document}

\begin{frame}{Block Diagrams}

\begin{tikzpicture}[
        block/.style = {draw, fill=white, rectangle, minimum height=3em, minimum width=3em},
        scalar/.style= {draw, fill=white, circle, node distance=1cm, node contents=$a$},
        sum/.style= {draw, fill=white, circle, node distance=1cm, node contents=$+$, scale = 0.7},
        ]
        % 1st block diagram
        \coordinate (input) at (0,0);
        \node (s1) [scalar, right = 1cm of input]{};
        \node (f1) [block, right = 1cm of s1]{$F$};
        \node (out1) [right = 1cm of f1]{};
        \draw [->] (input) to node[above]{$x$}(s1);
        \draw [->] (s1) to (f1);
        \draw [->] (f1) to node[above]{$y$}(out1);


        % 3rd block diagram
        \node (f3) [block, below = 1.0cm of f1]{$F$};
        \node (out3) [right = 1cm of f3]{};
        \draw [->] (f3) to node[above]{$y$}(out3);
        \node (sum1) [sum, left = 0.5cm of f3]{};
        \draw [->] (sum1) to (f3);
        \node (c1) [above left = 0.5 and 0.5 of sum1]{};
        \node (c2) [below left = 0.5 and 0.5 of sum1]{};
        \node (in3) [left = 1cm of c1]{};
        \node (in4) [left = 1cm of c2]{};
        \draw [->] (c1) to (sum1);
        \draw [->] (c2) to (sum1);
% These two lines do not connect as expected
        \draw [-] (in3) to node[above]{$x1$}(c1);
        \draw [-] (in4) to node[above]{$x2$}(c2);             
\end{tikzpicture}
\end{frame}

\end{document}

答案1

节点有尺寸,坐标没有,参见TikZ:\node 和 \coordinate 之间的区别?

\documentclass{beamer}
\usetheme{Boadilla}

\usepackage{tikz}
\usetikzlibrary{shapes, arrows, arrows.meta, positioning, calc}

\begin{document}

\begin{frame}{Block Diagrams}

\begin{tikzpicture}[
        block/.style = {draw, fill=white, rectangle, minimum height=3em, minimum width=3em},
        scalar/.style= {draw, fill=white, circle, node distance=1cm, node contents=$a$},
        sum/.style= {draw, fill=white, circle, node distance=1cm, node contents=$+$, scale = 0.7},
        ]
        % 1st block diagram
        \coordinate (input) at (0,0);
        \node (s1) [scalar, right = 1cm of input];
        \node (f1) [block, right = 1cm of s1]{$F$};
        \node (out1) [right = 1cm of f1]{};
        \draw [->] (input) to node[above]{$x$}(s1);
        \draw [->] (s1) to (f1);
        \draw [->] (f1) to node[above]{$y$}(out1);


        % 3rd block diagram
        \node (f3) [block, below = 1.0cm of f1]{$F$};
        \node (out3) [right = 1cm of f3]{};
        \draw [->] (f3) to node[above]{$y$}(out3);
        \node (sum1) [sum, left = 0.5cm of f3];
        \draw [->] (sum1) to (f3);
        \coordinate (c1) at ([shift={(-.5,.5)}]sum1);
        \coordinate (c2) at ([shift={(-.5,-.5)}]sum1);
        \node (in3) [left = 1cm of c1]{};
        \node (in4) [left = 1cm of c2]{};
        \draw [->] (c1) to (sum1);
        \draw [->] (c2) to (sum1);
        \draw [-] (in3) to node[above]{$x1$}(c1);
        \draw [-] (in4) to node[above]{$x2$}(c2);             
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

我想用 TikZ 矩阵展示一种替代解决方案:

\documentclass{beamer}
\usetheme{Boadilla}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{frame}{My proposal}

\begin{tikzpicture}[
        block/.style = {draw, rectangle, minimum height=3em, minimum width=3em},
        scalar/.style= {draw, circle},
        sum/.style= {scale = 0.7},
        inout/.style= {inner sep = 0pt},
        ]
        \matrix[
            matrix of math nodes,
            ampersand replacement=\&,
            column sep = 1cm,
            nodes={anchor=center},
            column 1/.style={nodes={inout}},
            column 2/.style={nodes={scalar}},
            column 3/.style={nodes={block}},
            column 4/.style={nodes={inout}},
            ]{
            % 1st block diagram
            |[name=in1]|{} \&[.1cm] |[name=s1]|{a} \& 
            |[name=f1]|{F} \&
            |[name=out1]|{}\\[1cm]        
        % 3rd block diagram
            |[name=in3up]|{} \\[-.1cm] 
             \& |[name=sum3,sum]|{+} \& 
            |[name=f3]|{F} \&
            |[name=out3]|{}\\[-.1cm]
            |[name=in3down]|{} \\ 
            };   
        % connections of the 1st block diagram
        \draw[->] (in1) -- node[above]{$x$} (s1);
        \draw[->] (s1) -- (f1);
        \draw[->] (f1) -- node[above]{$y$} (out1);
        % connections of the 3rd block diagram
        \draw [->] (in3up) -- node[above]{$x_1$} ++(1,0) -- (sum3);
        \draw [->] (in3down) -- node[above]{$x_2$} ++(1,0) -- (sum3);
        \draw [->] (sum3) -- (f3);
        \draw[->] (f3) -- node[above]{$y$} (out3);
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

答案2

OP 代码的变体。考虑到 @CarLaTeX 建议使用\coordinate而不是\node,使用quotes 库并更一致地使用positioning库,MWE 可以是:

\documentclass{beamer}
\usetheme{Boadilla}

\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning, quotes}

\begin{document}
\begin{frame}
\frametitle{Block Diagrams}
    \centering % if needed
    \begin{tikzpicture}[auto,
    node distance = 10mm and 10 mm,
  base/.style = {draw, fill=white, minimum size=5mm, inner sep=0pt},
 block/.style = {base, minimum size=3em},
scalar/.style = {base, circle, node contents=$a$},
   sum/.style = {base, circle, node contents=$+$},
every edge/.style = {draw, -Straight Barb}
                        ]
        % 1st block diagram
\coordinate (in1);
\node (s1) [scalar, right = of in1];
\node (f1) [block,  right = of s1]  {$F$};
\coordinate[right=of f1] (out1);
%
\draw   (in1)   edge ["$x$"] (s1) 
        (s1)    edge         (f1) 
        (f1)    edge ["$y$"] (out1);

% 3rd block diagram
\node (f3)  [block, below = of f1]   {$F$};
\coordinate[right=of f3] (out3);
    \scoped[node distance=5mm and 5mm]
{
\node (sum) [sum, left=of f3];
\coordinate[above left=of sum] (c1);
\coordinate[below left=of sum] (c2);
}
\coordinate[left=of c1] (in3);
\coordinate[left=of c2] (in4);
%
\draw   (in3)   to["$x1$"]  (c1) edge (sum);
\draw   (in4)   to["$x2$"]  (c2) edge (sum);
\draw   (sum)   edge        (f3)
        (f3)    edge["$y$"] (out3);
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容