
\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
被棄用。現在建議使用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
作為補充格爾諾特(沒有那麼多)「詳細」答案,希望它更簡潔:)在其中我使用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}
編輯:改變考慮CarLaTeX註釋,程式碼中的註釋現在更有意義。我仍然相信,我建議的區塊“sensor”和“measuremnt”的順序是正確的。然而,在這些區塊是線性的之前,順序並不重要:)
答案3
我想你也可以使用 TikZ matrix
。
請注意,該arrows
庫已被棄用。摘自 TikZ 和 PGF 手冊,第 7 段。 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}