Aller au contenu

Introduction

This article explains how to typeset German text: enabling correct typesetting of characters such as ä, ö, ẞ, ü etc., and providing support for language-specific features such as hyphenation. If you are looking for instructions on how to use more than one language in a single document, for instance English and German, see the International language support article.

German example using pdfLaTeX

We’ll start with the following pdfLaTeX example which you can open in Overleaf using the link below the code.

\documentclass{article}

% \usepackage[utf8]{inputenc} is no longer required (since 2018)

%Set the font (output) encoding
%--------------------------------------
\usepackage[T1]{fontenc} %Not needed by LuaLaTeX or XeLaTeX
%--------------------------------------

%German-specific commands
%--------------------------------------
\usepackage[ngerman]{babel}

%Hyphenation rules
%--------------------------------------
\usepackage{hyphenat}
\hyphenation{Mathe-matik wieder-gewinnen}
%--------------------------------------
\begin{document}
\tableofcontents
\vspace{2cm} %Add a 2cm space

\begin{abstract}
Dies ist eine kurze Zusammenfassung der Inhalte des in deutscher Sprache
verfassten Dokuments.
\end{abstract}

\section{Einleitendes Kapitel}
Dies ist der erste Abschnitt. Hier können wir einige zusätzliche Elemente
hinzufügen und alles wird korrekt geschrieben und umgebrochen werden. Falls ein
Wort für eine Zeile zu lang ist, wird \texttt{babel} versuchen je nach Sprache
richtig zu trennen.

\section{Eingabe mit mathematischer Notation}
In diesem Abschnitt ist zu sehen, was mit Makros geschieht, die zuvor definiert wurden.

\[ \lim x =  \theta + 152383.52 \]
\end{document}

 Open this pdfLaTeX example in Overleaf.

This example produces the following output:

OL2German1b.png

Text files: integers and characters

Any text file, such as a LaTeX input .tex file, is nothing more than a stream of numeric (integer) values which are being used as a mechanism to represent characters of text; consequently, processing a text file involves scanning (reading/processing) a series of integer values. However, an important question arises: which set of characters is actually represented by the integer values contained in a particular text file? In other words, how have those integer values been encoded: what is the correct (intended) "mapping" (encoding) from integers in the text file to the corresponding characters they are supposed to represent?

Text files can be generated within innumerable computing environments: across different countries/continents, using a multitude of different devices, operating systems and editing tools. Originators of text files could, potentially, use or apply different text encodings according to local requirements, such as language, when generating and storing the sequence of integer values chosen to represent the individual characters contained in a text file. This may work well if the generated text files stayed within compatible technical ecosystems, which use the same encoding, but what would happen when those files are transferred to completely different environments—because many text files do not contain any information which indicates the encoding used to generate them.

Clearly, the producer (originator) and consumer (user) of textual data must, somehow, agree on the encoding (mapping) being used, otherwise encoding errors are likely to arise due to mismatches between the integer data in the file and the set of characters it is assumed to represent. In addition to correctly mapping text file integer values to characters, any subsequent visual display of those characters requires some form of font that is capable of providing the data (shapes, or even bitmaps) to output a visual representation of the desired characters.

Input encoding: inputenc, UTF-8 and a change to LaTeX in 2018

Historically, a variety of 8-bit encodings were used to generate/process text files, including LaTeX inputs. To cut short a very long story, the developers of LaTeX created the inputenc package to address encoding issues—allowing text files, created using various encodings, to be transferred between disparate LaTeX installations.

However, over time, users/software developers moved away from multiple 8-bit encodings to using Unicode and its UTF-8 encoding scheme, which became the de facto option for encoding text files. Prior to 2018, to process UTF-8 encoded files LaTeX document preambles included the line

\usepackage[utf8]{inputenc}

Readers might observe that the example above does not include the line \usepackage[utf8]{inputenc} in the document preamble: why is that? This is due to an important change to LaTeX introduced in 2018: a switch to UTF-8 as the default input encoding. Documents typeset with pdfLaTeX, and using UTF-8 encoded text, including those created and typeset on Overleaf, no longer need to include \usepackage[utf8]{inputenc} but is does no harm to do so. For further information see the April 2018 issue of LaTeX News and the Overleaf blog post TeX Live upgrade—September 2019. All text files created on Overleaf are encoded using UTF-8.

Output encoding: the fontenc package

To correctly typeset characters contained within input files, those characters need to be mapped to the appropriate output shapes (glyphs) contained in fonts used to typeset the document. This “output encoding” is handled by another package called fontenc.

To use fontenc include the following line in your document preamble, using an encoding, such as the T1 encoding, which supports accented characters contained in Latin-based languages:

\usepackage[T1]{fontenc}

Using the T1 font encoding, via \usepackage[T1]{fontenc}, has other benefits:

  • The default OT1 LaTeX font (“output”) encoding is 7-bit, meaning it can only encode 128 characters and does not include (enable) access to genuine accented character shapes (glyphs) contained in fonts. The OT1 encoding causes TeX engines to “fake” accented characters by typesetting (combining) a base character with an overlaid, and shifted, accent character.
  • If the TeX engine has to resort to construction of accented characters, this has implications for copy/paste of text from PDFs: copying text containing constructed accented characters will result in two separate characters being pasted: the base character and the accent character. Using the T1 encoding avoids this.
  • Using the T1 encoding, to access genuine accented characters, improves hyphenation.

LaTeX T1 font encoding

The following chart lists the T1 font encoding. It is reproduced from page 22 of the document LaTeX font encodings which is available on CTAN.

Example of copy and paste

The following minimal example, which does not use fontenc, demonstrates issues with copy/paste of text containing accented characters.

\documentclass{article}
\begin{document}
können wir einige zusätzliche (OT1 encoding)

{
\fontencoding{T1}\selectfont können wir einige zusätzliche (T1 encoding)
}
\end{document}

 Open this example in Overleaf


The first piece of text is typeset using LaTeX’s default OT1 encoding, resulting in “fake” accented characters. The following piece of LaTeX code:

\fontencoding{T1}\selectfont

switches to using the T1 encoding, which results in LaTeX typesetting accented character glyphs. If you copy the text from the PDF produced by the example above you should see something like this:

k¨onnen wir einige zus¨atzliche (OT1 encoding)
können wir einige zusätzliche (T1 encoding)

Observe how the OT1 encoded text does not contain actual accented characters, whereas the text encoded with T1 does contain them. The result of copy/paste can also depend on the application being used to view the PDF from which you are copying/pasting text containing accented characters.

Language-specific packages and commands

To extend the default capabilities of LaTeX, providing proper hyphenation and translation of the names of document elements, import the babel package using the ngerman language option.

\usepackage[ngerman]{babel}

As you may see in example using pdfLaTeX, instead of “Abstract” and “Contents” the German versions “Zusammenfassung” and “Inhaltsverzeichnis” are used. In addition, the new orthographic rules approved in 1998 are supported by babel using ngerman instead of the german parameter, which supports the old orthography.

Hyphenation

The babel package usually does a good job of providing language-specific hyphenation capabilities, but if a particular word does not hyphenate correctly there are packages to assist. For example, you can add the hyphenat package in your preamble:

\usepackage{hyphenat}
\hyphenation{Mathe-matik wieder-gewinnen}

The second line is a list of space-separated words with defined hyphenation rules. If you want to prevent hyphenation of a specific word, write {\nobreak word} within your document, or include it in an \mbox{word}.

Further reading

For more information see

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX