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.
|
|
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.
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.
|
|
We compile this document and get the following output.
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
|
|
which correctly renders the document title.
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.