Close
hobbits in latex header

Hobbits in Latex

Today we are talking about latex. Flexible, adapts to any shape, but sometimes a bit uncomfortable, especially for beginners. If you don’t think of black, tight-fitting clothes when you think of latex, you’re definitely a nerd. We are talking about the word processing programme LaTeX – probably the most important and most widely used tool among scientists. It’s pronounced “LAH-tekh”, by the way.

Let’s start with a little history of word processing. Before printing was invented, people wrote by hand and try (more or less) hard to arrange the text prettily on the page, depending on the occasion. In a letter, people wrote and still write as it suits them, whereas for the writing of a book, more work went into the alignment and design of the text. With the invention of letterpress printing, for purely practical reasons, there is no way around planning in advance where which letter should end up on the page. In addition to authors, there were now typesetters whose job it is to turn a manuscript into a book. They combine lead letters into sentences, columns and pages to copy them onto any number of sheets using the “potato-stamping” method.

More than 500 years later – in the age of computers, smartphones and e-books – much is different and typesetters are long gone. But their craft is not extinct and lives on in modern word processing. Typical writing programmes like Microsoft Word still edit texts – but automatically, silently and secretly: a typed text is wrapped at the end of a line without being asked and spread over several pages; one click is enough to centre, underline or colour a text. It is easy to forget what is happening in the background and, in particular, what possibilities there are if you are prepared to open Pandora’s box.

The virtual typesetter

If all you want to do is write a letter to your gym to cancel your never-used membership, it is quite sufficient to use a word processing programme like Word. But what if you want more? Text in rainbow colours, alignments against all logic, words in places where no word has ever been before!

LaTeX can do all that and much more. It is based on TeX, a typesetting system (we remember the typesetter who puts the text on the page) developed in the 1980s. Leslie Lamport (that’s a guy, by the way, in case anyone was wondering) created a programme that made TeX easier to use and also added many features to it. The programme was named Lamport TeX after him – LaTeX was born.

So much for the history but how exactly can you imagine LaTeX? Word is a word processing programme based on the “What you see is what you get” principle. When I write a text in Word and then print it out, I see pretty much the same thing on the paper as on the screen – except for the wiggly spelling mistakes. LaTeX, on the other hand, works according to the “What you see is what you asked for” principle. That is, what I see on the screen has nothing to do with the final document at first sight – I just ask LaTeX to do something for me.

Let me show you an example. On the left-hand side you see a text in Word, with some special formatting and embellishments. On the right-hand side you see the LaTeX code that leads to about the same document (click on the image for a larger view):

I know what you are thinking. Why the hell should you do this to yourself? For simple texts, the advantage is not obvious but LaTeX shines in more complicated projects. Try to create the following document with Word:

Complicated LaTeX document

With LaTeX: no problem. The code can be found here. There are commands for everything. Font colour, orientation, position, formulas. Another advantage of LaTeX is that you can do everything without taking your hands off the keyboard once – that is, without touching the mouse. The distance your fingers travel to type on the keyboard is much shorter than moving your hand to the mouse. No more searching for buttons in menus. If you are good, you can write a LaTeX document blindfolded.

Besides showing off, it has the undeniable advantage of being faster. If you write a lot, you have to be efficient. Sure, you have to know the commands and that requires some endurance. It’s essentially like learning a new language or instrument. But it is worth it!

An instruction manual for LaTeX

Hobbit in Latex

All beginnings are difficult and the best way to learn is with a bit of humour. At the time, the LaTeX introduction by Manuela Jürgens of the FernUniversität Hagen sweetened my learning experience (unfortunately it is only available in German). Colloquially, it is also called the Hobbit Tutorial. It is an instruction manual for LaTeX that illustrates the basic building blocks using text examples from The Lord of the Rings. By now, there is also a second part: LaTeX – Advanced Applications. Or: News from the Hobbits. Will it be a trilogy?

As a physicist, you usually come across LaTeX when you first have to write formulas on the computer. I can type something like E = h f (Plank’s energy-frequency relation) without problems even on my mobile phone but I don’t want to write formulas like \langle \hat{f}\rangle(t) = \int \psi^*(\vec{r},t) f(\hat{\vec{r}},\hat{\vec{p}},t) \psi(\vec{r},t) d^3r in Word anymore. Admittedly: Microsoft’s formula editor has become better over the years, but – in my opinion – simply because it now understands LaTeX input.

If you want, you can combine the “What you see is what you get” principle of Word with the formula-friendliness of LaTeX. LyX is an example of a text editor based on the “What you see is what you mean” principle. It doesn’t give you what you see or what you asked for, but what you really mean. Finally, a programme that understands you!

LyX is easier to learn for beginners than LaTeX because you have a direct feedback on the screen what is formatted and how. But if you want to aim high, you can’t get past LaTeX – and I took Hobbits with me on my way back then. But enough theory, now I want to show you how much fun you can have with LaTeX!

6 LaTeX packages you didn’t know you needed – until now!

The standard installation of LaTeX can do most of what you need in everyday (scientific) life. If you want more, you can add additional packages. Each package brings new functions. For special characters, layout, colours.

LaTeX also has an incredibly important feature: anyone can write and publish their own packages. If I need something and I know LaTeX, I can write my own package. Because of the popularity of LaTeX, there are now packages for almost every problem: music notation for musicians, creating bibliographies or indexes, or a drawing programme.

But what happens when nerds and geeks have the opportunity to give free rein to their creativity? They jump at it (as we have already seen with Hello World). The functions become more and more complex and more and more useless. Because the question isn’t: Why? But: Why not?

Games

Let’s start easy. There are a number of packages for creating games. Sudoku, crosswords, chess, or bullshit bingo. Or how about a flip book? This document was created with the LaTeX package happy4th. If you view it in your PDF viewer in full page view, without continuous scrolling, and scroll fast enough, you can see fireworks – even if it’s not Fourth of July.

realhats

realhats package

In physics, we use operators, objects that perform a certain operation. For example, the position operator \hat{x} gives the position of a state. We mark operators, as you have just seen, with a little hat. If this is too boring for you, you can use the LaTeX package realhats. Instead of a small triangle, you can give your operators a Christmas hat, a top hat, a crown, or a sombrero.

coffee

When you look at someone else’s papers, you can often learn a lot about them. I’m sure we’ve all had that one teacher whose returned exams always smelled as if they’d corrected it in the next smoker’s lounge. You might also use this method to show your boss how hard and long you work by deliberately spreading a few coffee stains on your papers.

But what do you do in home office? Here you rarely handle real paper. How should I adequately express my coffee addiction?

This question was apparently also asked by Hanno Rein who has developed a LaTeX package for coffee stains. It should come as no surprise that he has been to Cambridge and Princeton where you need lots of coffee to survive. Without wasting paper, one can spread different kinds of coffee stains on the document. An extension for wine stains is already available, more packages for mustard, ketchup, chocolate and blood are probably coming soon. Everything you need in your home office.

Example of the coffee package

halloweenmath

There are lots of variables in physics. For a larger selection, we like to borrow the Greek alphabet. But sometimes even that is no longer enough. A friend of mine studied string theory for a while, and when his professor ran out of variables in a lecture, he used the number 7. Numbers hardly ever appear in theoretical physics anyway, so you can use them as variables.

But if that’s a bit too daring or boring for you, there’s the LaTeX package halloweenmath. With this, you can use pumpkins, witches, ghosts, or skulls in formulas. For that extra adrenaline in theory.

Example of the package halloweenmath

Let me show you some examples. In the first line we see a classical equation with one variable. If three times pumpkin equals six, what is pumpkin? It’s much nicer than always just x. In recent times also a popular quiz on Facebook and co.

Equation number 2 shows a typical example of black magic. The internet is full of “maths tricks” on how wrong maths leads to amazing results. With the help of the black magic operator, it can finally be labeled appropriately.

Equation number 3 is trivial, and is left as an exercise for motivated readers.

bookshelf

From the CTAN documentation of the bookshelf package (Peter Flynn)

Another problem with the home office: Everyone now sets up a nice bookshelf which sits decoratively and as if by chance in the background at the video conference. But what if you don’t like books, only read e-books, or don’t want to limit yourself to the video conference but want to show off your bookshelf as a profile picture? This is where the bookshelf package from XeLaTeX (another programme for using TeX that was not developed by Leslie Lamport) comes in handy. This package creates a pretty picture from a list of books or articles that puts many real book collections to shame. Also suitable as an alternative for the soulless bibliography at the end of an academic paper. Might be a bit harder to read but has a lot more character right away!

chickenize

Finally, let’s go haywire. The master of useless and entertaining packages is the LuaTeX package chickenize (like XeTeX, LuaTeX is another member of the TeX family). The madness can already be recognised in the documentation because they sort their functions into maybe useful, less useful and complete nonsense. There are quite a few commands and it is worth having a look. Examples are:

  • chickenize: Replace every word with “chicken”
  • randomfonts: change the font randomly for each letter
  • guttenbergenize: delete all quotations and footnotes (a reference to a certain German politician)
  • italianize: Imitate an Italian accent
  • dubstepenize: Translate the text into dubstep lyrics
  • drawcov: Brand new – draw a Corona Virus

Here’s the whole thing in action (the code is here, as you can see the changes are in fact introduced only very short commands):

Chickenize Latex document example
Demonstration of the package chickenize. In the first paragraph with an Italian accent (italienize), in the second random upper and lower case (randomuclc), in the third randomly replacing words with chicken (chickenize), and in the fourth on Dubsteb (dubstepenize) underlaid with a horse (drawhorse). And of course everything in rainbow colours (rainbowcolor).

Do you miss any package? What have you always wanted to do in a document? Let me know! And now: Have fun with the hobbits in LaTeX!


Do you like what you read? If you don’t want to miss any new posts, don’t forget to subscribe to my blog.