Monthly Archives: June 2013

Graph drawing in LaTeX

Graphs? No, not pie graphs. Not even graphs of functions. THESE graphs.

Yesterday I tried making LaTeX notes for one of my maths courses. Not so much because I needed digital notes (to be honest it would have been much faster on paper), but because I wanted to get some practice at typing up documents in LaTeX.

I aimed to finish the discrete maths half of the course in a day, just to see how quickly I could make the document. After all, LaTeX has a steep learning curve, and some people, like me initially, probably have the impression that LaTeX would be slower than other what-you-see-is-what-you-get word processing software. Well, I met my aim.

However, to be fair, discrete maths only involved a lot of equations, which are easy to type up once you’re used to it. Graph theory, the other half of the course however, would definitely take longer due to the diagrams that had to be drawn. Well I thought it would take a very long time, but that’s because I only started looking into how to draw LaTeX graphs today.

It’s a shame that I only know a few words of French. At least the code’s in English though

I found out, after some research, that there is a tkz-graph package for LaTeX. It makes graph drawing pretty easy. Installing it took some figuring out, but it turned out that MikTeX has an installer for packages inbuilt, which was very useful.

The good thing about tkz-graph is that you can define vertices, and then reference them when creating edges. I haven’t worked out most of the features yet, but I can make some simple graphs now at least.

Simple graphs.

Here’s the code I used to generate the first of the graphs (the other two are similar):

\begin{minipage}[b]{0.3333\linewidth}
\centering
    \begin{tikzpicture}[scale=.5]
        SetGraphUnit{2}
        \GraphInit[vstyle=Classic]
        \tikzset{VertexStyle/.style= {fill=black, inner sep=2pt, shape=circle}}

        \begin{scope}[rotate=135]
        \Vertices[Lpos=135, unit=2, NoLabel]{circle}{A, B, C, D}
        \end{scope}

        \Edges(A, B, C, D)
    \end{tikzpicture}
\end{minipage}

For those interested, here’s a few quick notes about what I know, and don’t know about the above code:

  • The \begin{minipage} is what allows me to show three graphs side by side. Make sure your minipage blocks of code don’t have any empty lines in between them though, or your diagrams will appear under each other as opposed to adjacent (empty lines are like telling LaTeX “I want a new paragraph!”)
  • I’ve got SetGraphUnit{2} there, which refers to how far about everything is. However, I’ve also got unit=2 when defining the vertices – I put that there because for some reason some graphs were turning out smaller than others if I didn’t have it…
  • In the \tikzset command, inner sep=2pt makes the vertices smaller than their usual size.
  • The Lpos=135 argument of the \Vertices command refers to the positioning of the vertex labels in relation to the vertices (135 degrees rotation). I’ve also set labels off with NoLabel though, so it’s not actually doing anything.
  • \Edges draws edges continuously, so \Edges{A, B, C, D} draws the edges AB, BC and CD.  For a single edge, use \Edge(A)(B).

I’m only just starting out though, so this will get interesting when I have to draw more complex graphs. As a forward reference though, here’s some things I’ll be expecting to use:

  • There is a label option for the edges, which will come in handy for weighted graphs.
  • tikz supports a \foreach command, which is like a programming for loop for repetitive tasks. This will probably come in useful for drawing complete graphs.

Too lazy to click links

Quite a lot of files to download and not enough patience in me

Quite a lot of files to download and not enough patience in me

It’s the end of the university semester, and that means downloading-every-available-resource time. For archiving reasons.

Unfortunately, there are a lot of files to be downloaded. For instance, one subject might have  12 weeks of content, each week having a tutorial sheet, a tutorial solution, a practice class sheet and a practice class solution. That’s 48 links already, and that doesn’t even include past exams and other resources yet!

So I started to think, maybe I could use Python and Requests to nab the files I need. First I’d specify a URL, an output folder and the types of files to be downloaded (pdf, tex, etc). Then all I would have to do is run the program and voila! Everything downloaded in a second!

Well, that only worked for my first subject.

Unfortunately, other subjects needed a bit more work to get the files – you had to first log in with your university account. I saw this coming, so I was surprised that my first attempt even worked (maybe the lecturer just couldn’t be bothered).

I didn’t end up figuring my way around this. Requests has a way of submitting forms by using requests.post(url, data), but unfortunately my university used some sort of authentication I couldn’t work out. Other subjects I didn’t even attempt, since they were on a completely different system which also required logging into.

One thing I did learn from this, however, is that you can download files with Requests quite easily. Fetch the file with r = requests.get(url), and the binary data can be found in r.content. Then all you have to do is write to a file using open(filename, “wb”).

What this means is that when I wrote my xkcd comic downloader, I didn’t actually have to use the Python Imaging Library. I could have just written to a file directly using the “wb” mode – and in fact this would also resolve my problem of downloaded GIFs being static

An overview of past projects

High school quality graphics.

Over time you learn new things, and find new ways to apply what you know. Or maybe there’s something you don’t yet know but want to learn, so you decide to look it up and play around with it.

That, in a nutshell, is how my post-primary school life went. I’d (infrequently) embark on new experiments, generally digital ones, and make something small over the course of a few days. Rarely did I ever create a large project which gave off a sense of completion, but nonetheless even the small projects were nice learning experiences.

Here I thought I’d share… quite a lot of these projects. Maybe you’ll find my journey amusing, and consider this a nice read. Or maybe I can somehow motivate you to try things you’ve never done before. Either way, enjoy.

(This is quite a long post, so feel free to skim the page and read the parts that look interesting. My heartfelt thanks goes to you if you end up reading it all though.)

Continue reading