1

I am wondering how LaTeX implements its algorithms for laying out the text. How it figures out how to break a word in the mid-dle to better fit on the page, or how it slightly compresses the words to fit on a line or a page, etc. Wondering all the things that happen when you compile a LaTeX document, how it gets into its final rendered form. How it optimally measures everything so it gets things to look just right.

The question is if there is any documentation on this somewhere, or some aspects of it.

  • 2
    The TeXbook answers much of your questions as it describes the underlying TeX system (which for instance is responsible for line breaking and hyphenation). LaTeX's output routine (which I do not know that well) adds float placement to that. As the link provided by @Johannes_B states, page breaking in TeX is a first fit model. – Skillmon Jul 22 '18 at 6:54
  • Hey there is a JavaScript implementation of the TeX line-breaking algorithm. github.com/bramstein/typeset – Lance Pollard Jul 23 '18 at 5:14
  • @LancePollard If you're interested in different implementations of the TeX algorithms, you may want to look into all the different “implementations” of TeX. (Back in the 70s and 80s, it was common to take a useful program and rewrite it in another language to run on one's system, so there are many people who have written TeX in various languages.) You can look up TUGboat issues or search for a survey of TeX implementations. I had started collecting a few here, but the list is surely very incomplete. – ShreevatsaR Jul 24 '18 at 10:49
8

The main paper about the algorithms that TeX uses for laying out text is:

Breaking paragraphs into lines, by Michael F. Plass and Donald E. Knuth, published in Software—Practice and Experience Volume 11 (1981), pages 1119–1184.

You can find the paper online or read its latest version reprinted in the book Digital Typography; you can also search on this site to find answers mentioning it (like this one).


The algorithm is of course described in more detail in the source code — available as a printed book (TeX: The Program), and also available (without a few things like the mini-indices on each page) digitally. Specifically, this starts in Part 38 of the program (section 813), and continues into Part 39. This is also described, to some extent, in The Texbook, though mostly from a “user's” point of view.

Both The TeXbook and the TeX program have the flaw (IMO) that understanding any part of it properly requires you to understand much of the entire thing, so the paper is probably the best place to start.


Of course there is more to text layout than breaking paragraphs into lines (though in the case of TeX you may be surprised to discover how much just boils down to this basic box-glue-penalties model for line breaking, even things that don't seem to be about line-breaking). Everything that TeX does is in the TeX program, though you may want to read the corresponding research papers first before reading the relevant parts of the program.

LaTeX is a set of macros on top of TeX that adds quite a few things, but not much of that (AFAIK) is directly related to the question you seem to be asking, on how text is laid out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.