Controlling the page count in LaTeX

October 11th, 2009

I’ve been working on a project which involves generation of simple books for print in LaTeX. The print shop set the requirement that the page count of the PDFs we give them always are multiple of four.

As this is something LaTeX doesn’t provide out of the box, my first idea was to do some post processing with the PostScript tools that you have on a Unix system. But introducing an extra tool wasn’t feasible at all: Running LaTeX from a webapp is fragile enough. Fortunately, LaTeX is just a module for TeX, and TeX is nothing but a programming language. So there most definitely is a way for doing this in plain TeX. Unfortunately, programming in TeX is everything but straight forward. Luckily I can’t resist language challenges…

And I did it! Here is my LaTeX macro that makes the number of pages in the document a multiple of some number:

The loop construct is the tricky part. It’s of the form

\loop
  ...
  \ifsomething...
  ...
\repeat

The if-statement in the middle controls whether we continue the loop or break out. Today, most people are probably more used to seeing the above logic written like this:

while (something) { .... }

A thing to note is that TeX-loops allow for statements to be run both before and after the if-statement is evaluated. Code before the statement is run in each iteration, similar to traditional do {} while (...) loops, but the code after is only run if the if-statement returns true:

\loop
  % Code to run in each iteration (do-while style)
  \ifsomething   %
  % Code to run if the if-statement above returned true (while-style)
\repeat

Finally, note that the if-statement must not have a closing \fi, as most if-statements in TeX have.

Tip: Finding material on programming in TeX is hard, but the TeXbook provides some help. Deep down in chapter 20 there are some examples of looping and conditionals, which proved to be enough for me.

Chord progressions – now for the Ukulele

September 12th, 2009

I’ve updated my chord progressions page and added Ukulele chords:

Ukulele chord progressions

The progressions currently lack some basic variations, most notably some seventh chords. So that’s up next!

Mandolin Chord Progressions

September 6th, 2009

I’ve just uploaded a hobby project of mine – mandolin chord database. It currently displays standard chord progressions needed to play most songs, e.g. the major chords C-F-G and the accompanying minors Am-Dm-E.

Ukulele and guitar versions coming!

Here it is: Mandolin chord database.

The implementation is pure JavaScript, using the canvas element for displaying the chords.

Making jQuery code readable

June 17th, 2009

As a long time jQuery user I use many of it’s features, and much more than just the most basic ones. I love it, especially when I find new tricks to simplify or shorten my code. But my code is most often only read by me.

Recently I’ve been working with developers new to jQuery, and it has come clear to me that my code – as simple and smooth I find it – is often hard to understand. I’ve gotten the same looks I would get presenting a Perl-golf program. Ah well, maybe not quite. But you get my point.

I have set to make my code more readable, without making it ugly. Below are a few best-practices I’ve made for myself.

Break down long chains

Here is a typical bit of code I might write:

$('table')
    .find('.activate').click(function () { activate(this.id) }).end()
    .find('.deactivate').click(function () { deactivate(this.id) }).end()
    .find('a').show();

Long chains of code tend to use jQuery commands like end and andSelf to modify the wrapped set. Those methods are good to have when you know them, but their semantics can’t be easily deducted from their names. A long chain performing many tasks can also be harder to walk through than smaller independent bits. So it is wise to break chains down:

var table = $('table');
$(table).find('.activate').click(function () { activate(this.id) });
$(table).find('.deactivate').click(function () { deactivate(this.id) });
$(table).find('a').show();

This version – without doubt – lacks the beauty of the chain above, but is more readable, especially to someone unfamiliar.

Bind events using explicitly using bind

Having the same method to bind and fire an event is bound to confuse – hell, I get confused again and again:

$('select')
    .change(function () { ... })
    .change();

Explicitly calling the bind command to bind to the change event is much easier to grasp:

$('select')
    .bind('change', function () { ... })
    .change();

This holds for all events who have shortcuts for binding, e.g blur, click and mousedown.

Clarify what this means

The this keyword is probably one of the most infamous elements in the JavaScript language. And even when you’ve gotten fluent in using it, you never can be quite sure it’s used the same way in the same contexts.

This is a typical code snippet:

$('a')
    .addClass('logged')
    .bind('click', function() {
        logUrl(this.href);
    });

What does this point to? This example is that simple, and the href attribute that specific, that it’s maybe not hard to guess. But in more complex situations it’s hard to find out.

Copying this to a clearly named variable does a great deal in clarifying what’s going on:

$('a')
    .addClass('logged')
    .bind('click', function() {
        var clickedLink = this;
        logUrl(clickedLink.href);
    });