# How to position in tikz relatively?

I am trying to draw some control diagram with tikz and I have a few questions regarding that:

• How can be removed the border from the disturbance?
• How can be left and right position the Cost and Constraints nodes?
\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}


EDITED

How can I move the y(n) closer to the Predict rectangle, to be aligned with u(n)

I tried the following, with the result above.

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

-
For the first question you can use white color, that is, draw=white inside the node's option. – Sigur Mar 23 '14 at 17:11

1. draw=none. Or perhaps, define a new node style without drawing the border?

2. Many possibilities here, depending on the desired result. You can use, for example below right, below left and fine tune positioning using anchors, shifts. If the positioning library has been loaded you can use something like below left=10pt and 20pt of <name> (see second example code below).

The code:

\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}


## Som remarks:

1. Please change from of= to =of using the positioning library.

2. Cahnge from \tikzstyle to \tikzset.

3. You cannot place a float like figure inside tikzpicture; you can do it the other wat around.

4. Your scaling factor seems a little low.

The code:

\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}


For the new question after the edit to the original question: the idea is to name one the node for the first label and then use the perpendicular coordinate system to place the other label at the same x-coordinate. The following example code illustrates this:

\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}


-
Thanks, this is what I am looking for. – OHLÁLÁ Mar 23 '14 at 17:44
I have one more question: how can I put a node at the beginning: node [near start] {$u(n)$}. The near start still put a bit to far from the start. – OHLÁLÁ Mar 23 '14 at 18:31
@run At the beginning of what exactly? Please describe exactly what you are trying to achieve. – Gonzalo Medina Mar 23 '14 at 18:35
I updated the question – OHLÁLÁ Mar 23 '14 at 18:39
@run Please try to keep one question per post. This is not a forum. Follow up questions are welcome. Your updated code is almost useless. Where's the complete code producing that result? – Gonzalo Medina Mar 23 '14 at 18:44