16

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

25

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...

  • 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 Jul 25 '17 at 15:07
  • I'm not sure what you mean. I can use ÖÄÜßüäö~ with no problems. – someonr Jul 26 '17 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 Jul 26 '17 at 11:06
11

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.

8

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.