如何修正这个图表块?

如何修正这个图表块?

在此处输入图片描述

\tikzstyle{block} = [draw, fill=blue!20, rectangle, 
minimum height=3em, minimum width=6em]
\tikzstyle{sum} = [draw, fill=blue!20, circle, node distance=1cm]
\tikzstyle{input} = [coordinate]
\tikzstyle{output} = [coordinate]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]

% The block diagram code is probably more verbose than necessary
\begin{tikzpicture}[auto, node distance=2cm,>=latex']
% We start by placing the blocks
\node [input, name=input] {};
\node [sum, right of=input] (sum) {};
\node [block, right of=sum] (controller) {Controller};
\node [block, right of=controller, pin={[pinstyle]above:Disturbances},
node distance=3cm] (system) {System};
% We draw an edge between the controller and system block to 
% calculate the coordinate u. We need it to place the measurement block. 
\draw [->] (controller) -- node[name=u] {$u$} (system);
\node [output, right of=system] (output) {};
\node [block, below of=system] (measurements) {Measurements};
\node [block, left of=measurements ] (sensor) {Sensor};
\draw [<-] (measurements) -- node[name=k] {$k$} (sensor);
% Once the nodes are placed, connecting them is easy. 
\draw [draw,->] (input) -- node {$r$} (sum);
\draw [->] (sum) -- node {$e$} (controller);
\draw [->] (system) -- node [name=y] {$y$}(output);
\draw [->] (y) |- (measurements);
\draw [->] (sensor) -| node[pos=0.99] {$-$} 
node [near end] {$y_m$} (sum);
\end{tikzpicture}

答案1

替换left of=measurementsbelow=of controller

三个小注释:

  • left of、 etc键right of已弃用。现在建议使用left=of ...代替left of=...etc。这需要\usetikzlibrary{positioning}。此更改不仅仅是字母的换位。新的定位比旧方式更强大、更灵活。

  • 您可以编写block/.style = {...}(作为 的选项tikzpicture或参数\tikzset)而不是\tikzstyle{block} = [...]。据我所知,第一种方法(我在下面也使用了该方法)更符合当前的 tikz 约定。第二种方法是 tikz 其他地方未使用的相当独特的语法。

  • 正如 Zarko 在评论中指出的那样,可以通过指定例如在 x 和 y 方向上选择不同的节点距离node distance=1cm and 2cm

在此处输入图片描述

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}
\tikzset
  {block/.style =
    {draw, fill=blue!20, rectangle, minimum height=3em, minimum width=6em},
   sum/.style = {draw, fill=blue!20, circle, node distance=1cm},
   input/.style = {coordinate},
   output/.style = {coordinate},
   pinstyle/.style = {pin edge={to-,thin,black}}
  }

\begin{document}
% The block diagram code is probably more verbose than necessary
\begin{tikzpicture}[auto, node distance=1cm and 2cm,>=latex']
% We start by placing the blocks
\node [input, name=input] {};
\node [sum, right=of input] (sum) {};
\node [block, right=of sum] (controller) {Controller};
\node [block, right=of controller, pin={[pinstyle]above:Disturbances},
node distance=3cm] (system) {System};
% We draw an edge between the controller and system block to 
% calculate the coordinate u. We need it to place the measurement block. 
\draw [->] (controller) -- node[name=u] {$u$} (system);
\node [output, right=of system] (output) {};
\node [block, below=of system] (measurements) {Measurements};
\node [block, below=of controller] (sensor) {Sensor};
\draw [<-] (measurements) -- node[name=k] {$k$} (sensor);
% Once the nodes are placed, connecting them is easy. 
\draw [draw,->] (input) -- node {$r$} (sum);
\draw [->] (sum) -- node {$e$} (controller);
\draw [->] (system) -- node [name=y] {$y$}(output);
\draw [->] (y) |- (measurements);
\draw [->] (sensor) -| node[pos=0.99] {$-$} 
node [near end] {$y_m$} (sum);
\end{tikzpicture}
\end{document}

答案2

作为补充格诺特(没有那么多)“冗长的”答案,希望它更简洁:)在其中我使用calcquotes库:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows, calc, positioning, quotes}
\tikzset{
     block/.style = {rectangle, draw, fill=blue!20, minimum height=3em, minimum width=6em},
       sum/.style = {circle, draw, fill=blue!20},
 every pin/.style = {pin edge={<-,black}},
                > = latex'
        }

\begin{document}
    \begin{tikzpicture}[auto, 
    node distance = 6mm and 12mm]
% placing the blocks
\coordinate (in);
\node (sum)             [sum, right=of in] {};
\node (controller)      [block, right=of sum]           {Controller};
\node (system)          [block, pin=above:Disturbances, 
                         right=of controller]           {System};
\coordinate[right=of system] (out);
\node (measurement)     [block, below=of controller]    {Measurements};
\node (sensor)          [block, below=of system]        {Sensor};
% edges between blocks
\draw[->]   (in)            edge ["$r$"] (sum)
            (sum)           edge ["$e$"] (controller)
            (controller)    edge["$u$"]  (system)
            (system)        edge["$y$"]  (out)
            (sensor)        edge["$k$"] (measurement)
            (measurement)   -|  (sum)
                            node [pos=0.75] {$y_m$}
                            node [pos=0.95] {$-$};
\draw[->]   ($(system.east)!0.5!(out)$) |- (sensor);
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:改变考虑车牌注释,代码中的注释现在更有意义了。我仍然相信,我建议的“传感器”和“测量”块的顺序是正确的。然而,在这些块是线性的之前,顺序并不重要 :)

答案3

我认为您也可以使用 TikZ matrix

请注意,该arrows库已被弃用。摘自 TikZ 和 PGF 手册第 16.1 段:

备注:arrows 和 arrows.spaced 库已弃用。请改用/另外使用 arrows.meta,它允许您执行旧库提供的所有功能,以及更多功能。但是,旧库仍然有效,您甚至可以混合使用新旧箭头尖(只是,旧箭头尖不能按照本节其余部分所述的方式进行配置;例如,对于乳胶箭头,scale=2 没有效果,而对于乳胶箭头,它会将其大小加倍,正如人们所期望的那样。)

此外,正如 gernot 所说,\tikzset应该用来代替\tikzstyle(见这里)。

\documentclass{book}
\usepackage{tikz}   
\usetikzlibrary{arrows.meta,matrix,positioning}

\begin{document}

\tikzset{%
    block/.style={draw, fill=blue!20, rectangle, 
    minimum height=3em, text width=7em,align=center},
    sum/.style={draw, fill=blue!20, circle},
    pinstyle/.style={pin edge={latex-,thin,black}},
    mylabup/.style={midway,above},
    >=latex,
    }   
\begin{tikzpicture}
    \matrix[column sep=3em, row sep=4ex]{%
    \coordinate (input);
    &
    \node [sum] (sum) {};
    &
    \node [block] (controller) {Controller};
    &
    \node [block, pin={[pinstyle]above:Disturbances}] (system) {System};
    &
    \coordinate (output);
\\
    &
    &
    \node [block] (sensor) {Sensor};
    &
    \node [block] (measurements) {Measurements};
    \\
    };
    \draw [->] (controller) -- node[mylabup] {$u$} (system);
    \draw [->] (measurements) -- node[mylabup] {$k$} (sensor);
    \draw [->] (input) -- node[mylabup] {$r$} (sum);
    \draw [->] (sum) -- node[mylabup] {$e$} (controller);
    \draw [->] (system) -- node [name=y,mylabup] {$y$} (output);
    \draw [->] (y) |- (measurements);
    \draw [->] (sensor) -| 
node [near end, left] {$y_m$} (sum);

\node[below left = -2pt and -1pt of sum] {$-$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容