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

EncodingLatexUtf 8DiacriticsListings

Encoding Problem Overview


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

Encoding Solutions


Solution 1 - Encoding

I found a simpler approach, which works for me:

\usepackage{listings}

\lstset{
  literate={ö}{{\"o}}1
           {ä}{{\"a}}1
           {ü}{{\"u}}1
}

Solution 2 - Encoding

For comments only, you can use the texcl option:

\lstset{language=C++,texcl=true}

Than your comments become Latex and you can use "special" characters

\begin{lstlisting}
int iLink = 0x01; // Paramètre entrée
\end{lstlisting}

Solution 3 - Encoding

This should work for other languages (Spanish, Danish) as well:

\documentclass[
a4paper, %% defines the paper size: a4paper (default), a5paper, letterpaper, ...
12pt %% set default font size to 12 point
]{scrartcl} %% article, see KOMA documentation (scrguide.dvi)

\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage{listings}

\lstset{language=Pascal}
\lstset{literate=%
{Ö}{{\"O}}1
{Ä}{{\"A}}1
{Ü}{{\"U}}1
{ß}{{\ss}}2
{ü}{{\"u}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
}

\begin{document}

[Latex: kann man Umlaute in lstlisting verwenden?]
\begin{lstlisting}
Test für Umlaut äöü ÄÖÜ ß So geht es
\end{lstlisting}

\end{document} 

Solution 4 - Encoding

My contribution for Czech language.

\lstset{
    inputencoding=utf8,
    extendedchars=true,
    literate=%
    {á}{{\'a}}1
    {č}{{\v{c}}}1
    {ď}{{\v{d}}}1
    {é}{{\'e}}1
    {ě}{{\v{e}}}1
    {í}{{\'i}}1
    {ň}{{\v{n}}}1
    {ó}{{\'o}}1
    {ř}{{\v{r}}}1
    {š}{{\v{s}}}1
    {ť}{{\v{t}}}1
    {ú}{{\'u}}1
    {ů}{{\r{u}}}1
    {ý}{{\'y}}1
    {ž}{{\v{z}}}1
    {Á}{{\'A}}1
    {Č}{{\v{C}}}1
    {Ď}{{\v{D}}}1
    {É}{{\'E}}1
    {Ě}{{\v{E}}}1
    {Í}{{\'I}}1
    {Ň}{{\v{N}}}1
    {Ó}{{\'O}}1
    {Ř}{{\v{R}}}1
    {Š}{{\v{S}}}1
    {Ť}{{\v{T}}}1
    {Ú}{{\'U}}1
    {Ů}{{\r{U}}}1
    {Ý}{{\'Y}}1
    {Ž}{{\v{Z}}}1
}

Solution 5 - Encoding

My contribution for brazilian portuguese.

\lstset{%
        inputencoding=utf8,
        extendedchars=true,
        literate=%
        {é}{{\'{e}}}1
        {è}{{\`{e}}}1
        {ê}{{\^{e}}}1
        {ë}{{\¨{e}}}1
        {É}{{\'{E}}}1
        {Ê}{{\^{E}}}1
        {û}{{\^{u}}}1
        {ù}{{\`{u}}}1
        {ú}{{\'{u}}}1
        {â}{{\^{a}}}1
        {à}{{\`{a}}}1
        {á}{{\'{a}}}1
        {ã}{{\~{a}}}1
        {Á}{{\'{A}}}1
        {Â}{{\^{A}}}1
        {Ã}{{\~{A}}}1
        {ç}{{\c{c}}}1
        {Ç}{{\c{C}}}1
        {õ}{{\~{o}}}1
        {ó}{{\'{o}}}1
        {ô}{{\^{o}}}1
        {Õ}{{\~{O}}}1
        {Ó}{{\'{O}}}1
        {Ô}{{\^{O}}}1
        {î}{{\^{i}}}1
        {Î}{{\^{I}}}1
        {í}{{\'{i}}}1
        {Í}{{\~{Í}}}1
}

Solution 6 - Encoding

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.

Solution 7 - Encoding

Works for most iso-characters. (including swedish åäöÅÄÖ which is what I needed)

\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstset{literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\EUR}}1 {£}{{\pounds}}1
}

source: http://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings#Encoding_issue

Solution 8 - Encoding

Simpler approach for french.

\documentclass{article}
	\usepackage[utf8]{inputenc}
	\usepackage[francais]{babel}
	\usepackage[T1]{fontenc}
	\usepackage{listings}
	\lstloadlanguages{R}
	\lstset{language=R}
	\lstset{%
		inputencoding=utf8,
	        extendedchars=true,
	        literate=%
	        {é}{{\'{e}}}1
	        {è}{{\`{e}}}1
	        {ê}{{\^{e}}}1
	        {ë}{{\¨{e}}}1
	        {û}{{\^{u}}}1
	        {ù}{{\`{u}}}1
	        {â}{{\^{a}}}1
	        {à}{{\`{a}}}1
	        {î}{{\^{i}}}1
            {ô}{{\^{o}}}1
	        {ç}{{\c{c}}}1
	        {Ç}{{\c{C}}}1
	        {É}{{\'{E}}}1
	        {Ê}{{\^{E}}}1
	        {À}{{\`{A}}}1
	        {Â}{{\^{A}}}1
	        {Î}{{\^{I}}}1
    }
    	
	\begin{document}
	\begin{lstlisting}
	# Est-ce que ça marche : é ê è à Â Ê É Î ç Ç
	x<-rnorm(100,0,10)
	plot(density(x))
	\end{lstlisting}
	\end{document}

Solution 9 - Encoding

Works fine for me!

My 2 cents for the French side:

\lst@ProcessOther {"C0}{\`{A}}
\lst@ProcessOther {"C1}{\'{A}}
\lst@ProcessOther {"C2}{\^{A}}
\lst@ProcessOther {"C4}{\"{A}}
\lst@ProcessOther {"C7}{\c{C}}
\lst@ProcessOther {"C8}{\`{E}}
\lst@ProcessOther {"C9}{\'{E}}
\lst@ProcessOther {"CA}{\^{E}}
\lst@ProcessOther {"CB}{\"{E}}
\lst@ProcessOther {"CE}{\^{I}}
\lst@ProcessOther {"CF}{\"{I}}
\lst@ProcessOther {"D4}{\^{O}}
\lst@ProcessOther {"D6}{\"{O}}
\lst@ProcessOther {"D9}{\`{U}}
\lst@ProcessOther {"DB}{\^{U}}
\lst@ProcessOther {"E0}{\`{a}}
\lst@ProcessOther {"E1}{\'{a}}
\lst@ProcessOther {"E2}{\^{a}}
\lst@ProcessOther {"E4}{\"{a}}
\lst@ProcessOther {"E7}{\c{c}}
\lst@ProcessOther {"E8}{\`{e}}
\lst@ProcessOther {"E9}{\'{e}}
\lst@ProcessOther {"EA}{\^{e}}
\lst@ProcessOther {"EB}{\"{e}}
\lst@ProcessOther {"EE}{\^{\i}}
\lst@ProcessOther {"EF}{\"{\i}}
\lst@ProcessOther {"F4}{\^{o}}
\lst@ProcessOther {"F6}{\"{o}}
\lst@ProcessOther {"F9}{\`{u}}
\lst@ProcessOther {"FB}{\^{u}}

Solution 10 - Encoding

Similarly to posts before (yaxz and others) I defined macros for Croatian:

% on top
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[croatian]{babel}

% add listings package
\usepackage{listings}

% set font translations
\lstset{inputencoding=utf8}
\lstset{extendedchars=true}
\lstset{
	literate=%
	{ć}{{\'c}}1
	{č}{{\v{c}}}1
	{đ}{{\dj{}}}1
	{š}{{\v{s}}}1
	{ž}{{\v{z}}}1
	{Ć}{{\'C}}1
	{Č}{{\v{C}}}1
	{Đ}{{\DJ{}}}1
	{Š}{{\v{S}}}1
	{Ž}{{\v{Z}}}1
}

I still have "little" problem with adding keyword "inače". Following won't work for "inače":

\lstset{morekeywords={ponavljaj, ako, inače, dok}}

Keyword "inače" (that appear in code) is not recognized as keyword. Any ideas?

Hope this will help someone (besides me :)).

Solution 11 - Encoding

The Swedish version:

\lstset{literate=%
{å}{{\r{a}}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
{Å}{{\r{A}}}1
{Ä}{{\"A}}1
{Ö}{{\"O}}1
}

Solution 12 - Encoding

My contribution for Lithuanian language:

\lstset{%
    literate=%
     {ą}{{\k{a}}}1
     {č}{{\v{c}}}1
     {ę}{{\k{e}}}1
     {ė}{{\.{e}}}1
     {į}{{\k{i}}}1
     {š}{{\v{s}}}1
     {ų}{{\k{u}}}1
     {ū}{{\={u}}}1
     {ž}{{\v{z}}}1
     {Ą}{{\k{A}}}1
     {Č}{{\v{C}}}1
     {Ę}{{\k{E}}}1
     {Ė}{{\.{E}}}1
     {Į}{{\k{I}}}1
     {Š}{{\v{S}}}1
     {Ų}{{\k{U}}}1
     {Ū}{{\={U}}}1
     {Ž}{{\v{Z}}}1
}

Solution 13 - Encoding

For Polish:

\lstset{%
literate=%
 {ą}{{\k{a}}}1
 {ę}{{\k{e}}}1
 {Ą}{{\k{A}}}1
 {Ę}{{\k{E}}}1
 {ś}{{\'{s}}}1
 {Ś}{{\'{S}}}1
 {ź}{{\'{z}}}1
 {Ź}{{\'{Z}}}1
 {ń}{{\'{n}}}1
 {Ń}{{\'{N}}}1
 {ć}{{\'{c}}}1
 {Ć}{{\'{C}}}1
 {ó}{{\'{o}}}1
 {Ó}{{\'{O}}}1
 {ż}{{\.{z}}}1
 {Ż}{{\.{Z}}}1
 {ł}{{\l{}}}1
 {Ł}{{\l{}}}1
}

And here's list of symbols so you can create it for your language: https://en.wikibooks.org/wiki/LaTeX/Special_Characters

Solution 14 - Encoding

I can confirm that solution provided by Janosch works almost correctly.

In my case I needed to use Spanish characters: á,é,í,ó,ú,ñ,Á,É,Í,Ó,Ú and tried his solution without results until I compiled my file with xelatex instead of pdflatex.

Anyway you should modify listings.sty either in your local copy or directly in the common file and add the following:

\lst@CCPutMacro
    \lst@ProcessOther {"23}\#
    \lst@ProcessLetter{"24}\textdollar
    \lst@ProcessOther {"25}\%
    \lst@ProcessOther {"26}\&
        %spanish letters coded in UTF
    \lst@ProcessOther {"E1}{\'a}
    \lst@ProcessOther {"C1}{\'A}
    \lst@ProcessOther {"E9}{\'e}
    \lst@ProcessOther {"C9}{\'E}
    \lst@ProcessOther {"ED}{\'i}
    \lst@ProcessOther {"CD}{\'I}
    \lst@ProcessOther {"F3}{\'o}
    \lst@ProcessOther {"D3}{\'O}
    \lst@ProcessOther {"FA}{\'u}
    \lst@ProcessOther {"DA}{\'U}
    \lst@ProcessOther {"F1}{ñ}
    \lst@ProcessOther {"D1}{Ñ}

In my .tex file I used the following options for listings:

\usepackage{listingsutf8}
\lstset{
        inputencoding=utf8,
        extendedchars=\true}

Hope it can help anybody and maybe we can construct a listing.sty containing almost every UTF8 char... :)

Solution 15 - Encoding

We all know how LaTeX doesn’t play nice with Greek. Nevertheless, here’s my contribution, which requires several workarounds, and doesn’t include diacritics (but works!):

\usepackage[greek]{babel}
\usepackage{textgreek}
\usepackage{listings}

% Notice that here it is spelled ‘omikron’, not ‘omicron’,
% and that I use ‘\textmugreek’ instead of ‘\textmu’,
% which are both part of the textgreek package, but the
% ‘normal’ \textmu conflicts with the textcomp package as
% that uses another font for \textmu.
\DeclareUnicodeCharacter{03BF}{\textomikron}
\DeclareUnicodeCharacter{039F}{\textOmikron}
\DeclareUnicodeCharacter{03BC}{\textmugreek}

\lstset{
    inputencoding=utf8,
    extendedchars=true,
    literate=
    {α}{{\textalpha}}1   {Α}{{\textAlpha}}1
    {β}{{\textbeta}}1    {Β}{{\textBeta}}1
    {γ}{{\textgamma}}1   {Γ}{{\textGamma}}1
    {δ}{{\textdelta}}1   {Δ}{{\textDelta}}1
    {ε}{{\textepsilon}}1 {Ε}{{\textEpsilon}}1
    {ζ}{{\textzeta}}1    {Ζ}{{\textZeta}}1
    {η}{{\texteta}}1     {Η}{{\textEta}}1
    {θ}{{\texttheta}}1   {Θ}{{\textTheta}}1
    {ι}{{\textiota}}1    {Ι}{{\textIota}}1
    {κ}{{\textkappa}}1   {Κ}{{\textKappa}}1
    {λ}{{\textlambda}}1  {Λ}{{\textLambda}}1
    {μ}{{\textmugreek}}1 {Μ}{{\textMu}}1
    {ν}{{\textnu}}1      {Ν}{{\textNu}}1
    {ξ}{{\textxi}}1      {Ξ}{{\textXi}}1
    {ο}{{\textomikron}}1 {Ο}{{\textOmikron}}1
    {π}{{\textpi}}1      {Π}{{\textPi}}1
    {ρ}{{\textrho}}1     {Ρ}{{\textRho}}1
    {σ}{{\textsigma}}1   {Σ}{{\textSigma}}1   {ς}{{\textvarsigma}}1
    {τ}{{\texttau}}1     {Τ}{{\textTau}}1
    {υ}{{\textupsilon}}1 {Υ}{{\textUpsilon}}1
    {φ}{{\textphi}}1     {Φ}{{\textPhi}}1
    {χ}{{\textchi}}1     {Χ}{{\textChi}}1
    {ψ}{{\textpsi}}1     {Ψ}{{\textPsi}}1
    {ω}{{\textomega}}1   {Ω}{{\textOmega}}1
}

Solution 16 - Encoding

My contribution for spanish language.

Tested, it works great, both importing files with the code with spanish characters, and pasting snippets of code directly in the latex document.

\usepackage[spanish, activeacute]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}

\lstset{literate=%
    {ñ}{{\~n}}1
    {Ñ}{{\~N}}1
    {á}{{\'a}}1
    {é}{{\'e}}1
    {í}{{\'i}}1
    {ó}{{\'o}}1
    {ú}{{\'u}}1
    {Á}{{\'A}}1
    {É}{{\'E}}1
    {Í}{{\'I}}1
    {Ó}{{\'O}}1
    {Ú}{{\'U}}1
}

Solution 17 - Encoding

Author of package suggests using texcl option. Sometimes it helps, sometimes doesn't.

Solution 18 - Encoding

Simply don't use UTF-8 in LaTex if you want to use any listings packages. Using latin1 in your documents will display German Umlauts just fine.

I'm writing my thesis in German using this setup:

% Your language, here German
\usepackage[ngerman]{babel} 
% Will work with Umlauts
\usepackage[latin1]{inputenc}
% Euro characters etc.
\usepackage{textcomp}
% Works perfectly with latin1
\usepackage{listings}

Solution 19 - Encoding

The cleanest solution for this is to use the listings2 package: http://www.atscire.de/index.php?nav=products/listings2

Simply use \usepackage{listings2} instead of \usepackage{listings} and that's it.

Solution 20 - Encoding

Norwegian letters for listings.sty in @Janosch's solution:

\lst@ProcessLetter{"C5}{\AA}
\lst@ProcessLetter{"C6}{\AE}
\lst@ProcessLetter{"D8}{\O}
\lst@ProcessLetter{"E5}{\aa}
\lst@ProcessLetter{"E6}{\ae}
\lst@ProcessLetter{"F8}{\o}

Solution 21 - Encoding

For Vietnamese:

\usepackage{vntex}
\usepackage[utf8]{inputenc}
\usepackage{listings}  
\usepackage{tipa}

\lstset{columns=fullflexible,literate=
{đ}{{\dj}}1
{â}{{\^a}}1
{ă}{{\u{a}}}1
{ê}{{\^e}}1
{ô}{{\^o}}1
{ơ}{{\ohorn}}1
{ư}{{\uhorn}}1
{á}{{\'a}}1
{à}{{\`a}}1
{ả}{\h{a}}1
{ã}{{\~a}}1
{ạ}{\textsubdot{a}}1
{ấ}{\'{\^a}}1
{ầ}{\`{\^a}}1
{ẩ}{\h{\^a}}1
{ẫ}{\~{\^a}}1
{ậ}{\textsubdot{\^a}}1
{ắ}{\'{\u{a}}}1
{ằ}{\`{\u{a}}}1
{ẳ}{\h{\u{a}}}1
{ẵ}{\~{\u{a}}}1
{ặ}{\textsubdot{\u{a}}}1
{é}{{\'e}}1
{è}{{\`e}}1
{ẻ}{\h{e}}1
{ẽ}{{\~e}}1
{ẹ}{\textsubdot{e}}1
{ế}{\'{\^e}}1
{ề}{\`{\^e}}1
{ể}{\h{\^e}}1
{ễ}{\~{\^e}}1
{ệ}{\textsubdot{\^{e}}}1
{í}{{\'i}}1
{ì}{{\`i}}1
{ỉ}{\h{i}}1
{ĩ}{{\~i}}1
{ị}{\textsubdot{i}}1
{ó}{{\'o}}1
{ò}{{\`o}}1
{ỏ}{\h{o}}1
{õ}{{\~o}}1
{ọ}{\textsubdot{o}}1
{ố}{\'{\^o}}1
{ồ}{\`{\^o}}1
{ổ}{\h{\^o}}1
{ỗ}{\~{\^o}}1
{ộ}{\textsubdot{\^o}}1
{ớ}{\'{\ohorn}}1
{ờ}{\`{\ohorn}}1
{ở}{\h{\ohorn}}1
{ỡ}{\~{\ohorn}}1
{ợ}{\textsubdot{\ohorn}}1
{ú}{{\'u}}1
{ù}{{\`u}}1
{ủ}{\h{u}}1
{ũ}{{\~u}}1
{ụ}{\textsubdot{u}}1
{ứ}{\'{\uhorn}}1
{ừ}{\`{\uhorn}}1
{ử}{\h{\uhorn}}1
{ữ}{\~{\uhorn}}1
{ự}{\textsubdot{\uhorn}}1
{ý}{{\'y}}1
{ỳ}{{\`y}}1
{ỷ}{\h{y}}1
{ỹ}{{\~y}}1
{ỵ}{\textsubdot{y}}1
{Đ}{{\DJ}}1
{Â}{{\^A}}1
{Ă}{{\u{A}}}1
{Ê}{{\^E}}1
{Ô}{{\^O}}1
{Ơ}{{\OHORN}}1
{Ư}{{\UHORN}}1
{Á}{{\'A}}1
{À}{{\`A}}1
{Ả}{\h{A}}1
{Ã}{{\~A}}1
{Ạ}{\textsubdot{A}}1
{Ấ}{\'{\^A}}1
{Ầ}{\`{\^A}}1
{Ẩ}{\h{\^A}}1
{Ẫ}{\~{\^A}}1
{Ậ}{\textsubdot{\^A}}1
{Ắ}{\'{\u{A}}}1
{Ằ}{\`{\u{A}}}1
{Ẳ}{\h{\u{A}}}1
{Ẵ}{\~{\u{A}}}1
{Ặ}{\textsubdot{\u{A}}}1
{É}{{\'E}}1
{È}{{\`E}}1
{Ẻ}{\h{E}}1
{Ẽ}{{\~E}}1
{Ẹ}{\textsubdot{E}}1
{Ế}{\'{\^E}}1
{Ề}{\`{\^E}}1
{Ể}{\h{\^E}}1
{Ễ}{\~{\^E}}1
{Ệ}{\textsubdot{\^{E}}}1
{Í}{{\'I}}1
{Ì}{{\`I}}1
{Ỉ}{\h{I}}1
{Ĩ}{{\~I}}1
{Ị}{\textsubdot{I}}1
{Ó}{{\'O}}1
{Ò}{{\`O}}1
{Ỏ}{\h{O}}1
{Õ}{{\~O}}1
{Ọ}{\textsubdot{O}}1
{Ố}{\'{\^O}}1
{Ồ}{\`{\^O}}1
{Ổ}{\h{\^O}}1
{Ỗ}{\~{\^O}}1
{Ộ}{\textsubdot{\^O}}1
{Ớ}{\'{\OHORN}}1
{Ờ}{\`{\OHORN}}1
{Ở}{\h{\OHORN}}1
{Ỡ}{\~{\OHORN}}1
{Ợ}{\textsubdot{\OHORN}}1
{Ú}{{\'U}}1
{Ù}{{\`U}}1
{Ủ}{\h{U}}1
{Ũ}{{\~U}}1
{Ụ}{\textsubdot{U}}1
{Ứ}{\'{\UHORN}}1
{Ừ}{\`{\UHORN}}1
{Ử}{\h{\UHORN}}1
{Ữ}{\~{\UHORN}}1
{Ự}{\textsubdot{\UHORN}}1
{Ý}{{\'Y}}1
{Ỳ}{{\`Y}}1
{Ỷ}{\h{Y}}1
{Ỹ}{{\~Y}}1
{Ỵ}{\textsubdot{Y}}1
  }

Solution 22 - Encoding

For Icelandic:

\lstset{
  literate=%
    {Á}{{\'A}}1
    {á}{{\'a}}1
    {Ð}{{\dh}}1
    {ð}{{\dh}}1
    {É}{{\'E}}1
    {é}{{\'e}}1
    {í}{{\'i}}1
    {Í}{{\'I}}1
    {Ó}{{\'O}}1
    {ó}{{\'o}}1
    {Ú}{{\'U}}1
    {ú}{{\'u}}1
    {Ý}{{\'Y}}1
    {ý}{{\'y}}1
    {Þ}{{\TH}}1
    {þ}{{\th}}1
    {Æ}{{\AE}}1
    {æ}{{\ae}}1
    {Ö}{{\"O}}1
    {ö}{{\"o}}1
}

Solution 23 - Encoding

You might need to set the document's input encoding to also be UTF-8. There's a comp.text.tex thread discussing how to do this. The following accomplishes this in TeXLive on GNU/Linux:

\usepackage[utf8]{inputenc}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJanoschView Question on Stackoverflow
Solution 1 - EncodingyaxuView Answer on Stackoverflow
Solution 2 - EncodingSeb DA ROCHAView Answer on Stackoverflow
Solution 3 - EncodingHeimo AdelsbergerView Answer on Stackoverflow
Solution 4 - EncodingTomáš LinhartView Answer on Stackoverflow
Solution 5 - EncodingMarcelo LacerdaView Answer on Stackoverflow
Solution 6 - EncodingJanoschView Answer on Stackoverflow
Solution 7 - EncodingMetalGodwinView Answer on Stackoverflow
Solution 8 - EncodingfxiView Answer on Stackoverflow
Solution 9 - EncodingPhilView Answer on Stackoverflow
Solution 10 - EncodingLeonardoView Answer on Stackoverflow
Solution 11 - Encodinguser2916607View Answer on Stackoverflow
Solution 12 - EncodingniekasView Answer on Stackoverflow
Solution 13 - EncodingFilip KowalView Answer on Stackoverflow
Solution 14 - EncodingJabotoView Answer on Stackoverflow
Solution 15 - Encodinguser4664340View Answer on Stackoverflow
Solution 16 - EncodingSergio SanzView Answer on Stackoverflow
Solution 17 - EncodingVanuanView Answer on Stackoverflow
Solution 18 - EncodingAlain M. LafonView Answer on Stackoverflow
Solution 19 - EncodingrfzeroView Answer on Stackoverflow
Solution 20 - EncodingsindrejhView Answer on Stackoverflow
Solution 21 - EncodingKhoa NguyenView Answer on Stackoverflow
Solution 22 - EncodingofurkusiView Answer on Stackoverflow
Solution 23 - EncodingMichael EkstrandView Answer on Stackoverflow