How to draw diagrams in latex

Today"s tutorial is around drawing a block diagram in LaTeX using TikZ package. This step-by-action tutorial teaches you how to develop nodes and customize them, place a node through respect to one more one, attach nodes and also draw line arrows
*
Block diagram drawn in LaTeX making use of TikZ package
Drawing a block diagram in LaTeX making use of TikZ package deserve to be completed in 3 steps: Step 1: Create nodes with various shapes (use family member positioning). Step 2: Connect various nodes through their anchors. Step 3: Define styles for the same forms.

You watching: How to draw diagrams in latex


In our instance, we identify two kinds of shapes: a circle and also 3 rectangles. These shapes have the right to be created using ode command. Here is an example:
documentclassstandalone% More identified colorsusepackagexcolor% Required packageusepackagetikzusetikzlibrarypositioningegindocumentegintikzpicture ode (sum) at (0,0);endtikzpictureenddocument
*

The over code creates a node through name (sum) at the point through coordinates (0,0). To draw the node, we have to include the alternative draw to it and via circle alternative, we obtain a circle shape. By default, a node corresponds to a rectangle.We offered likewise the minimum dimension of the node which is equal to 0.6cm. It must be detailed that adding details to a node (text, picture or even a tikz code) increases its size. The node is filled with the shade Rhodamine!50 which is just one of the dvipsnames gave by xcolor package. For a complete list, examine the post: Precharacterized LaTeX colors: dvipsnames.

See more: Book Review Silence Shusaku Endo, Book Review


You might wonder: why we don"t use draw command also to draw a circle following the syntax: attract (0,0) circle(0.6cm); Without a doubt, this is doable using attract command but a node has an interesting function which coincides to anchors, it represents the works with of the node border.
Each node has actually anchors which corresponds to points works with of the node border. A function that is not offered by attract command also through circle shape. Different anchors are displayed in the following illustration wright here we use the name of the node and also an angle or we usage predefined anchors such as (north, south, east, west, etc)
*

Now, we have the right to draw quickly the sum form of the block diagram by connecting the suggest (sum.north east) with the suggest (sum.south west) with a straight line utilizing draw command also. The very same for the points via collaborates (sum.south east) and also (amount.north west). Here is the code:
documentclassstandalone% More defined colorsusepackagexcolor% Required packageusepackagetikzusetikzlibrarypositioningegindocumentegintikzpicture% Sum shape ode (sum) at (0,0);draw (sum.north east) -- (amount.southern west)(amount.north west) -- (amount.south east);endtikzpictureenddocument
*

Now, it continues to be to add the positive and negative signs. We deserve to location them as nodes family member to the center of the sum node. Here is the matching item of code:
*

We added the alternative left= to put the node at the left of the provided coordinates (amount.center) via an balance out equals to the offered value. The exact same for the negative authorize which is located below the center of the amount node.
Now, we would certainly prefer to develop the controller block which coincides to a rectangle filled with a golden shade and also has the label C(s). This have the right to be achieved following the same procedures as above wright here we will create a node via draw option and also specify its size. To avoid placing the at certain coordinates (x,y), we will certainly use family member placing via respect to the amount node. Check the adhering to code:


documentclassstandalone% More characterized colorsusepackagexcolor% Required packageusepackagetikzusetikzlibrarypositioningegindocumentegintikzpicture% Sum shape ode (sum) at (0,0);attract (sum.north east) -- (amount.south west)(sum.north west) -- (amount.south east);attract (amount.north east) -- (sum.southern west)(amount.north west) -- (sum.southern east); ode at (amount.center) iny $+$; ode at (sum.center) iny $-$;% Controller ode (controller) $C(s)$;endtikzpictureenddocument
The over code creates a node via rectangular form (default form no need to specify it) which has a minimum width =2cm and also minimum height=2cm. The node is situated at the right of the amount node with 1cm distance. This is achieved using the alternative right=1cm of sum. We saved the rectangle node through the name (controller) which will be offered for relative positioning of the device block H(s). Here is the acquired illustration:
Following the same measures as over, we can develop the mechanism and also the sensor blocks. Here is the equivalent code:
documentclassstandalone% More identified colorsusepackagexcolor% Required packageusepackagetikzusetikzlibrarypositioningegindocumentegintikzpicture% Sum shape ode (sum) at (0,0);draw (amount.north east) -- (amount.southern west)(sum.north west) -- (amount.south east);draw (amount.north east) -- (amount.south west)(sum.north west) -- (amount.south east); ode at (amount.center) iny $+$; ode at (sum.center) iny $-$;% Controller ode (controller) $C(s)$;% System H(s) ode (system) $H(s)$;% Sensor block ode (sensor) $G(s)$;endtikzpictureenddocument
The sensor is positioned at the right and also below of the controller node making use of the option: listed below right= 1cm and also -0.25cm of controller. It continues to be to connect these blocks with arrows.
This is the simplest part in illustration a block diagram in LaTeX. We have to attach anchors of various nodes. Let"s begin by illustration an arrowhead in between the amount and also the controller blocks:
This line of code draws a line from (sum.east) to (controller.west). The line ends with an arrowhead of stealth form (For even more arrow tips, inspect the postExploring TikZ Arrows) . We included a label to the arrow using a node command, the label is positioned at the above the middle of the distance in between two blocks. This has been achieved making use of midmeans choice. The following table provides more details about positioning along a path:
For the output of the system, we will certainly draw an arrow with 1.25cm length and also we conserve the node at the middle of the arrow to use it for connecting the output via sensor block. Here is the equivalent code:
The node is named (output) and also we will certainly draw a line from its center (output.center) to the (sensor.east) via a perpendicular lines. To accomplish the later on, we use |- (vertical then straight line) rather of --. 
draw<-stealth> (output.center) |- (sensor.east);draw<-stealth> (sensor.west) -| (sum.south) node$y_m$;
The west anchor of the sensor is associated with the southern of the sum node utilizing the route procedure -| (directly then vertical line).The input reference coincides to a labeled arrowhead which have the right to be attracted with the same manner as the output arrow.Here is the Final code of the latex block diagram:
documentclassstandalone% More defined colorsusepackagexcolor% Required packageusepackagetikzusetikzlibrarypositioningegindocumentegintikzpicture% Sum shape ode (sum) at (0,0);attract (sum.north east) -- (sum.south west)(amount.north west) -- (sum.south east);draw (amount.north east) -- (amount.south west)(amount.north west) -- (sum.south east); ode at (sum.center) iny $+$; ode at (amount.center) iny $-$;% Controller ode (controller) $C(s)$;% System H(s) ode (system) $H(s)$;% Sensor block ode (sensor) $G(s)$;% Arrows through message labeldraw<-stealth> (amount.east) -- (controller.west)node$e$;draw<-stealth> (controller.east) -- (system.west) node$u$;draw<-stealth> (mechanism.east) -- ++ (1.25,0) node(output)node$y$;draw<-stealth> (output.center) |- (sensor.east);draw<-stealth> (sensor.west) -| (sum.south) node$y_m$;draw (amount.west) -- ++(-1,0) node$y_ref$;endtikzpictureenddocument