Monday, 12 November 2012

Batch cropping white space borders in Matlab figures

Matlab figures have, by default, a white space border around them. This is fine if you want to view them by themselves, but wastes space if you want to add them to a text document, where the white borders are "added" to the document margins. It is possible to adjust the border width in Matlab, but if you already have exported the figures to an image file, it's too late. (Note: When exporting to EPS the border is not included by default).

If you want to remove this white space for a large number of image files in one go, ImageMagick is your friend. For example,

mogrify -trim +repage *.png

will remove the white border of all PNG files in the current directory, using the "smart crop" trim option. Consult the ImageMagick documentation on cropping for further options.

Tuesday, 16 October 2012

Editing Grub boot menu order and changing default OS

I recently decided to try running a dual-boot system with Windows 7 and Ubuntu. I first installed Windows 7, and then installed Ubuntu. And with Ubuntu comes the GRUB boot loader, which pops up when I start the machine, and lets me choose which OS to use. If I don't choose anything, the item at the top of the list is selevted automatically after about 10 seconds.

I want to use Windows 7 by default, but unfortunatly Windows is at number 6 or 7 on the list. If I'm not paying attention when I start the machine, it boots into Ubuntu, and I have to restart it. How can I make Windows 7 the default OS?

The answer lies in reconfiguring the GRUB menu. There are several ways to do this, but I used the grub-customizer GUI app, which seems both very simple and flexible. Install it using the terminal:

sudo add-apt-repository ppa:danielrichter2007/grub-customizer
sudo apt-get update
sudo apt-get install grub-customizer

In the grub-customizer, you can reorder the GRUB menu items, and for example check the option "Default OS: Last used OS". You can read more about the grub-customizer on the How-To Geek Blog.

Wednesday, 29 August 2012

Online LaTeX equation editor

Today I got a great tip from my colleague Vidar: A link to an online LaTeX equation editor at The equations can be downloaded as both raster and vector graphics files. The editor shows a preview of the equation you're writing in "real time", so it's acually also a nice way to test your LaTeX code while you're writing.

Tuesday, 26 June 2012

Creating a simple nomenclature for a PhD thesis (or similar)

The deadline for my PhD thesis is drawing close, and I'm putting on some finishing touches. One of the things I'm currently working on is the nomenclature - a list of all the mathematical symbols. There are several tools for doing this "properly", like the glossaries and nomencl packages, but they work best if you use them consistently through the entire writing process. It's too late for me to use these, so I have to add the symbols to the nomenclature manually. But what formatting should I use? I tried the tabular environment, but my list soon grew too long for a single page.

Luckily, I came upon this post at the howtotex blog written by Frits Wenneker. He suggests using the longtable environment for the nomenclature - which elegantly breaks the table up to fit several pages. Check out his post if you're looking for something similar.

Tuesday, 12 June 2012

Using accented characters with BibTeX

When writing scientific articles, I often cite authors with accented characters in their names, for example

Pörtzgen, N., Gisolf, D., & Blacquière, G. (2007).
Inverse wave field extrapolation: A different NDI approach to imaging defects.
IEEE Trans. Ultrason., Ferroelectr., Freq. Control , 54 (1), 118–127.

Such accented characters often creates problems when BibTeX is used. The reason for this is that BibTeX uses 8-bit encoding for the .bib file, as explained in this post at StackOverflow, and also in the LyX wiki.

The simple solution to this problem is to write accented characters in the BibTeX file using special symbols, as explained on the BibTeX webpage. For example, ö is written \"{o} and è is written \`{e}. With these special symbols, the BibTeX source code for the citation above is

  author = {Niels P\"{o}rtzgen and Dries Gisolf and Gerrit Blacqui\`{e}re},
  title = {Inverse Wave Field Extrapolation: A Different {NDI} Approach to Imaging Defects},
  journal = {IEEE Trans. Ultrason., Ferroelectr., Freq. Control},
  year = {2007},
  volume = {54},
  pages = {118-127},
  number = {1},
  month = {January}

Wednesday, 23 May 2012

Adjusting vertical alignment of subfigures in LaTeX

When using subfigures in Latex, I have sometimes found it annoying how the figures are vertically aligned at the bottom, rather than being centered. An example: The following code


produces the following figure:

which seems a little lopsided. I found a nice solution at stackoverflow: Use the \raisebox command to adjust the vertical position of individual subfigures, for example


which yields the following result:

Much nicer! :)

Monday, 14 May 2012

Adjusting LaTeX margins to fit a wide figure

I'm currently writing my PhD thesis, trying to shape a number of scientific articles into a coherent whole. In this process, I've come across a problem: The PhD thesis format has a text width of 120mm, which is suitable for one-column text, but I sometimes want my figures to be wider than this. How can I adjust the margins of a page on a per-figure basis? The answer: The changepage package.

Include the package by adding this line to the preamble


... and use the \adjustwidth{leftmargin}{rightmargin} inside the figure environment, for example

        \caption{Wide figure}

In this example, both the left and right margins are reduced by 10 mm, making more room for the figure. The command also works with subfigures. 

Friday, 4 May 2012

Batch converting PDF to EPS

Today I found myself needing to convert about 20 vector images from PDF to EPS, because a journal that I want to submit an article to, demands that all graphics are in EPS format.

Searching the net, I found several solutions, many of them involving the pdf2ps or pdftops command-line utilities. The best solution was the one I found here, which converts all the images in a folder with a single-line for loop:

for f in *.pdf; do pdftops -eps $f; done

The code is run on the command line. I've only tried this in Linux, but some version of it may work in Windows or OSX as well.

Monday, 20 February 2012

Batch converting images files from one format to another using ImageMagick

Let's say you have 500 JPG images and want to convert them all to PNG in one go. The mogrify function in the ImageMagick software suite comes in handy here:

mogrify -format png *.jpg

This produces a copy of each image in the new format. It's possible to combine the conversion with other operations, for example resizing and conversion to monochrome:

mogrify -resize 50% -monochrome -format png *.jpg

I often create PDF vector graphics which I use together with PDFLaTeX. However, sometimes I need bitmap versions of the vector graphics images. Mogrify can convert from PDF to bitmap formats (for example PNG), but it's important to set the density of the sampling grid in the conversion (dots per inch), otherwise the bitmap image might have a too low resolution. Example:

mogrify -format png -density 600 *.pdf

The commands above can be executed directly in the terminal if you are inside the folder containing the images. The folder can also be specified explicitly, for example

mogrify -format png /home/martin/images/*.jpg

Wednesday, 4 January 2012

Running a graphical application remotely using SSH

This finding is in no way revolutionary, but I found it useful, so maybe others will too. I'm running Ubuntu Linux on my laptop, and recently I wanted to run a Matlab script on a Linux workstation. I can log on the the workstation using ssh, for example


which gives me access to the terminal. However, SSH can also be used for graphical programs, if the server is running a X window system. The SSH connection is configured for X session forwarding by adding a "-X" switch at logon,

ssh -X

Such forwarding is further explained here. If I logon using the above syntax and start matlab, it appears as a regular window on my desktop, although it's running on the workstation. Unfortunately it can be a bit slow in responding to mouse and keyboard input, so using the terminal version of Matlab may in some cases be just as good.