The Talmud's typesetting dates back to the invention of the printing press. Printmaking was easy but paper was expensive so people crammed several texts onto a single page. The standard "Talmud page layout" is actually the Talmud as printed in the Vilna Shas, and other page layouts are possible.

There are rules defining how to typeset a Talmud page. The Vilna Shas predates the formalization of iterative algorithms, so some pages follow the rules less strictly than others. Talmudifier strictly follows the following rules:

1. There are always three columns of text
2. Whenever possible, the page starts with four lines of text in the left and right columns followed by a "gap" row to give the center column some breating space:
  ![The first four rows, and gap row, of an actual Talmud page.](images/four_rows.jpg)
3. After this, columns are typeset using the following algorithm until there is no more text:
  - Each column can have a different typeface (most famously, Rashi's commentary has its own typeface).
  - The widths of the columns are determined by which columns still have text. For example, if the left and right columns have text that needs to be typeset but the center column doesn't, then the column widths are each half of the total width of the table.
  - Find the column with the least number of lines (*n*).
  - Add all columns that still have text to the page up to *n* lines (or, if there isn't enough text, just add all remaining text).
  - Marginalia is always in-line with the text that it is commenting on.
  - For the columns that still have text, add "gap" rows.
  ![An actual Talmud page with red arrows over each of three columns. The arrows describe when the columns start and end.](images/center.jpg)

For more information, read: `Printing the Talmud : a history of the earliest printed editions of the Talmud by Martin Heller`

There is a fundamental problem in the typesetting algorithm: We need to iteratively get the number of lines in a column. Traditionally, this process would be sped up by experienced typesetters because they'd be able to eyeball how many character blocks would fit in a rectangle. Talmudifier emulates this heurisitic with the following algorithm:

1. Create a table that has text only in one column (the one we're trying to measure).
2. Using Cosmic Text, a crate normally meant for GUI text, add words to the column until we've reach the the target line count. This will be used as an initial guess in the next step for guessing the number of words that might fit on the PDF page. Cosmic Text is significantly faster than XeTeX.
3. Generate an XDV file with XeTeX in-memory. Normally, XeTeX generates an XDV file from a TeX string and then converts the XDV file to a PDF. We skip the final step because right now we just need the line counts.
4. Extract the line count per page from the XDV file.
5. Add or subtract a word as needed, and repeat the process until the column is filled up to the target number of lines.
6. Render the final PDF.