LaTeX Typesetting Tips

LaTeX is a great typesetting & publishing system. Its been around for years and is very powerful and stable. It’s my preferred tool for creating books, letters and reports for work. Here are a couple of my favourite LaTeX typesetting tips.


Typesetting clinic outcome letters using LaTeX

During 2016 I’ll be switching the production of over a million clinic letters per year to a LaTeX typesetting system. Tasked with developing the rules and standards for the layout of the letters, this article covers how I solved each of the strict requirements. The LaTeX system is installed on a Ubuntu Linux server, so some of the points are Ubuntu specific, but most are covering the LaTeX commands developed. Let’s take a look at the requirements first:

  1. The letter needed to use the Arial font, set at a size of 10 points.
  2. The paper size needs to be A4, and the first page needs to have a larger top margin to accommodate the pre-printed stationary used within the hospital.
  3. The letter should print page numbers in the footer, using the format “Page 1 of 2”. However, if the letter fits onto a single page then no page numbers should be used.
  4. If the letter content spans multiple pages, then starting from page two onwards a patient identifying banner should be added to the top of each page. This banner should include the letters unique number along with the patients name and address.
  5. The PDF produced should highlight letters that have not yet been signed by the consultant. The page background colour should be made yellow so that it stands out. There should also be a large banner stating that the document is still in a draft status.
  6. The LaTeX system should prevent CC's on the letter being spilt across page breaks. The whole address should be kept together as windowed envelopes are used.

1. Installing the Arial font on Ubuntu

To check if the Arial font is already installed on a Ubuntu server, for use with LaTeX, use the following command to list the currently installed non free fonts.

sudo getnonfreefonts-sys -l

Which will generate similar output to:

Installation directory: /usr/local/share/>texmf
arial-urw  Arial (URW)                [installed]
classico   Classico (URW)             [not installed]
dayroman   DayRoman (Apostrophiclabs) [not installed]

If Arial is not installed on your system then you can install it using the following command:

sudo getnonfreefonts-sys arial-urw

To set Arial as the default font for your LaTeX document, add the following LaTeX code to your preamble.


2. Setting the paper size to A4 and specifying a larger top margin for page one

To set the paper size and initial margins, use the geometry package. The larger top margin of 2.8cm is to accommodate the patient information banner, see requirement 4.

    paper       = a4paper,
    top         = 2.8cm,
    bottom      = 1.8cm,
    right       = 2cm,
    left        = 2cm,
    headheight  = 0.89cm,
    headsep     = 0.89cm,
    footskip    = 0.89cm,]{geometry}

To create a larger top margin on the first page of the letter, so that the content wouldn’t print over the top of the pre printed stationary, I created a new command called FirstPageStyle. This command takes one parameter which specifies how much extra space to add on the first page.


In the above example, an extra point 4 cm is added to the first pages top margin, giving a top margin of 3.2cm. The code below shows how the page style is set and the vspace command is used to add the necessary vertical spacing before the letter content starts.


3. Page X of Y in the footer, except for single page letters

To print the page numbers in the bottom right hand corner of the letter, I used the fancy header package. Specifying the page style as using the fancy format. We blank out the left and centre sections of the footer for both the odd and even pages. In the right hand position of the footer we output the current page number, and use the last page package to output the page reference number of the last page. This gives us the page 1 of 2 shown in the image below.



Page 1 of 2 in LaTeX footer

For letters which fit onto a single page, we don’t want to print Page 1 of 1 as that would be redundant. To prevent this we extend the FirstPageStyle command so that it checks if the last page reference number is equal to one. If it is that we blank out both the headers and footers for the fancy page style by using \fancyhf{}


4. Adding a patient information banner on subsequent pages

To create the patient information banner (shown in the image above) we used the left odd and even headers of the fancy package. As you can see from the code below we blank out the centre and right header positions and specify a thin rule line to appear under the information strip.

\fancyhead[LO,LE]{\scriptsize{\OurRef \\ \RefPatient \\ \RefAddress}}

The patient information is passed to the header by three commands. The first specifying the letters unique reference number. The second specifying the patients name. The final command outputs the patients address. The commands are populated at the start of the LaTeX document, and are re-used in the PDF properties of the document.

\ALMA{OurRef}    {A1/134337/2606968/PAULX030}
\ALMA{RefPatient}{Mrs Jess Griffin DOB 29/04/1991 NHS 777 777 7777}
\ALMA{RefAddress}{16 The Street, Wyvern, South West England, HB3 4CY}

5. Setting the page background colour

It was an important requirement to ensure that letters which hadn’t been checked or signed were rendered to look totally different from signed letters. Signed letters would be typeset with black text on a white background. To stand out, draft letters are typeset with black text on a yellow background. A large banner also needed to be added to the top of the letter stating that the document was still in a draft status.

LaTeX page background colour yellow

To achieve this, one of two commands are used within the letters main project file. The first indicates that the letter is still in a draft status, with a message indicating who was the last person to edit the letter and when they last edited it. The second command is used when the letter has been checked and verified and needs to be marked as signed. When signed the PDF produced has a white background.

\ALMA{Draft}{Last edited by Paul Bradley on 22/01/2014 @ 10.20}

The ALMA command, which is the name of clinic letters system, has an if statement which checks for the presence of the word draft. When found the background colour of all the pages are switched to yellow. The draft status message is output, and the message regarding who last updated the letter is passed to the #2 variable.

        \textbf{\textsuperscript{$\oplus$} \Huge draft\normalsize \textsuperscript{$\oplus$}} \\
        \emph{Please note this document has not been signed the content is subject to changes, use at your own risk}
        \newline #2

6. Prevent CC’s from splitting across page breaks

The hospital makes extensive use of windowed envelopes so it is important that the address of any CC’s on the letter, and there can be up to 20, don’t span a page break. This achieved by wrapping the address in a minipage environment. The position parameter [t] is set to top to control the vertical alignment of the boxes content, relative to the baseline of the surrounding text. Three line breaks are added to the bottom of the address to create some padding space before the next CC entry.

    Dr J Das\\
    Holby City Surgery\\
    South West England\\
    HB2 3CY

Change the starting chapter number of your LaTeX book

In April 2015 I started keeping a journal, typeset in LaTeX, with the intention of turning it into a physical book using a print on demand service like Lulu. My two young sons might find it assuming to read after I’m gone.

As I only started keeping the journal in April, I needed to find a way to make LaTeX start the chapter numbering from the number 4. The setcounter command can be used to reset the numbering of chapters, sections & pages. So before my first chapter heading I simply use the following commands.


This technique would also be useful if you wanted to release a sample chapter of your book. Say you wanted to offer chapter seven as a free download, then you could include all the appropriate book front and back matter with just your sample chapter, but still have it numbered correctly by using the setcounter command.

Typesetting journal / diary entries in LaTeX

I will soon be starting a years long software development project. When working on longer projects I like to keep a development journal documenting my design decisions, software dependencies and thought processes as the project progresses. At the end of the project I have the journal printed via LuLu as a pocket book to hand over to the developers who will be maintaining the project.

On a recent podcast, bugs considered harmful, Douglas Crockford also suggests that developers should keep a bug journal. The idea being, it helps you to become aware of the type of bugs you introduce into your software and documents the process of fixing those bugs. It’s a nice idea and something I will try on this new project.

Being a LaTeX user I wanted to format my journal entries so that the dates are in small capitals with the ordinal suffix (st/nd/rd/th) in superscript. I also wanted the titles to be formatted as bold text. This is the result I wanted to achieve:

typeset journal entry

Extract taken from
Diary of a Nursing Sister on the Western Front, 1914-1915

I created a new LaTeX command called JournalEntry which accepts two parameters, the date and the ordinal suffix, like:

\JournalEntry{Sunday, October 18}{th}-- Got under way at 6 A.M.,
and are now about half-way between Paris and Rouen.

This is the small piece of LaTeX that creates the actual formatted result.

\newcommand{\JournalEntry}[2] {

Spell checking multiple LaTeX files

Your editor of choice may not have a spell checker that knows how to ignore LaTeX commands, and it can become tiresome to keeping ignoring commands that the spell checker should ignore by default. If this is the case, then switch to using aspell on the command line.

Most Linux distributions already come with aspell installed, and if it’s not then it’s usually available via your distributions package manager. Aspell is a great standalone spell checker which can check any text file you supply. It has built in filters for ignoring HTML and LaTeX tags within your files, and comes with a wide range of language dictionaries.

Using Aspell to spell check a LaTeX file

To spell check a LaTeX file called filename.tex, using the English Great Britain dictionary, you would use the following command:

aspell --lang=en_GB -t -c filename.tex

The -t switch instructs aspell to use tex mode, thus ignoring all the tex & LaTeX commands.

If your working on a large LaTeX project, like a book or a thesis, then it’s recommended that you split each chapter or section into its own file. So you end with file names like ch01.tex, ch02.tex and so on. So how do you spell check all the tex files within your project when aspell doesn’t allow wild cards as the input file.

aspell -c *.tex
Error: Only one file name may be specified.

Luckily you can use the find command to search for all the LaTeX files, and then pass each file onto the spell checker. If you name your chapter files in number order, then they will be passed to the spell checker in the correct order

find ch*.tex -exec aspell --lang=en_GB -t -c {} \;