# The Hitch-Hiker’s Guide to LaTeX3

## Introduction

As with the original Hitch-Hiker’s Guide, this blog post won’t actually be all that useful to someone wanting to truly explore LaTeX3. It’s more of a “What I did on my holidays” kind of guide. I’ve recently had my first go at doing some coding with LaTeX3 and I thought it might be interesting to record my experiences.

The main site has certainly made me more aware of the LaTeX3 project. Familiarity, in this case, has not bred contempt but rather it has meant that LaTeX3 has seeped into my consciousness so that I’ve become vaguely familiar with it without ever actually having to think about it. Before this current experience then I had had a bit of exposure to LaTeX3 through making a few adaptations to the (most excellent) unicode-math package, but that was very definitely a case of “Monkey see, Monkey not really have a clue what he’s doing so it’s a million to one chance but it might just work.”. To really get to grips with something new, I need a problem to work on. Finally, along came a problem that I wanted to solve in TeX but which was very definitely a regular programming problem. So the prospect of doing it in ordinary TeX was too daunting to contemplate. For this problem, learning LaTeX3 seemed the lesser of two evils.

## The Problem

The problem was to implement Hobby’s algorithm1 for finding a smooth curve through a specified list of points. The original question was this one. This is most definitely a programming question. It’s a series of manipulations of arrays to set up a matrix equation which is then solved, then there are various computations made from the solution which feed back into the construction of the path. A “normal” programming language eats this sort of thing for breakfast. There’s a very nice python implementation as an answer to that question and one can see in that all sorts of nice programming features: iterating over arrays, doing fancy calculations, and so forth.

But I wanted an implementation in some variant of TeX. So LaTeX3 seemed the closest to a programming environment in the TeX world. I was right, but in working through the implementation I found out that “seemed the closest to” and “is” are not quite the same thing. There were a few places where I had to remember that TeX is a macro language, not a function language.

## The Slogan

I think that if I had to think of a slogan for programming in LaTeX3 it would be a variant of a slogan for object-oriented programming. That slogan is:

An object is something that knows what it is.

In LaTeX3, I’d say that the slogan is:

Programming in LaTeX3 is like programming in an object-oriented language where every object has amnesia.

What I mean by that is that it has, to my inexperienced eye, the look of an object-oriented language. But it’s like you have to keep reminding every object what it is.

If I were allowed a second slogan, it would be that programming in LaTeX3 is like programming with a keyboard that is missing an equals sign.

## Amnesiastic Objects

I’m not a programmer, but I’ve written scripts and hacked stuff in quite a variety of languages so I’ve a bit of experience of different types of programming. LaTeX3 had a strong object-oriented feel to it. It might just be that I’ve recently “gotten” OO so I see everything through that lens. But it does feel as though there are objects and methods. It feels a long way from ordinary syntax. It particularly feels a long way from Perl’s “do your best to make sense of it” syntax where almost whatever you throw at it, Perl will do something vaguely sensible with it.

But when writing my LaTeX3 code, I felt that it was extremely important to remember what each “thing” was. The methods are not directly attached to their objects. In Lua, I might write `v:add(u)` but in LaTeX3 I have to write `\int_add:Nn \l_my_int {\l_my_other_int}`. So `\l_my_int` doesn’t know that it is an integer and thus doesn’t know how to add other integers to itself.

Once I’d gotten used to this, life became much easier.

## No Equals

This is probably a “feature” of the fact that underlying LaTeX3 is TeX’s macro language. There are no equals in TeX (well, okay, but not in the same way as in more mainstream languages). So there are no functions and no return values. It isn’t possible to do `a = myfunc()` to assign the return value of `myfunc()` to `a`. Rather, we have to pass `a` to `myfunc()` and it has to do the assignment inside it (there are, of course, other ways but I’m a beginner).

This also has an impact on scoping. I’m used to the adage that what happens in a function, stays in a function. But if I need to do `myfunc(a)` and expect this to modify `a` then that can no longer be true. So defining auxiliary functions needs to be done with care, and sometimes I just didn’t bother as it was more complicated than it was worth. Fortunately, there are some smart people around so this is solvable. But my point is that things that one takes for granted in other languages, are not quite the same in LaTeX3.

## Not Only, But Also

Doing Hobby’s algorithm in pure TeX would have been a task too big for me. I’m sure that there are many users on the main site that could do this with ease but I’m not one of them. Doing Hobby’s algorithm in a language I’m more familiar with, say Perl or Lua, would be a reasonable task. (I don’t say easy, but I wouldn’t expect to struggle with the language itself.) Implementing it in LaTeX3 was certainly not a breeze and certainly I struggled at times with the language. But at no point did I feel that I’d bitten off more than I could chew. There always seemed to be a way to achieve what I wanted. I’d say that the only real complaint is the lack of examples of code in the documentation. I am, unashamedly, a Cargo-Cult programmer but that’s only a feasible programming methodology if there are good examples to cut-and-paste. Fortunately, the TeX-SX chat room has plenty of experts willing to help out.

Will I use LaTeX3 again? Absolutely. I wouldn’t choose it for a non-TeX situation, but if it’s something to be done within TeX then LaTeX3 is definitely high up on the list of choices. Do I expect an easy ride? Not at all. But at the end, I expect a sense of accomplishment not quite like coding in any other language.

That’s what I got from implementing Hobby’s algorithm.

1. I recommend looking at the GIF page images, the OCR on the PDF is not good on the mathematics.

## One thought

1. Reinhard Neuwirth says:

0 (zero) comments for a post that I would give 10 out of 10 for being informative as well as hilarious. Not sure what this says about the forum. Andrew, if I had your email address I would discuss with you my experiences 1 on 1.