Latex ads with UTF-8 (or at least German umlauts)

Trying to include the source file in my latex document using a listing package, I have problems with German umlauts inside comments in the code. Using

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

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

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

in the original 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

+65
encoding utf-8 diacritics latex listings
Jul 12 '09 at 15:41
source share
21 answers

ok, now find a suitable workaround:

  • instead of an ad package, use listingsutf8

    \ usepackage {listingsutf8}

  • copy .sty lists to the folder where the document is located

  • find the following lines

      \ lst @ CCPutMacro
         \ lst @ ProcessOther {"23} \ #
         \ lst @ ProcessLetter {"24} \ textdollar
         \ lst @ ProcessOther {"25} \%
         \ lst @ ProcessOther {"26} \ & 
  • Enter the following lines there (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 {}} 
  • Save file

  • Using

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

to enable utf8 character to display latin1 character

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

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

+11
Jul 12 '09 at 21:15
source share

I found a simpler approach that works for me:

 \usepackage{listings} \lstset{ literate={ö}{{\"o}}1 {ä}{{\"a}}1 {ü}{{\"u}}1 } 
+57
May 6 '10 at 3:13
source share

For comments only, you can use the texcl option:

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

How your comments will become latex, and you can use "special" characters

 \begin{lstlisting} int iLink = 0x01; // Paramètre entrée \end{lstlisting} 
+32
Aug 21 2018-12-12T00:
source share

So, geht es (should work in other languages ​​- Spanish, Danish)

--- SNiP ---

 \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} 
+21
Apr 08 '10 at
source share

My contribution to the 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 } 
+13
Apr 18 '13 at 13:25
source share

My contribution to Brazilian Portuguese.

 \lstset{% inputencoding=utf8, extendedchars=true, literate=% {é}{{\'{e}}}1 {è}{{\`{e}}}1 {ê}{{\^{e}}}1 {ë}{{\¨{e}}}1 {É}{{\'{E}}}1 {Ê}{{\^{E}}}1 {û}{{\^{u}}}1 {ù}{{\`{u}}}1 {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}}1 {Å}{{\r A}}1 {€}{{\EUR}}1 {£}{{\pounds}}1 } 

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

+4
Mar 25 '15 at 15:59
source share

A simplified 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} 
+3
Dec 28
source share

Works great 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}} 
+2
May 28 '10 at 13:17
source share

Swedish version:

 \lstset{literate=% {å}{{\r{a}}}1 {ä}{{\"a}}1 {ö}{{\"o}}1 {Å}{{\r{A}}}1 {Ä}{{\"A}}1 {Ö}{{\"O}}1 } 
+2
Dec 13 '13 at
source share

I can confirm that the solution provided by Janos works almost correctly.

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

In any case, you should modify listing.sty in the local copy or directly in the shared 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 lists:

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

Hope this can help anyone, and maybe we can create a .sty list containing almost all UTF8 char ... :)

+1
Feb 09 '10 at 23:28
source share

Similar to the posts before (yaxz et al.) I defined macros for the Croatian language:

 % 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 a "small" problem with adding the keyword "inače". The following will not work for "inače":

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

The keyword "inače" (which is displayed in the code) is not recognized as a keyword. Any ideas?

Hope this helps someone (except me :)).

+1
Aug 08 '12 at 17:00
source share

My contribution to the 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 } 
+1
Mar 09 '14 at 12:59 on
source share

The author of the package suggests using the texcl option. Sometimes it helps, sometimes not.

0
Sep 29 '09 at 21:58
source share

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

Just use \usepackage{listings2} instead of \usepackage{listings} and this.

0
Jun 16 '11 at 18:27
source share

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} 
0
Dec 16 '12 at 18:53
source share

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 {ự}{\textsub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ụ}{\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 } 
0
Mar 22 '15 at 17:14
source share

We all know how LaTeX does not play well with Greek. However, this is my contribution, which requires several workarounds and does not include diacritics (but it 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 } 
0
Oct 15 '15 at 15:55
source share

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 } 
0
Aug 31 '16 at 15:05
source share

Just do not use UTF-8 in LaTex if you want to use list packages. Using latin1 in your documents will display German Umlauts very well.

I am writing a dissertation in German using this setting:

 % 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} 
-one
Jan 17
source share

You may need to set the document input encoding, also be UTF-8. There , the comp.text.tex thread discusses how to do this. The following does this on TeXLive on GNU / Linux:

 \usepackage[utf8]{inputenc} 
-3
Jul 12 '09 at 20:10
source share



All Articles