News

TeX and LaTeX typesetting for perfectionists – tutorial

TeX may be the smartest way to lay out maths, but learn how it also makes invoices, books and awesome PDFs

Typesetting for perfectionists

You use LibreOffice for your documents, perhaps Scribus for making a company brochure, and edit most documents online. You’ve heard of TeX – in the context of mathematical equations, mostly – but why would you use it to lay out everything from invoices to 100-page PDFs? Quite simply, because it produces beautiful-looking documents that are easy to maintain, and using a simple markup that makes keeping track of document evolution and changes far simpler than it is with word processors and desktop publishing packages. Format once for each type of content you write, then concentrate just on the content. Thanks to tablets with insanely large numbers of pixels per inch, beautiful PDFs become important, so feel the irony as you produce them from the *NIX command line.

TeX itself is simply a formatting engine, stable and virtually bug free. LaTeX is a complete document preparation system built as a macro package on TeX, and containing templates and add-ons to create just about any document. We’ll use LaTeX2e, known as Standard LaTeX, which has barely changed in 20 years – it hasn’t needed to.

The commands are straightforward, but there are many options. Best skim through the basics by following our lightning-quick intro, modifying the documents, then move online and build bigger, better docs as your knowledge increases.

Typesetting for perfectionists
Typesetting for perfectionists

Resources

TexLive
Comprehensive TeX Archive Network

Step-by-step

Step 01 Bugs will be features!

TeX is extremely stable, with years passing between bug reports! Since release 3, the version number of TeX is converging to π, and is now at 3.1415926 (released March 2008). The version number will be changed to π upon creator Donald Knuth’s death, and all future bugs found are to be regraded as features!

Step 02 TeX Live

TeX Live is the recommended way to install the LaTeX packages you’ll need. It’s available for almost all distros. To try our invoice example, also install the latex-invoice RPM, or apt-get install texlive-latex-extra.

Step 03 TeX in your browser

You needn’t wait until you can download and install TeX Live. JaxEdit is an online LaTeX editor with live preview – for maths but not much else yet – which you can try at JaxEdit. You can also download the JS and run it in your browser.

Step 04 e=mc2

Most people have heard good things about TeX and LaTeX in the context of laying out mathematical equations. In fact, it was because of how badly typesetters were laying out mathematical equations that Knuth felt compelled to write TeX originally…

Step 05 Just a fraction

The step 4 example code produces this output when converted to a PDF. We’ll look at some of the commands in a minute, so you can get started trying LaTeX out, but first we want to make sure that you realise it’s not just for maths.

Step 06 Right-to-left

LaTeX spread through the academic world and quickly gained popularity with linguists and language researchers, leading to excellent layout of non-Latin alphabets. Pictured at the top of the page is an example of Arabic in ArabTeX (which also handles Hebrew).

Step 07 Booked up

LaTeX continues to be virtually the default tool for producing post graduate theses and other academic works – this is because of its structural strengths, and consequent abilities at producing bibliographies and nested contents pages.

Step 08 WYSIWYM formatting

These strengths are a product of the What You See is What You Mean (WYSIWYM) paradigm. You write your .tex document in any text editor and let LaTeX take care of the typesetting: running latex on the file produces a .dvi, which you can convert to PostScript or PDF.

Step 09 It’s just text

Remember it’s just text. With structure. And a few commands that all begin with and may have braces {} and possibly square brackets []. The problem for the beginner is knowing where to start with so many options, so let’s start simply.

Step 10 Macro power

Remember those imported macros on the title-page screenshot? There are thousands of user-contributed packages at CTAN. Here, your advisor is looking at a Dutch .sty for producing invoices. Useful, but not as powerful as the official invoice package…

Step 11 More macro power

…which can do the adding up for you. Having tried in the past to produce invoices both from spreadsheet templates and word- processors, it is with great relief that your advisor turns to something that just needs text filling in, yet produces readable output.

Step 12 Boilerplate

Everything before begin{} is what’s called boilerplate, analogous to in an HTML document. documentclass must be specified in the {} braces: {letter}, {article} or {book} are common types. Options such as twoside for double-sided printing, paper size, and font size go in the [] brackets.

Step 13 Make a date

usepackage{}, title{}, and date{} are common boilerplate. include{filename.tex} will import other files, so big projects can be split, such as a short story collection of chapters from different authors. Text commands can be embedded in fields, as here with date.

Step 14 Desktop calculator

Using the Fee command followed by {activity}, {rate} and {units done} for each invoice item lets invoice.sty work out totals for you and produces clear, readable invoices (see main picture). VAT options are also available.

Step 15 Draught preview

In the pic at the bottom of the page, we’re previewing an earlier draught of the invoice. Note we’ve used separate programs – GEdit, xterm and xdvi – in this case, but done it all within Emacs on the screenshot of the opening page.

The point is, we’re dealing with text, so it’s easy to fit in with your workflow, and the tools with which you are familiar. It might also be a time to try something new [will you stop pushing people onto Emacs, already – Ed], or even use LaTeX to generate your slides, or your website’s PDF downloads.

Step 16 Special character

Note the & in LinuxUser & Developer – &, like #, $, %, ^, _, {, }, ~ and , is a special character, with special meaning to LaTeX, so must be called like: # $ % ^{} & _ { } ~{} textbackslash.

Step 17 Like the hat

Note the {}s after the escaped ^ and ~ in the previous step. This tells LaTeX that they are standalone, otherwise it will treat them as accents and place them on top of the following letter. Find oodles more in The Comprehensive LaTeX Symbol List, by Scott Pakin.

Step 18 Error messages

For complex documents, with references and tables of contents, you may have to rinse and repeat the latex command until everything is fully processed. If there’s a problem, LaTeX will tell you about it. Here, we’ve left in special characters by mistake.

Step 19 Pretty pictures

LaTeX seems to work best with graphics from EPS files and PDFs, or tools generating vector graphics from data on-the-fly. However, your advisor doesn’t excel at all things visual, and has seen others do more than dump a pic in a doc, as here.

Step 20 txt2tex

If you’re converting a lot of documents to TeX, txt2tex will take some of the tedium out of the job – download it and run the demo. But you’re by no means limited to text-based documents in TeX. See what LaTeX can do with music and games…

Step 21 Know the score

The WYSIWYM choice for music is not so clear, with Philip’s Music Writer (PMW) arguably easier to learn than TeX’s MusicTeX markup. Additionally, Lilypond, a standalone MusicTeX fork, produces beautiful scores. Choices, choices.

Step 22 En passant

Install one of the chess packages (skak or TeXmate) and you can draw a board from Forsyth–Edwards Notation:

fenboard{r5k1/1b1p1ppp/p7/1p1Q4/2p1r3/ PP4Pq/BBP2b1P/R4R1K}

With TeXmate you don’t need the numbers to end lines.

Step 23 TeXnicians and TeXperts

There are some great guides to using LaTeX – The Not So Short Introduction to LaTeX2ε (Or LaTeX2ε in 157 minutes) by Tobias Oetiker, Hubert Partl, Irene Hyna and Elisabeth Schlegl is a good concise start, but don’t just read a book…

Step 24 Try it out

The best way to learn is, as with any coding language, by reading .tex documents out there and making your own. If you’ve got a document you’re about to write, give yourself an extra hour and find a .tex with similar structure to build it upon.

×