20

I'm having some issues with listings that include German Umlaute. The following example code generates a completely messed up PDF.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{listings}
\begin{document}
\begin{lstlisting}[language={[LaTeX]TeX},inputencoding={utf8},extendedchars=false]
Staatsangehörigkeit
\end{lstlisting}
\end{document}

Does anyone have an idea how to fix this?

Umlaute

3 Answers 3

31

the normal listings package doesn't provide unicode support. I use the following code to work around the Problem

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{listings}

\lstset{literate=%
    {Ö}{{\"O}}1
    {Ä}{{\"A}}1
    {Ü}{{\"U}}1
    {ß}{{\ss}}1
    {ü}{{\"u}}1
    {ä}{{\"a}}1
    {ö}{{\"o}}1
    {~}{{\textasciitilde}}1
}
\begin{document}
    \begin{lstlisting}[language={[LaTeX]TeX}]
    Staatsangehörigkeit
    \end{lstlisting}
\end{document}

I also tried listingsutf8 and listings2 which ist BETA, but for me the literate solution just works perfekt ;)

edit:sry, marco has the same solution earlier...

3
  • This always shows the final Umlaut (in your case an ö) in the compiled PDF no matter which one was used in the source file.
    – mat
    Commented Jul 25, 2017 at 15:07
  • I'm not sure what you mean. I can use ÖÄÜßüäö~ with no problems.
    – someonr
    Commented Jul 26, 2017 at 10:02
  • 1
    I found my error. I had an additional extendedchars entry (the value beein true or false did not matter) in the lstset command, which caused the strange error I described.
    – mat
    Commented Jul 26, 2017 at 11:06
13

I use the following method in my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{listings}
\lstset{literate={ö}{{\"o}}1}
\begin{document}
\begin{lstlisting}[language={[LaTeX]TeX},inputencoding={utf8},extendedchars=false]
Staatsangehörigkeit
\end{lstlisting}
\end{document}

enter image description here

Ulrike Fischer wrote in the German community mrunix:

Weil listings eine Verbatim-Umgebung ist, die Befehle wie \section, \bfseries und eben auch \"a nicht ausführt, sondern einfach ausdruckt.

English try: Because listings is a verbatim environment, commands like \section, \bfseries and also \"a aren't executed, but instead just printed.

10

An alternative solution to that provided by marco, perhaps a touch less typing, by using the key escapeinside:

\documentclass{article}
\usepackage[ngerman,english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[]{listings}
\begin{document}
\begin{lstlisting}[language={[LaTeX]TeX},
             inputencoding={utf8}, extendedchars=false,  
             escapeinside=``]
Staatsangeh`ö`rigkeit
\end{lstlisting}
\end{document}

In general utf8 and listings don't play well, especially if you importing files generated from different programs. The backticks, just enable whatever is enclosed between them to be escaped to LaTeX.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .