Pretty plots: Automatically create TikZ source code for plots in LaTeX with Matlab

sin(x) -- blue, cos(x) -- orange, dashed

Example TikZ plot from Matlab code

LaTeX …
I have just finished the first chapter draft for my thesis. As you’d expect from a mathematician, I write my thesis in LaTeX (or $latex LaTeX$). In the evenings, when I couldn’t concentrate enough for creating text that would make sense to anyone, I instead played around with the plots I wanted to include.

… and Matlab don’t work too well together
I used to create all my plots in Matlab (MathWorks), save them as eps (for latex) and png (for pdflatex) and include those in the LaTeX file via includegraphics. This causes a few annoyances. The size of the included pictures is decided in LaTeX, but rescaling has an effect on everything, including the text in the labels and legend. This meant that for every type of plot (and every decision to use a different scaling) I need to fiddle with the font size in the Matlab figures, until it fits and create all those files again. In addition, although Matlab understands some LaTeX, there were some symbols it has problems with. Finally, the font in the labels is never the same as in the text.

Enter TikZ
I recently heard about this on TeX StackExchange. It’s a way to code your pictures like you code everything else in LaTeX. Unlike pstricks, which I had used in the past and can hardly remember, it works natively with pdflatex. In this way, you hand over the responsibility for any text in your plot to LaTeX. Great! To get started with TikZ, there are an extensive manual, a minimal introduction, loads of answered questions on TeX StackExchange and more.

Automate
To plot a function in TikZ, you interpolate it linearly through many points. That’s a lot of source code per function. There will be loads of plots in my thesis, each showing several functions. Oh, and want axes, too. Naturally, this calls for automation. I wrote two functions in Matlab that would automatically create the tikz code for the axes and a function plot. The plot at the beginning of this post was created with the following code (and cut to size).

Matlab code
x=(0:0.1:7); sinx=sin(x); cosx=cos(x);
fid=fopen('tikzcodefrommatlab.tex','w');
tikzaxes(fid,[0 1 7],[-1 0.5 1],'$x$','$y$','2em');
tikzfunctionplot(fid,x,sinx,'blue,ultra thick','sin(x)');
tikzfunctionplot(fid,x,cosx,'orange,dashed,ultra thick','cos(x)');
fclose(fid);

LaTeX source
documentclass[a4paper,12pt]{article}
usepackage{tikz}
begin{document}
begin{tikzpicture}[yscale=1.5] input{tikzcodefrommatlab}
end{tikzpicture}
end{document}

Related Links

How do you make sure that your plots and figures fit with the rest of your document? And does anyone have a good idea what to do with 3D plots?

Let me know in the comments below.

Update: I’ve added further links to packages that were pointed out to me: matlab2tikz and PGFplots. I haven’t properly tried them out yet, but especially pgfplots seems awesome.

Don’t be scared

Found yesterday via WordPress’ Freshly Pressed:

3. Back up your work. […]

10. Don’t be scared. So many people have a guttural fear of technology—a fear of doing something wrong and messing up the whole computer or phone in the process. The truth is that it’s pretty hard to do anything that would permanently damage a computer or phone. Don’t be afraid to play around with things, make mistakes, and then figure out how to fix them. That’s how you learn. […]

Check out and follow all Pam’s “Top 10 Things I’ve Learned from Living with a Nerd”!

How to get a list of your blog posts on your website

I have recently updated my website. One of the things I wanted to include was a list of my most recent blog posts. Well, not the whole posts, but the titles of and links to them. I searched a bit and found RSS-to-JavaScript.com. It does exactly what I had hoped for and is also damn easy to use. Paste/type the RSS feed address into one text field, choose a few options and click on create JavaScript. Then you can copy that code snippet and paste it into the html file of your website. Or a local file if you want to create your very own RSS feed reader. (I’m getting ideas there …) To also include my twitter updates, I used the twitter widget.

You can admire the result here not anymore, unfortunately.

Moleskine/Notebook Hacks

I do not have a Moleskine notebook. Mine is wire bound and so far I’ve always been happy with that. However, I stumbled upon a collection of Moleskine hacks, such as how to make your Moleskine into a Getting Things Done organizer. I prefer having my to do lists on my computer, to be able to easily shuffle tasks around, add, delete and change them. If I have a paper to do list, I end up spending more time rewriting the list again and again than actually working through the tasks. (My to do list manager of choice is todoist, since I can easily break up tasks into a hierarchy of subtasks.)

So my paper notebook won’t become an organizer. It was originally meant as a diary. Later I started using it to prepare texts, such as emails, for my touch typing practice. Now, I do the same with some of my blog posts, when I’m not near a computer. Typing the stuff afterwards already acts as a first or second editing step.

And for this purpose, I picked up one of the tricks from the first link, PigPogPDA, from the list mentioned above: Using Post-It tags to mark the current “collection point”, that is the page where I want to continue writing, and the “processing point”, meaning everything before this has been processed, i.e. typed, already.

Here is my interpretation:

My Notebook Hack

My Notebook Hack

About the new Facebook groups

There just have been elections for a student body at my university. And a few times in the last days, I suddenly found myself belonging to Facebook groups supporting this or that person, people I did not know and had no idea what their agendas were. The first time I received an email informing me about that, later these notifications somehow didn’t reach me anymore, which is even more disturbing. I am not sure whether this has worked in favour of those candidates.

It is bad enough that Facebook has introduced these groups as opt-out, but that does not mean you should use this feature lightly. The 1000yr Old Man has a very nice post, explaining and demystifying the new Facebook groups. Please read this carefully before adding your friends to a group that they might potentially not want to join. Especially if it is about politics. And especially if it is me you want to add.