encodinglatexutf-8diacriticslistings

Listings in Latex with UTF-8 (or at least german umlauts)


Trying to include a source-file into my latex document using the listings package, i got problems with german umlauts inside of the comments in the code. Using

\lstset{
extendedchars=\true,
inputencoding=utf8x
}

Umlauts in the source files (encoded in UTF-8 without BOM) are processed, but they are somehow moved to the beginning of the word they are contained in. So

// die Größe muss berücksichtigt werden

in the input source file, becomes

// die ößGre muss übercksichtigt werden

in the output file.

NOTE: since i found errors in my initial setup, i heavily edited this question


Solution

  • ok, found kinda workaround now:

    1. instead of listings package, use listingsutf8

      \usepackage{listingsutf8}

    2. copy listings.sty to the folder the document resides

    3. find the following lines

      \lst@CCPutMacro
          \lst@ProcessOther {"23}\#
          \lst@ProcessLetter{"24}\textdollar
          \lst@ProcessOther {"25}\%
          \lst@ProcessOther {"26}\&
    4. Enter there the following lines (each "registers" one umlaut)

      \lst@ProcessLetter{"E4}{\"a}
      \lst@ProcessLetter{"F6}{\"o}
      \lst@ProcessLetter{"FC}{\"u}
      \lst@ProcessLetter{"C4}{\"A}
      \lst@ProcessLetter{"D6}{\"O}
      \lst@ProcessLetter{"DC}{\"U}
      \lst@ProcessLetter{"DF}{\ss{}}
    5. Save the file

    6. Use

      \lstset{
          extendedchars=\true,
          inputencoding=utf8/latin1
      }

    to enable utf8 character to latin1 character mapping

    1. Convert line endings of your source file from windows (\r\n) to unix (\n)
    2. enjoy

    I know this is ugly in many way, but its the only solution that works for me so far.