# How to plot functions with LaTeX

Following on a previous blog post on drawing fractals with LaTeX, today I am going to plot functions using LaTeX. As in the previous post, let’s start by preparing the LaTeX environment from scratch:

The pgfplots package for plotting functions and the standalone package for displaying the graph in a single document need to be installed manually as they are not currently present in the Ubuntu repositories:

To get started I am going to plot two simple functions which can be mathematically expressed as follows:

The first function is a linear function whose graph is a straight line. The second one is a quadratic function whose graph is a parabola. Here is the code to plot them with LaTeX:

To compile the tex file into pdf from the command line you can use the latexmk utility previously installed and open the generated file with your preferred PDF editor (I use evince here).

The plotted functions look like this (and like this in PDF):

The next graph is obtained by joining a sequence of points whose coordinates on the x and y axis are clearly visible in the code (this example is adapted from the beautiful PGFPlots Gallery):

Again, I use latexmk to compile the code and evince to open the PDF.

The plotted function looks like this (and like this in PDF):

Finally, I’m going to draw a saddle surface. This is an example of hyperbolic geometry, a non-Eucledian geometry where the postulate of parallel lines doesn’t hold. Popular examples of hyperbolic geometry are the Pringles crisps and print works by the Dutch artist M. C. Escher. So here is the LaTeX code to generate a saddle surface:

As usual, I compile it with latexmk and open it with evince.

And here is the beautiful saddle surface (and here is the associated PDF):

Sources:

Mihalis Tsoukalos, LaTeX: Make text beautiful, Linux Format 201, Summer 2015

http://pgfplots.sourceforge.net/gallery.html

# How to draw fractals in LaTeX

Fractals are mathematical sets showing a repeating pattern at every scale. Fractal patterns are fascinating and are commonly found in nature, for example in cauliflowers, broccoli, lungs, or trees.

LaTeX can generate complex fractals with few lines of code but needs some packages to do that. So let’s prepare the LaTeX environment from scratch by installing all the necessary packages (and prepare a cup of coffee in the meantime):

The standalone package needs to be installed manually as it is not currently present in the Ubuntu repositories:

I noticed that PDF readers like evince and okular do not always show the generated fractals in high definition whereas Adobe Reader always does. If you experience the same issue install Adobe Reader:

The first fractal I am going to draw is the Mandelbrot set. Here is the LaTeX code using the tikz package:

To compile the tex file from the command line you can use the latexmk utility  previously installed.  I noticed that evince and okular don’t show this fractal in high definition so it’s better to open it with Adobe Reader.

The fractal looks like this (and like this in PDF):

Another approach is to use the pst-fractal package based on  the PSTricks package which is more powerful (but slower to compile) than the tikz package. Here is the code using pst-fractal:

You should compile it with xelatex and wait a bit more until you get the message Output written on filename.pdf (1 page) . This time you can open it in high definition also with evince or okular:

And this is the result in full colour (here is the PDF):

I am now going to draw the Julia set. Again, I use the pst-fractal package. Here is the LaTeX code:

Compile it with xelatex as usual (and go for a cup of tea):

Here is the result (and here is the PDF):

Sources:

Mihalis Tsoukalos, LaTeX: Make text beautiful, Linux Format 201, Summer 2015

http://tex.stackexchange.com/questions/39474/generate-mandelbrot-images-using-tikz