LaTeX Draw A Graph Using LaTeX - Baeldung On Computer Science
LaTeX Draw A Graph Using LaTeX - Baeldung On Computer Science
(/cs/) (https://www.baeldung.com/cs/)
1. Overview
100% AUTHENTIC, Forest Essentials Essentials For Him | Sandalwood &…
PURE, POTENT &
In this tutorial, we’llLUXURIOUS
discuss how to draw a graph using LaTeX.
481
AYURVEDIC
₹
1,850
00
https://www.baeldung.com/cs/latex-drawing-graphs 1/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
We’ll first start by listing the main LaTeX packages that we can use for
graphs, and express their particular advantages.
Then, we’ll study some examples of graphs drawn with those packages,
and also their variations.
At the end of this tutorial, we’ll be able to implement a graph in a LaTeX
document.
https://www.baeldung.com/cs/latex-drawing-graphs 2/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
If the vertex has a label, this is typed inside the circle, as is the case with
the number 1 in the example above. We can also represent multiple
vertices by placing them in a non-overlapping manner anywhere in the
image:
https://www.baeldung.com/cs/latex-drawing-graphs 3/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
And lastly, if the graph has loops, we can represent them as edges that
connect a vertex to itself:
https://www.baeldung.com/cs/latex-drawing-graphs 4/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
3. Tikz
We can now discuss the packages that we can use to draw graphs in
LaTeX. The most common LaTeX package used for drawing, in general,
is TikZ (https://en.wikipedia.org/wiki/PGF/TikZ), which is a layer over
PGF (https://github.com/pgf-tikz/pgf) that simplifies its syntax. TikZ is a
powerful package that comes with several libraries dedicated to specific
tasks, such as:
Drawing mindmaps (https://github.com/pgf-
tikz/pgf/blob/9ac32f11fd2f8255a8a74b0994cb9aed5c42a3e7/d
https://www.baeldung.com/cs/latex-drawing-graphs 5/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
oc/generic/pgf/text-en/pgfmanual-en-library-mindmaps.tex),
useful for depicting conceptual relationships
Diagrams for entity-relationships (https://github.com/pgf-
tikz/pgf/blob/03aa54d26fe2a1d6130c65240d41262961005d86
/doc/generic/pgf/text-en/pgfmanual-en-library-er.tex), which
can assist us in representing knowledge graphs (/cs/ml-
knowledge-graphs)
Drawing logical circuits (https://github.com/pgf-
tikz/pgf/blob/03aa54d26fe2a1d6130c65240d41262961005d86
/doc/generic/pgf/text-en/pgfmanual-en-library-circuits.tex),
that are useful to represent operations in Boolean algebra
(/cs/boolean-algebra-basic-laws)
And also, relevant for our purposes, it contains a graphdrawing
(https://github.com/pgf-
tikz/pgf/blob/03aa54d26fe2a1d6130c65240d41262961005d86/doc/ge
neric/pgf/text-en/pgfmanual-en-gd-usage-tikz.tex) library that we can
use for the automatic drawing of graphs. TikZ is the standard package
we use to draw graphs, and we’ll dedicate it to most of the coded
examples.
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[main/.style = {draw, circle}]
\node[main] (1) {$x_1$};
\end{tikzpicture}
\end{document}
The word main here refers to a style that we define when introducing
the tikzpicture environment. In this case, we use it to tell the compiler to
draw the shape of a circle for the nodes.
The most important command in the code above is \node. Its syntax is
this:
We can use this command repeatedly to add more vertices to the graph:
Notice how the term between the second pair of square brackets lets us
define the position of the nodes in relation to other nodes. We can do
this by using the keywords right, left, above, below, followed by a blank
space, and then the keyword of and the label of the node to which they
refer. This is particularly useful if we later need to rearrange the graph on
https://www.baeldung.com/cs/latex-drawing-graphs 7/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
If instead, we want to add an arrow for a directed edge, we can give the
parameter -> to the \draw command:
https://www.baeldung.com/cs/latex-drawing-graphs 8/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
If the vertices appear too close to one another, we can also specify the
parameter node distance to the tikzpicture environment to spread them
further:
https://www.baeldung.com/cs/latex-drawing-graphs 9/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
(https://ads.freestar.com/?
di & t di l L d& t b ld & t t t
It doesn’t look good. Instead, we want to loop around the graph, from
the outside and above node 2, and then descend back towards the
graph. Luckily, we can do that by specifying a parameter looseness after
declaring the initial point of a line. To better clarify where we want the
https://www.baeldung.com/cs/latex-drawing-graphs 10/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
edge to go, we also specify the angles out and in which indicate,
respectively, the angle of the outgoing and ingoing edges (/cs/graph-
theory-intro#5-endpoints-directions-loops):
3.5. Loops
We can use the same technique to draw loops (/cs/graph-theory-
intro#5-endpoints-directions-loops) in the graph, by indicating twice the
same node as the starting and ending points of a loose line:
(https://ads.freestar.com/?
di & t di l L d& t b ld & t t t
https://www.baeldung.com/cs/latex-drawing-graphs 12/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
And finally, we can repeat all the instructions above to populate the
graph with all edges we need:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[node distance={15mm}, thick, main/.style =
{draw, circle}]
\node[main] (1) {$x_1$};
\node[main] (2) [above right of=1] {$x_2$};
\node[main] (3) [below right of=1] {$x_3$};
\node[main] (4) [above right of=3] {$x_4$};
\node[main] (5) [above right of=4] {$x_5$};
\node[main] (6) [below right of=4] {$x_6$};
\draw[->] (1) -- (2);
\draw[->] (1) -- (3);
\draw (1) to [out=135,in=90,looseness=1.5] (5);
\draw (1) to [out=180,in=270,looseness=5] (1);
\draw (2) -- (4);
\draw (3) -- (4);
\draw (5) -- (4);
\draw[->] (5) to [out=315, in=315, looseness=2.5] (3);
\draw[->] (6) -- node[midway, above right, sloped, pos=1] {+1}
(4);
\end{tikzpicture}
\end{document}
4. Neuralnetwork
https://www.baeldung.com/cs/latex-drawing-graphs 13/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
\begin{neuralnetwork}
...
\end{neuralnetwork}
https://www.baeldung.com/cs/latex-drawing-graphs 14/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
The parameter count indicates the number of nodes in the given layer.
The term bias indicates instead whether the bias (/cs/neural-networks-
bias) for that layer should be counted as a separate element. In that
case, this bias wouldn’t be connected to the previous layer, when links
are established. Lastly, the class of the node consists of either one of the
predefined classes input node, hidden node, or output node, or also any
of our custom classes.
https://www.baeldung.com/cs/latex-drawing-graphs 15/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
\documentclass{article}
\usepackage{neuralnetwork}
\begin{document}
\begin{neuralnetwork}
\inputlayer[count=3, bias=false]{}
\hiddenlayer[count=2]{}
\outputlayer[count=1]{}
\end{neuralnetwork}
\end{document}
...
\inputlayer[count=3, bias=false]{} \linklayers
\hiddenlayer[count=2]{} \linklayers
\outputlayer[count=1]{}
...
Notice how the yellow node, indicating the bias term in the intermediate
layer, isn’t connected to the input layer. We can also add labels to the
nodes, though this is somewhat tricky. The way to do it is by defining a
new LaTeX command, and passing it as a parameter text to a layer:
https://www.baeldung.com/cs/latex-drawing-graphs 16/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
...
\newcommand{\x}[2]{$x_#2$}
\inputlayer[count=3, bias=false, text=\x]
...
We can also follow the same criterion with the hidden layer and the
output layer by creating appropriate commands:
...
\newcommand{\h}[2]{$h_#2$}
\newcommand{\y}[2]{$y_#2$}
...
\hiddenlayer[count=2, text=\h]{} \linklayers
\outputlayer[count=1, text=\y]{} \linklayers
...
https://www.baeldung.com/cs/latex-drawing-graphs 17/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
The bias term in the hidden layer automatically takes the index of , as
is common in the literature, without having to specify any particular
instructions.
4.4. Weights
We can also add weights to the edges by using a formula like the one
below. We have to define a new command first, and then assign it to the
default text for links:
\newcommand{\w}[4] {$w_{#2,#4}$}
\setdefaultlinklabel{\w}
Here, #1 indicates the layer of origin, #2 the node of origin, and #3 and
#4 indicate layer and node of destination, respectively. Notice that, while
the weights of the hidden layer look normal, some weights in the
previous layer are however missing.
This is because they’re automatically rewritten by some other weights,
and as a consequence, we only see some of them. The only way to
address this problem is by defining all links individually and then giving
them the appropriate label. We can do this by using the \link command,
instead of \linklayers:
...
\inputlayer[count=3,bias=false, text=\x]{}
\hiddenlayer[count=2, text=\h]{}
\link[style={}, labelpos=near start, from layer=0, from node=1,
to layer=1, to node=1]
...
https://www.baeldung.com/cs/latex-drawing-graphs 18/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
The parameter labelpos is the one we use to shift towards the node of
origin. In this case, the label is assumed by the edge. This, in turn, avoids
the overlapping between the edges’ labels.
\foreach \n in {1,...,3}{
\foreach \m in {1,2}{
\link[style={}, labelpos=near start, from layer=0, from
node=\n, to layer=1, to node=\m]
}
}
https://www.baeldung.com/cs/latex-drawing-graphs 19/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
\documentclass{article}
\usepackage{neuralnetwork}
\begin{document}
\begin{neuralnetwork}
\newcommand{\x}[2]{$x_#2$}
\newcommand{\y}[2]{$y_#2$}
\newcommand{\h}[2]{$h_#2$}
\newcommand{\w}[4] {$w_{#2,#4}$}
\setdefaultlinklabel{\w}
\inputlayer[count=3, bias=false, text=\x]
\hiddenlayer[count=2, text=\h]
\foreach \n in {1,...,3}{
\foreach \m in {1,2}{
\link[style={}, labelpos=near start, from layer=0, from
node=\n, to layer=1, to node=\m]
}
}
\outputlayer[count=1, text=\y] \linklayers
\end{neuralnetwork}
\end{document}
There’s also another option to mention that relates not so much to LaTeX
packages themselves but, instead, to the automatic conversion into
LaTeX code of graphs created in other graphical editors. This is
particularly convenient if we’re used to drawing the graphs in other
programming languages, and we’re still learning how to draw graphs in
LaTeX.
https://www.baeldung.com/cs/latex-drawing-graphs 20/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
There are, in fact, libraries or extension for the automatic conversion into
LaTeX code of the graphical output of another editor:
In Python, we can convert graphs created with matplotlib
(https://matplotlib.org/) into LaTeX, thanks to the library
tikzplotlib (https://github.com/nschloe/tikzplotlib)
In MatLab, we can use the scripts from matlab2tikz
(https://www.mathworks.com/matlabcentral/fileexchange/22
022-matlab2tikz-matlab2tikz) to perform a similar operation
In Blender 2.4, we can use blend2tikz
(https://github.com/xyz2tex/blend2tikz) to export Blender
curves into LaTeX code
6. Conclusion
In this article, we studied how to draw graphs using LaTeX.
2 COMMENTS Oldest
View Comments
https://www.baeldung.com/cs/latex-drawing-graphs 21/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
CATEGORIES
ALGORITHMS (/CS/CATEGORY/ALGORITHMS)
ARTIFICIAL INTELLIGENCE (/CS/CATEGORY/AI)
CORE CONCEPTS (/CS/CATEGORY/CORE-CONCEPTS)
DATA STRUCTURES (/CS/CATEGORY/DATA-STRUCTURES)
GRAPH THEORY (/CS/CATEGORY/GRAPH-THEORY)
LATEX (/CS/CATEGORY/LATEX)
NETWORKING (/CS/CATEGORY/NETWORKING)
SECURITY (/CS/CATEGORY/SECURITY)
SERIES
ABOUT
ABOUT BAELDUNG (HTTPS://WWW.BAELDUNG.COM/ABOUT)
THE FULL ARCHIVE (/CS/FULL_ARCHIVE)
EDITORS (HTTPS://WWW.BAELDUNG.COM/EDITORS)
https://www.baeldung.com/cs/latex-drawing-graphs 22/23
9/16/23, 9:30 AM Draw a Graph Using LaTeX | Baeldung on Computer Science
https://www.baeldung.com/cs/latex-drawing-graphs 23/23