Category Archives: Programming Languages

How to plot functions with LaTeX

Saddle surface

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:

sudo apt-get install latex209-base latex209-bin latexmk texlive-full texlive-math-extra texlive-extra-utils texlive-generic-extra texlive-latex-extra

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:

# navigate to your local texmf folder (could also be ~/texmf or ~/.texmf)
cd ~/.texmf-var
# download the pgfplots package
wget http://sourceforge.net/projects/pgfplots/files/pgfplots/1.7/pgfplots_1.7.tds.zip/download
# unzip it in the texmf folder
unzip pgfplots_1.7.tds.zip -d .
# download the standalone package
wget  http://mirrors.ctan.org/install/macros/latex/contrib/standalone.tds.zip
# unzip it in the texmf folder
unzip standalone.tds.zip -d .
# update the texmf folder
texhash ~/.texmf-var
# remove the zip files
rm standalone.tds.zip pgfplots_1.7.tds.zip

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

y = x * x
y = x / 2

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:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xmax=9,ymax=9,samples=50,grid=major,xlabel={x axis},ylabel={y axis},title={Linear and Quadratic Functions}]
    \addplot[blue, ultra thick](x,x*x);
    \addplot[red, ultra thick](x,x/2);
\end{axis}
\end{tikzpicture}
\end{document}

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).

latexmk -pdf plot_functions.tex
evince plot_functions.pdf &

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

Plotting linear and quadratic functionsThe 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):

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\begin{tikzpicture} 
    \begin{axis}[ xlabel=Cost, ylabel=Error] 
        \addplot[color=red,mark=x] coordinates {
        (2,-2.8559703) 
        (3,-3.5301677) 
        (4,-4.3050655) 
        (5,-5.1413136) 
        (6,-6.0322865) 
        (7,-6.9675052) 
        (8,-7.9377747)
    }; 
    \end{axis} 
\end{tikzpicture}

\end{document}

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

latexmk -pdf plot_points.tex
evince plot_points.pdf &

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

plot pointsFinally, 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:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}

\usepgfplotslibrary{patchplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
    \addplot3[patch,patch refines=3,
		shader=faceted interp,
		patch type=biquadratic] 
    table[z expr=x^2-y^2]
    {
        x  y
        -2 -2
        2  -2
        2  2
        -2 2
        0  -2
        2  0
        0  2
        -2 0
        0  0
    };
    \end{axis}
\end{tikzpicture}
\end{document}

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

latexmk -pdf saddle.tex
evince saddle.pdf &

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

Saddle surface
Saddle surface

Sources:

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

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

How to draw fractals in LaTeX

Mandelbrot set

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):

sudo apt-get install latex209-base latex209-bin latexmk texlive-full texlive-math-extra texlive-extra-utils texlive-generic-extra texlive-latex-extra

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

# navigate to your local texmf folder (could also be ~/texmf or ~/.texmf)
cd ~/.texmf-var
# download the standalone package
wget  http://mirrors.ctan.org/install/macros/latex/contrib/standalone.tds.zip
# unzip it in the texmf folder
unzip standalone.tds.zip -d .
# update the texmf folder
texhash ~/.texmf-var
# remove the zip file
rm standalone.tds.zip

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:

# enable Canonical partners repositories
sudo add-apt-repository "deb http://archive.canonical.com/ $(lsb_release -sc) partner"
# update the repositories
sudo apt-get update
# install Acrobate reader
sudo apt-get install acroread

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

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadings}
\begin{document}
    \tikz\shade[shading=Mandelbrot set](0,0) rectangle (15,15);
\end{document}

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.

latexmk -pdf mandelbrot.tex
acroread mandelbrot.pdf &

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

Mandelbrot set
Mandelbrot set

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:

\documentclass[border=12pt]{standalone}
\usepackage{pst-fractal}
\begin{document}
    \psset{xWidth=8cm, yWidth=6cm}
    \psfractal[type=Mandel, baseColor=red, maxRadius=30, dIter=30, cx=-1.3](-3,-2) (2,2)
\end{document}

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:

xelatex mandelbrot-colour.tex
evince mandelbrot-colour.pdf &

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

Mandelbrot set
Mandelbrot set in colour

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

\documentclass[border=12pt]{standalone}
\usepackage{pst-fractal}
\begin{document}
    \psset{xWidth=8cm, yWidth=8cm}
    \psfractal[dIter=10, cx=-1.3,cy=0](-2,-2) (2,2)
\end{document}

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

xelatex julia.tex
acroread julia.pdf &

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

Julia set
Julia set

Sources:

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

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

How to install Groovy on Linux system-wide

Groovy Logo
Image by Zorak1103 – CC BY-SA 3.0

(see section UPDATE at the bottom of the post to install Groovy using sdkman)

A colleague of mine recently asked me to install the Groovy programming language on our Red Hat 6.5 server and to make it accessible to all users. I thought it would be a very straightforward task but a quick search on the Red Hat 6.5 official repositories didn’t return any package for Groovy.

The easiest way to install Groovy manually is via gvm. I followed this  procedure to do it:

  1. Log in as root
sudo -i

2.  Retrieve the gvm install script and store it in a temporary file

curl –s get.gvmtool.net > /tmp/gvm.sh

3. Make the temporary file executable

chmod +x /tmp/gvm.sh

4. Run the install script

./tmp/gvm.sh

5. Complete the installation as requested at prompt

source "/root/.gvm/bin/gvm-init.sh"

6. Check that gvm is installed (this should return the help message explaining how to use gvm)

gvm help

7. Remove the temporary install script

rm /tmp/gvm.sh

8. Install groovy via gvm

gvm install groovy

9. Select the current version of groovy as default (at the time of writing version 2.4.3)  and check that groovy is installed

groovy -version
Groovy Version: 2.4.3 JVM: 1.7.0_79 Vendor: Oracle Corporation OS: Linux

10. Create a symlink to use groovy system-wide

ln -s /root/.gvm/groovy/current/bin/groovy /usr/bin/groovy

11. Exit the root user

exit

12. Check that groovy is installed system-wide

whereis groovy
groovy: /usr/bin/groovy

 

UPDATE:

Recently I had to install groovy on other Linux systems and discovered that that it is now much easier using sdkman, which is the evolution of gvm. This is the procedure to follow:

  1. Make sure you have Java installed by running:
java -version

If you don’t have Java, follow these instructions to install the default JRE/JDK or Oracle JDK.

2.  Install sdkman and set it up:

curl -s "https://get.sdkman.io" | bash
source "/home/cirulls/.sdkman/bin/sdkman-init.sh"

3. Check that sdkman is correctly installed:

sdk version

4. Install groovy:

sdk install groovy

5. Check that groovy is correctly installed:

groovy -version

 

Happy grooving!