LaTeX or Word?

What is the best tool for writing a programming book?

I have just published Programming for Beginners,

This was prepared using LaTeX. 
If you have look at the previews on Amazon, I’m sure you’ll agree that the production values are pretty good, and as far as the layout and fonts go, they’re excellent.

Previously, I co-authored Swing Extreme Testing.

Again, the production values are pretty good, though the layout is somewhat simpler. This book was prepared using Word.

So what will I be using next time?

LaTex. But I can understand why some people would prefer Word.

LaTeX is typesetting program in which the text and markup are entered into a plain text document. The formatting commands in LaTeX are relatively straightforwards and programmers should have no trouble handling them. There are editors such as TexStudio that provide some degree of code completion and a fast edit/preview cycle.

LaTeX comes with a lot of pre-built packages of commands that make common operations pretty simple. For example, in my latest book I used a lot of code samples which were formatted satisfactorily using the “listings” package.

One of the most powerful packages is TikZ, which is used for diagrams. Here’s a picture from Chapter 13:

The TikZ code to produce this is very simple:

In the third part of the book, the readers build a simple image processing library. (It’s for adult and teenage beginner programmers, and all of the projects are built in small steps, so it’s not too hard.)
This TikZ picture helps explain image cropping:

Again, the code to produce this image was very simple and I found it much easier to produce this kind of image in TikZ than in a regular drawing program.

To see just what can be done with TikZ, have a look at one of the online galleries, such as www.texample.net/tikz/examples/

Equally impressive are the freely available book templates, such as those at www.latextemplates.com/cat/books My own book is built with a variation of the Legrand Orange Book Template.

All of this power comes at a bit of a price and I did spend a lot of time trying to get LaTeX to do exactly what I wanted. For example, the current edition has a slight indentation problem with the text alongside some of the images:

Up close, you can see the problem, which is that the ‘C’ of “Consider” is to the right of the ‘I’ in the heading:

It took me ages to work out what was going wrong, and it’s corrected in the upcoming Spanish version:

In fact the problem was that I needed a % sign, which is the comment in LaTeX, at the end of a line so that the whitespace (newline) was ignored.

This was the most difficult problem I had, and it now makes sense to me, whereas with wysiwyg programs I have always sooner or later just not understood what was going on internally.

There’s a very active LaTeX community, and for most other problems that I had, I was able to find a working example at Stack Overflow, but sometimes it was not clear what question I should ask.

So if you´re prepared for a bit of pain, go with LaTeX and you can get amazing results. But if you think that you’ll be driven mad by tricky problems such as needing a comment at the end of a line, Word might be a better choice.