如何在tikz中相對定位?

如何在tikz中相對定位?

我正在嘗試用 tikz 繪製一些控制圖,對此我有幾個問題:

  • 怎樣才能排除邊境的干擾?
  • 如何左右定位成本和約束節點?

在此輸入影像描述

\tikzstyle{controller} = [draw, fill=blue!20, rectangle, 
minimum height=3em, minimum width=6em]
\tikzstyle{block} = [draw, fill=yellow!20, rectangle, 
minimum height=3em, minimum width=6em]

\tikzstyle{disturbance} = [draw, node distance=1.5cm, line width=0pt]    
\tikzstyle{sum} = [draw, circle, node distance=1.5cm]
\tikzstyle{input} = [coordinate]
\tikzstyle{output} = [coordinate]
\begin{figure}[!]
\centering
\begin{tikzpicture}[auto, node distance=3cm,>=latex', scale=0.5 ,every
node/.style={transform shape}]      
    % We start by placing the blocks
    \node [input, name=input] {};
    \node [sum, right of=input] (sum) {};
    \node [controller, right of=sum] (controller) {Controller};
    \node [block, right of=controller, node distance=4cm] (system) {System};
    \node [disturbance, name=disturbance, above of=system] {Disturbance};
    \node [disturbance, name=costfunc, below of=system,] {Cost};
    \node [disturbance, name=constraint, below of=system] {Constraints};
    % 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(n)$} (system);
    \node [output, right of=system] (output) {};        

    % Once the nodes are placed, connecting them is easy. 
    \draw [draw,->] (input) -- node {$r(n)$} (sum);
    \draw [->] (sum) -- node {$e(n)$} (controller);
    \draw [->] (disturbance) -- (system);
    \draw [->] (system) -- node [name=y] {$y(n)$}(output);
    \draw [->] (y) |-  ($(y.south) + (0,-2)$) -| node[pos=0.99]
    {$-$} node [near end] {$y(n)$} (sum);
\end{tikzpicture}   
\label{fig:mpc_bloc_diagram}
\end{figure}

編輯

在此輸入影像描述

我怎樣才能更接近y(n)Predict rectangle與 對齊u(n)

我嘗試了以下操作,結果如上。

\draw [->] (y) |- node [near end] {$y(n)$} ($(predict.east)+(0,0.35)$);
\draw [->] (u) |- node [near end] {$u(n)$} ($(predict.east)+(0,-0.35)$);

答案1

  1. draw=none。或者,定義一個新的節點樣式而不繪製邊框?

  2. 這裡有很多可能性,取決於所需的結果。例如below right,您可以使用 ,below left並使用錨點、移位來微調定位。如果positioning庫已加載,您可以使用類似的內容below left=10pt and 20pt of <name>(請參閱下面的第二個範例程式碼)。

代碼:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}

\begin{document}

\tikzstyle{controller} = [draw, fill=blue!20, rectangle, 
minimum height=3em, minimum width=6em]
\tikzstyle{block} = [draw, fill=yellow!20, rectangle, 
minimum height=3em, minimum width=6em]

\tikzstyle{disturbance} = [draw, node distance=1.5cm, line width=0pt]    
\tikzstyle{sum} = [draw, circle, node distance=1.5cm]
\tikzstyle{input} = [coordinate]
\tikzstyle{output} = [coordinate]
\begin{figure}
\centering
\begin{tikzpicture}[auto, node distance=3cm,>=latex', scale=1.5 ,every
node/.style={transform shape}]      
    % We start by placing the blocks
    \node [input, name=input] {};
    \node [sum, right of=input] (sum) {};
    \node [controller, right of=sum] (controller) {Controller};
    \node [block, right of=controller, node distance=4cm] (system) {System};
    \node [disturbance, draw=none,name=disturbance, above of=system] {Disturbance};
    \node [disturbance, name=costfunc, below left of=system,] {Cost};
    \node [disturbance, name=constraint, below right of=system] {Constraints};
    % 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(n)$} (system);
    \node [output, right of=system] (output) {};        

    % Once the nodes are placed, connecting them is easy. 
    \draw [draw,->] (input) -- node {$r(n)$} (sum);
    \draw [->] (sum) -- node {$e(n)$} (controller);
    \draw [->] (disturbance) -- (system);
    \draw [->] (system) -- node [name=y] {$y(n)$}(output);
    \draw [->] (y) |-  ($(y.south) + (0,-2)$) -| node[pos=0.99]
    {$-$} node [near end] {$y(n)$} (sum);
\end{tikzpicture}   
\label{fig:mpc_bloc_diagram}
\end{figure}

\end{document}

索姆評論:

  1. 請從使用圖書館改為of=使用圖書館。=ofpositioning

  2. 坎格 從\tikzstyle\tikzset

  3. figure你不能像在裡面那樣放置一個浮動tikzpicture;你可以在其他地方做。

  4. 你的比例因子似乎有點低。

代碼:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,positioning}

\tikzset{
controller/.style={
  draw, 
  fill=blue!20, 
  rectangle, 
  minimum height=3em, 
  minimum width=6em
  },
block/.style={
  draw, 
  fill=yellow!20, 
  rectangle, 
  minimum height=3em, 
  minimum width=6em
  },
disturbance/.style={
  draw, 
  line width=0pt
  },    
sum/.style={
  draw, 
  circle, 
  node distance=1.5cm
  },
input/.style={coordinate},
output/.style={coordinate}
}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[
  auto, 
  node distance=1.5cm,
  >=latex', 
  scale=0.5 ,
  every node/.style={transform shape}
]      
    % We start by placing the blocks
    \node [input, name=input] {};
    \node [sum, right =of input] (sum) {};
    \node [controller, right =of sum] (controller) {Controller};
    \node [block, right =of controller, node distance=4cm] (system) {System};
    \node [disturbance, draw=none,name=disturbance, above = 20pt of system] {Disturbance};
    \node [disturbance,draw=none,name=costfunc, below left = 20pt and 0pt of system,anchor=east] {Cost};
    \node [disturbance,draw=none,name=constraint, below right = 20pt and 15pt of system,anchor=east] {Constraints};
    % 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(n)$} (system);
    \node [output, right =of system] (output) {};        

    % Once the nodes are placed, connecting them is easy. 
    \draw [draw,->] (input) -- node {$r(n)$} (sum);
    \draw [->] (sum) -- node {$e(n)$} (controller);
    \draw [->] (disturbance) -- (disturbance|-system.north);
    \draw [->] (system) -- node [name=y] {$y(n)$}(output);
    \draw [->] (y) |-  ($(y.south) + (0,-2)$) -| node[pos=0.99]
    {$-$} node [near end] {$y(n)$} (sum);
\end{tikzpicture}   
\label{fig:mpc_bloc_diagram}
\end{figure}

\end{document}

在此輸入影像描述

對於編輯原始問題後的新問題:想法是為第一個標籤命名一個節點,然後使用垂直座標系將另一個標籤放置在相同的 x 座標處。以下範例程式碼說明了這一點:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,positioning}

\tikzset{
controller/.style={
  draw, 
  fill=blue!20, 
  rectangle, 
  minimum height=3em, 
  minimum width=6em
  },
block/.style={
  draw, 
  fill=yellow!20, 
  rectangle, 
  minimum height=3em, 
  minimum width=6em
  },
disturbance/.style={
  draw, 
  line width=0pt
  },    
sum/.style={
  draw, 
  circle, 
  node distance=1.5cm
  },
input/.style={coordinate},
output/.style={coordinate}
}

\begin{document}

\begin{tikzpicture}[
  auto, 
  node distance=1.5cm,
  >=latex', 
  every node/.style={transform shape}
]      
  \node [controller] (nodea) {Some node a};
  \node [controller, right =of nodea] (nodeb) {Some node b};
  \node [controller, above left =of nodea] (nodec) {Some node c};

  \draw [->] 
    (nodea) |- 
      node [near end] (un) {$u(n)$}
  ($(nodec.east)+(0,-0.35)$);
  \draw [->] 
    (nodeb) |- 
      coordinate[near end] (aux) 
  ($(nodec.east)+(0,0.35)$) node[above] at (un|-aux) {$y(n)$};
\end{tikzpicture}   

\end{document}

在此輸入影像描述

相關內容