216

I'm interested in writing packages for TeX/LaTeX/XeLaTeX. I googled around, but all I found was guides or tutorials about how to prepare documents using LaTeX. I want to know how LaTeX works and how to change it or add new things to it.

Which book/guide/tutorial is the most useful source to learn LaTeX programming? Is there official documentations around LaTeX development? (I couldn't find development SCM on official site. is source code private?)

5

6 Answers 6

244

Being aware of the fact that this post is quite old, I would like to add two further resources which might be interesting when it comes to programming.

I think there are three categories that need to be mastered (perhaps not all to the same degree) in order to become comfortable around TeX programming:

  1. TeX programming. That's very basic, it deals with expansion control, counters, scopes, basic looping constructs and so on.

  2. TeX typesetting. That's on a higher level, it includes control over boxes, lines, glues, modes, and perhaps about 1000 parameters.

  3. Macro packages like LaTeX.

First, when I started programming in TeX, I found it difficult to get even one of them straight. I read the TeX Book, which is very strong when it comes to (2) and which is the best reference for (1) I am aware of. However, it does not introduce (1) very well, because its aim is to teach typesetting, not programming.

Since then, I have learned a lot - and I wrote an article about (1) which might be of interest to you. Furthermore, I stumbled on an online textbook about TeX, which covers (1) and (2) pretty well - although it is only available in german.

Here are the resources:

  1. TeX programming notes (written by me). It is shipped with pgfplots or can be found here.

  2. A free online book about TeX (in German!) Einführung in TEX.

  3. As soon as you start programming packages, you will certainly want a user interface, i.e. key-value. Joseph Wright wrote an introductory article about it and I contributed a section about the relatively new pgfkeys. I would recommend using it, it is quite simple (provided one uses the mentioned article as an introduction instead of the the complicated reference manual which is part of pgfmanual.pdf). The article can be found at TUGboat here.

I must honestly admit that I have longed for a good introduction about how to program in LaTeX - but I never found one. Finally, I got stuck with pgf which implements a lot of the utility functions, but I found it easier to understand.

Besides pgf, the best documentation for LaTeX that I found (besides the clsguide mentioned by Stefan) are the the LaTeX kernel sources (which he mentioned as well). But they are cryptic and require a very deep understanding of (1) and (2) before you can even read them.

6
  • 24
    Can I upvote this twice? The only thing I would say differently is to emphasize the magic of PGF even more; that package is really an entire LaTeX programming environment with an incredibly smooth, powerful, and well-constructed interface. It should be its own format "pgflatex" and not a package.
    – Ryan Reich
    Oct 13, 2011 at 16:15
  • 2
    I'm glad you find the answer useful. Concerning pgf: you may be right that it is a valuable part of (3) (especially the generic pgfkeys sub-package). Oct 13, 2011 at 17:56
  • 3
    @ChristianFeuersänger I would also upvote twice because I appreciate very much your TeX programming notes. I hope to see something about pgfkeys because there are a lot of examples with a lot of different methods and it's not easy to know what is the correct way to use pgkeys for programming macros and developing packages. I feel that the methods have evolved along with the package. Feb 19, 2012 at 13:23
  • 8
    btw: christians "Notes On Programming in TEX" are available via texdoc programm Its a very condensed introduction which i like a lot
    – bloodworks
    Feb 19, 2012 at 22:51
  • @ChristianFeuersänger plz, look at this question: tex.stackexchange.com/questions/107102/using-toks-registers. We are discussing your book.
    – user4035
    Apr 5, 2013 at 22:18
77

You could start with LaTeX2ε for class and package writers aka clsguide.

We already have some information regarding this on the site, for example:

You could learn much if you would read the commentes LaTeX2ε sources aka source2e. To understand it best, you could learn TeX or use a TeX reference.

13

When I first got in contact with LaTeX, I bought Lamport’s LaTeX: A Document Preparation System, but, alas, I found it too superficial and didn’t understand many (even basic) things.

So, as I later took notice that all started with Knuth’s TeX, I felt that it might be the logical way to begin with looking at TeX. In consequence I bought Knuth’s The TeXbook, since it is the program’s manual written by the program’s inventor and creator. Actually it really was the right decision! It precisely describes exactly how TeX works:

  • how the input (file) is read and tokenized
  • how you define macros
  • how you use registers to do calculations

This book helped me to really understand TeX. In my opinion it can be considered as fundamental literature to gain a thorough understanding of TeX and as a basis for LaTeX.

(N.B.: I fell so deeply in love with TeX while reading The TeXbook that I never felt the need to use any other format. ;-) )

8

Take a look to Appunti di programmazione in TeX e LaTeX (2009, seconda edizione) by egreg

1
  • Oops, can't understand the language.
    – youthdoo
    Dec 17, 2022 at 14:23
5

The very helpful book-2 to start programming in latex https://www.dickimaw-books.com/booklist.php?book_id=13.

and more precise link: https://www.dickimaw-books.com/latex/novices/novices-report.pdf

0
5

Getting started

First, you definitely need to know how the engine (bottommost layer) works (expansion, etc.) -- otherwise you're going to get unpleasant surprises, because TeX is very counter-intuitive.

For this, read either the TeXbook or TeX by Topic.

Selecting a "framework"

You can program in

  • plain TeX
  • plain TeX with e-TeX (e.g. includes \expanded, \numexpr instead of \multiply, etc.)
  • LaTeX2e-style (use packages e.g. xstring, etc.)
  • pgf (TikZ) → texdoc pgfmanual
  • expl3 → texdoc interface3
  • Lua → texdoc luatex, texdoc ltluatex + (my answer) Can the Lua part of LuaTeX know about tokens?

These are like "frameworks" for programming (compare jquery over JavaScript, or Boost over C++), which leads to very different code style.

Reading other people's code

Sometimes, reading on how other people implemented something is the best way to learn.

For this you need to know where to find the source code -- see (contains my answer) Where do I find out how a command/environment is defined?

Also, it's unavoidable that if they use a different "framework", the code style would be different from yours.

Programming idioms

When you start programming in TeX and for a long time after you would encounter some annoying things.

I think the issues that make programming in TeX seems to be slow/difficult/annoying is that

In TeX, it's not very straightforward how to break down your task into the small tasks that the engine can do.

programming is all about breaking down tasks into small ones.

For this, I think the solution is to learn the TeX idioms (and the questions linked from it).

Other issues

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .