如何将具有不同高度的节点(在 TikZ 中)放置在两条线上,以使节点具有相同的距离?

如何将具有不同高度的节点(在 TikZ 中)放置在两条线上,以使节点具有相同的距离?

这是我修改后的代码这里

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}

  \begin{tikzpicture}[auto,
                      node distance = 3cm,
                      signal/.style = coordinate,
                      sum/.style = {draw,
                                    circle,
                                    node distance = 2cm
                                   },
                      block/.style = {draw,
                                      rectangle,
                                      minimum height = 2em,
                                      minimum width = 4em
                                     },
                      branch/.style = {sum,
                                       fill = black
                                      }
                     ]
    %placing the blocks
    \node[signal] (input) {};
    \node[sum, right of = input] (left sum) {};
    \node[block, right of = {left sum}] (controller) {Regler};
    \node[block, right of = controller] (system) {Strecke, $x(t)$};
    %connecting the controller and system to calculate the coordinate u,
    %it needed to place the measurement block
    \draw
      [->] (controller) -- node[name=u] {$u(t)$} (system);
    \node[block, below of = u] (measurement) {Messglied};
    \node[sum, right of = system] (right sum) {};
    \node[signal, above of = {right sum}] (disturbances) {};
    \node[branch, right of = {right sum}] (branch) {};
    %do the same as above (connect system and controller,
    %to be able to place measurement) didn't work here,
    %because the nodes have different height
    \draw
      (right sum) -- (branch);
    \node[sum, below of = branch] (lower sum) {};
    \node[signal, right of = branch] (output) {};
    \node[signal, right of = {lower sum}] (measurement noise) {};
    %connecting the placed nodes
    \draw
      [->] (input) -- node {$w(t)$} (left sum);
    \draw
      [->] (left sum) -- node {$e(t)$} (controller);
    \draw
      [->] (system) -- (right sum);
    \draw
      [->] (disturbances) -- node {$z(t)$} (right sum);
    \draw
      [->] (branch) -- node {$y(t)$} (output);
    \draw
      [->] (branch) -- (lower sum);
    \draw
      [->] (measurement noise) -- node[above] {$m(t)$} (lower sum);
    \draw
      [->] (lower sum) -- (measurement);
    \draw
      [->] (measurement) -| node[pos = .99] {$-$} (left sum);
  \end{tikzpicture}

\end{document}

我的错误框图的结果如下:

框图

这些错误是:

  1. 标题中有描述。
  2. 用红色标记的线应该具有大致相同的长度。
  3. 我无法控制圆的半径。例如,sum/.style = {draw, circle, radius = 1mm, node distance = 2cm}什​​么也没做。

简而言之:如何以自动化和最佳的方式解决上述任务。欢迎任何优化以及完全不同的解决方案!

提前感谢您的帮助和努力!

答案1

这可能会解决您的所有问题,尽管我不完全确定您第一点的意思。

从最后开始,使用第 3 点。没有radius圆形节点的键,minimum size而是使用它来定义直径。

对于第 2 点。执行 时right of=a,测量的是节点中心点之间的距离。当节点大小不同时,节点边界之间的距离会有所不同。但<position> of=无论如何,这些键都被视为已弃用,请参阅PGF/TikZ 中“right of=”和“right=of”之间的区别. 你应该positioning使用

\usetikzlibrary{positioning}

并使用right=of a和类似。这样,就可以测量节点边界之间的距离。(如果您希望在节点中心之间测量它们,则有关键信息on grid,请参阅手册。)

最后,第一点。假设你想放置梅斯格利德节点,使得从右侧进入的箭头是水平的,有各种可能性。

下面我将该节点移到lower sum代码中的节点之后,然后使用

\node[block] (measurement) at (u|-lower sum) {Messglied};

TikZ:箭头的 |- 符号到底起什么作用?|-如果您还不知道的话,请查看其描述。

代码输出

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

  \begin{tikzpicture}[auto,
                      node distance = 1.2cm,
                      signal/.style = coordinate,
                      sum/.style = {draw,
                                    circle,
                                    minimum size=1.5em % <--- modified
                                   },
                      block/.style = {draw,
                                      rectangle,
                                      minimum height = 2em,
                                      minimum width = 4em
                                     },
                      branch/.style = {sum,
                                       fill = black
                                      }
                     ]
    %placing the blocks
    \node[signal] (input) {};
    \node[sum, right=of input] (left sum) {};
    \node[block, right=of left sum] (controller) {Regler};
    \node[block, right=of controller] (system) {Strecke, $x(t)$};
    %connecting the controller and system to calculate the coordinate u,
    %it needed to place the measurement block
    \draw
      [->] (controller) -- node[name=u] {$u(t)$} (system);
    \node[sum, right=of system] (right sum) {};
    \node[signal, above=of right sum] (disturbances) {};
    \node[branch, right=of right sum] (branch) {};
    %do the same as above (connect system and controller,
    %to be able to place measurement) didn't work here,
    %because the nodes have different height
    \draw
      (right sum) -- (branch);
    \node[sum, below=of branch] (lower sum) {};
    \node[signal, right=of branch] (output) {};
    \node[signal, right=of lower sum] (measurement noise) {};
    % place measurement node
    \node[block] (measurement) at (u|-lower sum) {Messglied}; % <-- modified
    %connecting the placed nodes
    \draw
      [->] (input) -- node {$w(t)$} (left sum);
    \draw
      [->] (left sum) -- node {$e(t)$} (controller);
    \draw
      [->] (system) -- (right sum);
    \draw
      [->] (disturbances) -- node {$z(t)$} (right sum);
    \draw
      [->] (branch) -- node {$y(t)$} (output);
    \draw
      [->] (branch) -- (lower sum);
    \draw
      [->] (measurement noise) -- node[above] {$m(t)$} (lower sum);
    \draw
      [,->] (lower sum) -- (measurement);
    \draw
      [->] (measurement) -| node[pos = .99] {$-$} (left sum);
  \end{tikzpicture}

\end{document}

答案2

您的代码中存在更多问题:

  • 你使用过时的语法进行节点定位。最近需要positioning库和节点相对定位,例如\node[sum, right = of in] (sum-in) {}
  • 总之,您node distance再次确定样式定义,但其数量与其他方案元素的数量不同
  • 在相对定位中你应该考虑节点高度(因为较低的总和没有对齐)。

纠正这个问题后,我得到:

在此处输入图片描述

编辑:通过使用以下tikz库,您的代码可以简化:calcchainspositioning。为了获得漂亮的箭头,我建议使用arrows.meta库。

  • 方案区块位于链中
  • 对于区块名称使用链名称(A,因此第一个区块是A-1
  • 为了缩短连接线的代码,添加了代码,从而无需重复块名称即可绘制边缘。例如:

标准

\path   (in)    edge ["$w(t)$"] (A-1)
        (A-1)   edge ["$e(t)$"] (A-2)
...

建议的解决方案

\path   (in)    edge ["$w(t)$"] (A-1)
        (A-1)   edge ["$e(t)$"] (A-2)
...
  • 对于边缘标签使用quotes

完全的姆韦是:

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, 
                calc, chains, 
                positioning, 
                quotes}

\begin{document}
    \begin{tikzpicture}[auto,
node distance = 1cm and 1.5cm,
  start chain = A going right,
   sum/.style = {circle,
                 draw,
                 inner sep=0mm, minimum size=4mm,
                 node contents={}
                 },
 block/.style = {rectangle, 
                 draw,    
                 minimum height = 2em,
                 minimum width = 4em,
                 inner sep=2mm, outer sep =0mm
                 },
branch/.style = {circle,
                 inner sep=0mm, minimum size=2mm,
                 fill = black,
                 node contents={}
                 },
every edge/.append code = {%
\global\let\currenttarget\tikztotarget % save \tikztotarget in a global variable
\pgfkeysalso{append after command={(\currenttarget)}}% automatically repeat it
                           },
every edge/.append style = {-Straight Barb},
                     ]
%placing the blocks
    \coordinate (in);
    \begin{scope}[every node/.append style={on chain=A}]
\node [sum, right=of in];           % A-1
\node [block]   {Regler};
\node [block]   {Strecke, $x(t)$};
\node [sum];
\node [branch];                     % A-5
    \end{scope};
\coordinate [right=of A-5]  (out);
\node (A-6) [block, below = of $(A-2.south)!0.5!(A-3.south)$]   {Messglied};
    \coordinate[above = of A-4]   (z);
\node (A-7) [sum, at = {(A-6 -| A-5)}];
    \coordinate[right=of A-7]   (m);
% conections
\path   (in)    edge ["$w(t)$"] (A-1)
                edge ["$e(t)$"] (A-2)
                edge ["$u(t)$"] (A-3)
                edge            (A-4)
                edge            (A-5)
                edge ["$y(t)$"] (out)
        (z)     edge ["$z(t)$"] (A-4)
        (A-5)   edge            (A-7)
        (m)     edge ["$m(t)$" ']   (A-7)
                edge            (A-6);
\draw[-Straight Barb]    
        (A-6) -| (A-1) node[below left] {$-$};
  \end{tikzpicture}

\end{document}

相关内容