This notebook illustrates how to draw control block diagrams programmatically using a LaTeX template. So far, you've used $\LaTeX$ to write equations and symbols, but LaTeX can also be a very powerful tool for creating graphical elements. The diagram(s) on this page were generated using LaTeX with some help from the TikZ package.
By the end of this notebook, you should be able to:
A control block diagram provides a graphical description of the flow of signals in a control system. Here's a simple control block diagram with a feedback loop and disturbance.
from IPython.core.display import SVG
SVG(filename='../../assets/diagram.svg')
Unlike typical raster image formats such as JPGs and PNGs, SVG (Scalable Vector Graphics) uses a vector representation of the image data, allowing you to scale the image without losing resolution and sharpness. SVG files are human-readable and can be generated using code.
This Texample tutorial shows how to generate a block diagram using TikZ. We've adapted the code to generate the SVG diagram that you see on this page and used an online tool to convert TeX code to SVG.
Our TikZ code is available in this Overleaf project. Overleaf is a free online LaTeX editor.
To begin, click the 'clone this project' button at the top of the Overleaf page.
The code is self-documented and a live-preview is provided on the side of the screen. You only need to modify the {tikzpicture}
section to change the diagram. Here are some additional tips to get you started:
\node
syntax is \node [type, location] (name) {label}
:\node [block, right of=sum] (controller) {$G_c(s)$};
\draw
syntax is \draw [->] (start_point) -- node {label} (end_point);
\draw [->] (system) -- node {$Y_u$} (sum_disturbance);
The best way to get familiar with the syntax is to play around with the code and see what happens to your diagram.
Once you're done in Overleaf, click the 'PDF' button at the top of the page to save your diagram as a PDF file. To export your diagram as an SVG file, use this free LaTex Previewer tool and paste your code from Overleaf. Use the Download button at the bottom right of your screen and select SVG.
Now, you can upload your SVG diagram to your work folder and use this IPython method to embed it in a Jupyter notebook (after replacing diagram.svg with your filename of course):
from IPython.core.display import SVG
SVG(filename='diagram.svg')
Just like this:
from IPython.core.display import SVG
SVG(filename='../../assets/diagram.svg')
Link: https://github.com/uclmr/egal
Alternatively, you can also use the egal package to do quick and dirty SVG drawings in the Jupyter notebook itself. The results might not be as clean and neat as TikZ but this is an easier way to create diagrams if you just need to illustrate something simple.
To use the egal package, run the commands in the next cell (shift+enter). Go to the 'Control Panel' button on top right corner of your screen and restart your server. You should see an 'Insert Egal Cell' in the toolbar after you restart your server. Click on that to create an egal drawing cell.
!pip install git+https://github.com/uclmr/egal.git --user
!jupyter nbextension install --py egal --user
!jupyter nbextension enable egal --user --py