All posts by Sandro Cirulli

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 and update KeePass

KeePass
Image Loki 66 – GPL

KeePass is one of my favourite software. It is a password manager which allows to store secure passwords and forget about them. All you need to do is to remember one single (strong) password to open the software and that’s it. Considering the amount of passwords one has to remember these days, there is a bad tendency to use weak memorable passwords – or even worse always the same password! KeePass is good choice since it is cross-platform, uses strong encryption algorithms, and can store data offline.

I’m currently using KeePass2 (update: I now use KeePassX) However, I noticed that if you install KeePass via the Ubuntu Software Center you won’t necessarily install the latest version and you won’t get the latest updates automatically. So here is the procedure to overcome this problem.

First, add the KeePass repository. For example, in Ubuntu:

sudo add-apt-repository "deb http://ppa.launchpad.net/jtaylor/keepass/ubuntu precise main"

where precise is the codename of the Ubuntu version (here 12.04 Precise Pangolin). If you don’t know the codename of the Linux version find it out with:

lsb_release -a

Then update the repository directory and install KeePass2:

sudo apt-get update
sudo apt-get install keepass2

Finally, open KeePass, click on on Help -> Check for updates and KeePass2 will update to the latest stable version. From now on, Ubuntu will update it automatically.

Update: I’m now using KeePassX which is a native port of KeePass for Linux and Mac OS.

How to write a macro in vim

Vim Power Tool
Image vim.org ©

Vim is my favourite text editor. It takes a bit of a learning curve to get used to its quirks but it is time well spent if you are doing lots of stuff on the command line or don’t have access to a GUI. I never used emacs so I won’t go into the editor war.

Macros in vim allow to record a sequence of commands and play them back in one go. Here is a cheat sheet adapted from vim tips:

qa       start recording to register a
...      enter a sequence of commands
q        stop recording
@a       execute the macro
@@       execute the macro again

Macros allow to automate complex sequences of vim commands and there is much more to it than this simple cheat sheet. Practical Vim dedicates a full chapter to the subject and it’s a good place to start if you want to delve into the magic world of vim macros.

How to zip and unzip from the command line

Image from Roadsidepictures – CC BY-NC-ND 2.0
Image by Roadsidepictures – CC BY-NC-ND 2.0

There are lots of GUI utilities to zip and unzip files but sometimes you only have access to the command line on a machine.  Linux has different types of data compression  formats (.tar, tar.gz, tar.bz2, etc.) but I find the zip utilities easier to use and more compatible with Windows systems. So how do you zip and unzip files from the command line?

First install the zip and unzip packages. If you are on a Debian-based system like Ubuntu:

sudo apt-get install -y zip unzip

or if you are on Red Hat/CentOs:

sudo yum install -y zip unzip

The most intuitive way to zip a directory and all its files  is to navigate to that folder and zip everything recursively inside the current directory:

zip -r foo.zip .

To extract the zip file into a given directory (e.g.  /tmp), navigate to the directory where the zip file is stored and unzip it with:

unzip foo.zip -d /tmp

How to do an svn checkout with a different username

Subversion
Image by Wikipedia – Fair use licence

The other day I had to do an svn checkout from an Amazon Web Services (AWS) instance and, of course, my user name on SVN was different from the user name on the AWS instance  which is by default ec2-user.  So how to do a checkout from a Subversion repository and specify at the same time the SVN user who is performing the checkout? Subversion has a handy username parameter,  here is the full command with some dummy output.

[ec2-user@ip-172-31-9-166 ~] $ svn checkout --username mysvnusername http://www.example.org/mysvnrepository

Authentication realm: <http://www.example.org:80> USVN
Password for 'mysvnusername': **********

-----------------------------------------------------------------------

ATTENTION!  Your password for authentication realm:
   <http://www.example.org:80> USVN
can only be stored to disk unencrypted!  You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible.  See the documentation for details.

You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/home/ec2-user/.subversion/servers'.
-----------------------------------------------------------------------

Store password unencrypted (yes/no)? no

A    mysvnrepository/file

Checked out revision 3027.

You will be asked to enter your SVN password and if you want to store your password unencrypted. I suggest to select no for the latter for security reasons but this means that you will need to provide the SVN password every time you perform an SVN command.

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!

How to change timezone on a Linux server

Time Zone
Image by deckhand – CC BY-NC-ND 2.0

Every year in March and in October the clock changes in most countries for energy saving purposes. This has the annoying effect of messing up the current time on your server if the timezone is not set properly. Servers in Europe are often set up to UTC time. For example my web hosting provider explicitly says:

Note that any times specified are executed in the timezone of the server, and not your local timezone. Accordingly, you may need to make allowance for this when selecting when to run your cron job. Our servers run in the ‘UTC’ timezone because our client base is global, and it remains constant throughout the year with no changes for daylight saving.

This is a bit annoying for cron jobs as you need to do some calculations in case you want to run them at a specific local time.

Even if you don’t have sudo rights on your server, you can at least modify the time for a given user. For example, to change the timezone for the current user to a local time (e.g.  Europe/London)  run the following command and add it to your bash_profile to make the change permanent:

export TZ="Europe/London"

If you have full control on your server, you can change the timezone system-wide by symlinking /etc/localtime to the appropriate file in /usr/share/zoneinfo. For example, to set the timezone system-wide to the local time in Paris:

ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime

If you run Red Hat/CentOS, you may also need edit /etc/sysconfig/clock in a similar way.

How to add a new user with sudo rights

Sudo Sandwich
Image by xkcd – CC BY-NC 2.5

Let’s say you have a new user needing sudo access to a Linux server. How do you grant him/her this great privilege? First, explain to the new user that sudo rights allow to do pretty much anything on a Linux machine – including screwing everything up! – and that with greater power also comes  greater responsibilities. Then, follow this procedure:

1. Open the command line

2. Create a new user (e.g. newusername) and add it to group wheel (members of this group have sudo rights):

sudo useradd -G wheel newusername

3. Set up the password for the new user. First, login as root:

sudo -i

Then set the password for the new user (e.g. newusername):

passwd newusername

Finally exit from root with:

exit

4. Test the newly created user and password by logging in as user newusername:

su - newusername

5. Test that the newly created user is in group wheel:

groups newusername