Computational Physics

Blog

Software

Projects

Courses

Theses

People

About

Events

Internals Getting started Computing Cluster Coding style

Getting started

Computing Cluster

Coding style

While writing my master thesis I’ve found that one of the best ways of explaining a lot of concepts is by using illustrations and figures. I make most of my illustrations using Inkscape , an open source program for making and editing vector graphics .

Using vector graphics mean that all lines in the image is stored as mathematical expressions, contrary to raster graphics where an image is stored by storing the color in each pixel. This means that if you try to zoom in on a raster image, you will get and ugly pixelated image, while if zooming in on a vector image the computer can produce a higher resolution image from the mathematical expressions, as seen in the figure below.

This image illustrates the difference between bitmap and vector images. The bitmap image is composed of a fixed set of dots, while the vector image is composed of a fixed set of shapes. In the picture, scaling the bitmap reveals the dots while scaling the vector image preserves the shapes. Source .

The other advantage of vector images is that if the image consists of a few simple objects, as is the case for most illustrations in my thesis, the image can be stored in a very compact way. This means that the resulting pdf will be smaller in size, while still having nice and high resolution figures.

The impatient can check out a working example in this Github repo .

The problem is that there hasn’t been any really good ways of including vector graphics in LaTeX documents. But I recently discovered the package svg , which greatly simplifies everything. Using this package you can easily include vector graphics stored in the svg -format, which is the default format in Inkscape, by simply using the command

if the name of your image is “example.svg”, and adding

to your preamble. Note that this only works if “example.svg” is in the same directory as your LaTeX source file (I will come back to how to include images in other locations).

To compile the document you have to allow calling of external functions during compilation, which you do with latex or pdflatex by adding the flag “–shell-escape”:

The way this works is that it uses Inkscape to export the file to a .pdf_tex and a .pdf file. The advantage of doing this is that all text in the figure will have the same formatting as the rest of your document, and that the font can be controlled directly in your LaTeX source file.

One other advantage of using \includesvg{} is that you don’t have to export your figures from Inkscape every time you make a change to get the new image in your pdf, since the svg package does this automatically. The package also knows to only re-export an image if you changed the svg-file.

The package documentation does a good job of describing usage and options of the package. An often used option is the width or height of the image, which you control in the usual way

I don’t think the svg package is available in any Ubuntu-package yet, but installing it is as simple as downloading svg.sty and putting it in your texmf path

or putting it in the same directory as your LaTeX source file. You also have to have Inkscape installed, and possibly also imagemagick.

The default export option the package uses is to export the page of your svg. What you usually want is to export the drawing , so we change this by adding the command

in the preamble.

Most of you probably have your images in a different location than your LaTeX source code. The way Inkscape exports svg-files to .pdf_tex and .pdf means that you have to use some special commands to include svg-files in other locations, but this is done by adding some options to the \includesvg{}-command. If your images are located in a directory called “images” in the same directory as your main LaTeX file you use

This path can also be set permanently using

(Take the section with below with a grain of salt, as according to a recent blog post here on this blog, the subfig package isn’t depreceated after all.)

The svg package uses the package subfig , which apparently is deprecated[ 1 ][ 2 ], and should be replaced by for example subcaption . Using subfig and subcaption in the same document doesn’t work very well, so if you don’t want to use subfig this can be solved by editing svg.sty. Replace “subfig” with “subcaption” on line 40 and you’re done. An working version that uses subcaption can be found here .

Working as a scientist for Polytec R&D Institute.

Former Master student in Computational physics at the University of Oslo, where I worked on modelling water trapped in nanostructured silica using molecular dynamics.

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

Education Prize to Computational Physics!

Creating a 3D Neuron Scene in Blender

National education price awarded to Computational Physics professor

Choosing the right license for your code

Getting MCALab up and running on a 64-bit installation of Matlab on Ubuntu

Find Usages Keybind broken in QtCreator

PyCharm Keymap for QtCreator Fanatics

Fixing GDB issues with GCC 4.8.x and C++11

LaTeX package for UiO Master’s theses

Circumventing annoying library path issues with QtCreator

Log in

Entries RSS

Comments RSS

WordPress.org