Language-Aware LaTeX Title Typesetting

| 3 minutes

I use $\LaTeX$ on a daily basis to write reports, course notes and nearly any other document that needs to be properly formatted. While I write in English most of the time, I sometimes need to produce a document in French or German. In this post, we will see why the placement of title macros advocated by most $\LaTeX$ tutorials and tools can lead to incorrect typesetting in non-English documents.

Since I often switch between different operating systems and computers, I like to use Overleaf to avoid having to install a $\LaTeX$ distribution in all of my work environments. The code generated by Overleaf when creating a new Blank Project looks something like the following.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Title}
\author{Author Name}
\date{Date}

\begin{document}

\maketitle

\section{Introduction}

\end{document}

This code is more or less what you would write when first learning document typesetting with $\LaTeX$. It compiles just fine and produces the expected output.

Default title typesetting

Let’s try to switch to a different language. We will use the babel package to import multilingual typesetting rules and pass it the french option. We also need to specify a different font encoding to make sure that the document is typeset properly.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
\documentclass{article}

% Change font encoding (a warning should be emitted if you omit this line)
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% Import multilingual formatting rules
\usepackage[french]{babel}

\title{Titre: En Français}
\author{Auteur}
\date{Date}

\begin{document}

\maketitle

\section{Introduction}

\end{document}

We compile this document and get the following output.

Incorrect title typesetting

Notice the colon just after the first word in the title. French typographic rules tell us that a colon should always be preceded by a non-breaking space in addition to being followed by a space. Could it be that $\LaTeX$ and babel get this wrong? I’ll give you a hint: babel is perfectly aware of this rule—after all, such typographical discrepancies are the main reason why this package exists in the first place. Can you spot what’s wrong in the code above?

The issue is that packages that process user input in $\LaTeX$, e.g. by changing the way the text is laid out on the page, can only see the part of our document between the beginning and the end of the document environment. To fix the above example, we therefore only need to move the title macros inside of said environment, yielding

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}

\begin{document}

\title{Titre: En Français}
\author{Auteur}
\date{Date}

\maketitle

\section{Introduction}

\end{document}

which correctly renders the document title.

Correct title typesetting

Note: You may wonder why we didn’t simply add a space before the colon to the title outside of the document environment. While this would have worked in this particular instance, more subtle formatting issues may appear depending on your title and target language. The best practice should therefore be to always put your title macros inside of your document’s body.