
\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=measurements
на below=of controller
.
Три небольших замечания:
Клавиши
left of
,right of
etc устарели. Теперь рекомендуется использоватьleft=of ...
вместоleft of=...
etc. Для этого требуется\usetikzlibrary{positioning}
. Это изменение — больше, чем просто перестановка букв. Новое позиционирование более надежное и гибкое, чем старое.Вы можете написать
block/.style = {...}
(как опциюtikzpicture
или как аргумент\tikzset
) вместо\tikzstyle{block} = [...]
. Насколько я понимаю, первый метод (который я также использовал ниже) больше соответствует текущим соглашениям tikz. Второй метод — это довольно своеобразный синтаксис, не используемый больше нигде в tikz.Как отметил Зарко в комментариях, расстояние между узлами можно выбрать по-разному в направлениях 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
В качестве дополнения кгернот(не так уж и много) "подробный" ответ, надеюсь, он более лаконичен :) В нем я использую calc
библиотеку quotes
:
\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}
Редактировать:Изменение с учетомCarLaTeXcomment, комментарии в коде теперь более осмысленны. Я все еще считаю, что предложенный мной порядок блоков "sensor" и "measuremnt" правильный. Однако пока эти блоки не линейны, порядок не важен :)
решение3
Я думаю, вы также можете использовать TikZ matrix
.
Обратите внимание, что arrows
библиотека устарела. Из руководства TikZ & PGF, параграф 16.1:
Примечание: Библиотеки arrows и arrows.spaced устарели. Вместо этого/дополнительно используйте arrows.meta, что позволяет вам делать все, что предлагали старые библиотеки, плюс многое другое. Однако старые библиотеки все еще работают, и вы даже можете смешивать старые и новые наконечники стрелок (только старые наконечники стрелок нельзя настроить способами, описанными в остальной части этого раздела; например, указание scale=2 для латексной стрелки не даст никакого эффекта, тогда как для латексных стрелок это удвоит их размер, как и следовало ожидать.)
Более того, как уже сказал Гернот, \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}