15

I'm trying to recreate this diagram:

enter image description here

Mine already looks pretty good but the connection between the Environment and Sensornode lacks the 2nd right angle and space for the measured by text.

enter image description here

How can I make add the necessary space to the node?

Also, is it possible to load a tikz-diagram from an external file?

This is the code for a MWE (You can play with it here https://www.overleaf.com/3051741jzxtvm#/8435913/)

\documentclass[tikz, border=10pt]{standalone}
\usepackage{verbatim}

\tikzset{
    vertex/.style = {
        circle,
        fill            = black,
        outer sep = 2pt,
        inner sep = 1pt,
    }
}
\begin{document}
\begin{tikzpicture}[node distance = 2cm, auto]
  [
    post/.style={->,shorten >=1pt,semithick}
  ]

  % Nodes
  \node[draw] (Sensor) at (2,0) {Sensor};
  \node[draw] (Comparator) at (6,0) {Comparator};
  \node[draw] (Actuator) at (10,0) {Actuator};
  \node[draw] (Feedback) at (13,2) {Feedback};
  \node[draw] (Environment) at (6,-2) {Environment};
  \node[draw] (Disturbances) at (6,-4) {Disturbances};

  \node at (1, 1) {\textbf{ Input}};
  \node at (11, 1) {\textbf{ Output}};

  \draw[->,draw=red] (Sensor) to node {alerts}  (Comparator);
  \draw[->,draw=red] (Comparator) to node {drives}  (Actuator);

  \draw[-, draw=red] (Actuator) -| node[pos=0.25] {affects} (Feedback);
  \draw[->, draw=red] (Feedback) |- (Environment);
  \draw[->, draw=red] (Environment) -| node {measured by}  (Sensor);
  \draw[<-,draw=red] (Environment) to node {affect}  (Disturbances);



\end{tikzpicture}
\end{document}
Hedge
  • 969

3 Answers3

13

An alternative, which use tikzlibrary chains and positioning:

\documentclass[tikz, border=10pt]{standalone}
        \usetikzlibrary{chains,positioning}
    \usepackage{verbatim}

    \begin{document}
        \begin{tikzpicture}[
    node distance = 2cm, auto,
      start chain = going right,
       box/.style = {rectangle, draw, on chain}]
    % Nodes
      \node[box] (Sensor)       {Sensor};
      \node[box] (Comparator)   {Comparator};
      \node[box] (Actuator)     {Actuator};
      \node[box,above right=of Actuator] (Feedback)     {Feedback};
      \node[box,below=of Comparator] (Environment)      {Environment};
      \node[box,below=of Environment] (Disturbances)    {Disturbances};
    % Lines  
      \draw[->,draw=red] (Sensor)     to node {alerts}  (Comparator);
      \draw[->,draw=red] (Comparator) to node {drives}  (Actuator);
      \draw[-, draw=red] (Actuator) -| node[pos=0.25] {affects} 
                                       node[pos=0.25,above=1cm] {\textbf{ Output}}   
                                       (Feedback);
      \draw[->,draw=red] (Feedback) |- (Environment);
      \draw[->,draw=red] (Environment) -| ([xshift=-2.4cm] Sensor.west) -- 
                                node[above] {measured by}  
                                node[above=1cm] {\textbf{ Input}}   (Sensor.west);
      \draw[<-,draw=red] (Environment) to node {affect}  (Disturbances);
        \end{tikzpicture}
    \end{document}

enter image description here

Zarko
  • 296,517
13

Taken from Zarko's example code

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{chains,positioning}
\usepackage{verbatim}
\usepackage{xcolor}
\DefineNamedColor{named}{BrickRed}      {cmyk}{0,0.89,0.94,0.28}
\DefineNamedColor{named}{DarkRed}       {cmyk}{0.4,0.89,0.94,0.28}
\renewcommand{\familydefault}{\sfdefault}


\begin{document}
\begin{tikzpicture}[
  node distance = 2cm, auto,
  start chain = going right,
  box/.style = {rectangle, draw, on chain}]
  \tikzset{>=latex}
  \tikzstyle{nodebox}=[box,draw=none,font=\bf]
  \tikzstyle{redline}=[-,BrickRed,draw=BrickRed,ultra thick]
  \tikzstyle{redliner}=[redline,->]
  \tikzstyle{redlinel}=[redline,<-]
  \tikzstyle{darklabel}=[below,DarkRed]

  % Nodes
  \node[nodebox] (Sensor)       {Sensor};
  \node[nodebox] (Comparator)   {Comparator};
  \node[nodebox] (Actuator)     {Actuator};
  \node[nodebox,above right=of Actuator] (Feedback)     {Feedback};
  \node[nodebox,below=of Comparator] (Environment)      {Environment};
  \node[nodebox,below=of Environment] (Disturbances)    {Disturbances};

  % Lines  
  \draw[redliner] (Sensor)     to node[darklabel] {alerts}  (Comparator);
  \draw[redliner] (Comparator) to node[darklabel] {drives}  (Actuator);
  \draw[redline] (Actuator) -| node[darklabel,pos=0.25] {affects} 
  node[pos=0.25,above=1em,DarkRed,font=\bf] {Output} (Feedback);
  \draw[redliner] (Feedback) |- (Environment);
  \draw[redliner] (Environment) -| ([xshift=-2.4cm] Sensor.west) -- 
  node[darklabel] {measured by}  
  node[above=1em,DarkRed,font=\bf] {Input}   (Sensor.west);
  \draw[redlinel] (Environment) to node[DarkRed] {affect}  (Disturbances);
\end{tikzpicture}

\end{document}

enter image description here

Pål GD
  • 889
9

Here is a quick-hack:

\draw[->, draw=red] (Environment) -| ($(Sensor)+(-3,0)$) 
  |- node[anchor=south west] {measured by} (Sensor);

you need to add \usetikzlibrary{calc} to the preamble.

($(Sensor)+(-3,0)$) adds an additional coordinate to the path. (-3,0) is the offset from the (Sensor) node.

Update (inspired by Zarko's answer)

Instead of ($(Sensor)+(-3,0)$) you can use ([xshift=-2.4cm] Sensor.west). No need to include additional tikz-libraries.

enter image description here

sergej
  • 6,461
  • 32
  • 62