40

I'm trying to insert source code in LaTeX with using listings, but my code has accents and pdflatex gives me this error:

Unicode char \u8:�\expandafter not set up for use with LaTeX.

This is a code the reproduces the error.

example.tex

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

\begin{document}

\lstinputlisting[language=C++]{code.cpp}

\end{document}

code.cpp

// Código con una tilde

What can I do to generate the PDF correctly?

Werner
  • 603,163
kevinz
  • 555
  • 1
  • 4
  • 5

8 Answers8

29

You can load the package listingsutf8 and then set the encoding using \lstset{inputencoding=utf8/latin1}:

\documentclass{article}

\usepackage{listingsutf8}
\usepackage[spanish]{babel}

\begin{document}

\lstset{inputencoding=utf8/latin1}
\lstinputlisting[language=C++]{code.cpp}

\end{document}
Jake
  • 232,450
  • Thanks for your answer. With the second method the ó is not in its right place, but the first method works perfectly!. – kevinz Oct 05 '11 at 05:06
  • You're right, I didn't notice the slight misalignment. I've removed the second method. – Jake Oct 05 '11 at 05:08
  • 7
    the accpeted solution doesn´t work for me. – Mika Ike Jun 21 '14 at 09:44
  • 2
    This doesn't seem to work with \usepackage{listings} \usepackage{listingsutf8} \usepackage[french]{babel} and \begin{lstlisting}[breaklines]. Although I'm not getting compilation errors, the french characters aren't displayed properly. – JoErNanO Nov 09 '15 at 10:18
25

The accepted solution didn't work for me, so here is mine:

\usepackage{listings} % ordinary listings package

and then configure letter mappings - these mappings are for the Czech languaguge but you can configure your own

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

In Brazilian portuguese, this solution worked for me

\usepackage{listings} % for including code

\lstset{
language=R,   % R code
literate=
{á}{{\'a}}1
{à}{{\`a}}1
{ã}{{\~a}}1
{é}{{\'e}}1
{ê}{{\^e}}1
{í}{{\'i}}1
{ó}{{\'o}}1
{õ}{{\~o}}1
{ú}{{\'u}}1
{ü}{{\"u}}1
{ç}{{\c{c}}}1
}
4

Hungarian version based on previous answer:

\lstset{ literate={á}{{\'a}}1 {é}{{\'e}}1 {í}{{{\'\i}}}1 {ó}{{\'o}}1 {ö}{{\"o}}1 {ő}{{\H o}}1 {ú}{{\'u}}1 {Ú}{{\'U}}1 {ü}{{\"u}}1 {ű}{{\H u}}1 {Ü}{{\"U}}1 }

LaTex magyar ékezetes karakterek.

4

None of the above worked here, just this way:

\usepackage[T1]{fontenc}            
\usepackage[brazil]{babel}
\usepackage[utf8]{inputenc} 

\usepackage{listings}

\lstset{
    language=bash,   %% PHP, C, Java, etc... bash is the standard
    extendedchars=true,
    inputencoding=latin1
}
Andrey
  • 41
  • This last solution does not work if your source code is in utf-8 and you replace latin1 by utf8 ;-( – Tsf Jun 23 '17 at 13:47
3

Set for Polish language:

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

Set for the Greek language:

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

Set for French language :

\lstset{literate=%
{é}{{\'e}}{1}%
{è}{{\`e}}{1}%
{à}{{\`a}}{1}%
{ç}{{\c{c}}}{1}%
{œ}{{\oe}}{1}%
{ù}{{\`u}}{1}%
{É}{{\'E}}{1}%
{È}{{\`E}}{1}%
{À}{{\`A}}{1}%
{Ç}{{\c{C}}}{1}%
{Œ}{{\OE}}{1}%
{Ê}{{\^E}}{1}%
{ê}{{\^e}}{1}%
{î}{{\^i}}{1}%
{ô}{{\^o}}{1}%
{û}{{\^u}}{1}%
{ä}{{\"{a}}}1
{ë}{{\"{e}}}1
{ï}{{\"{i}}}1
{ö}{{\"{o}}}1
{ü}{{\"{u}}}1
{û}{{\^{u}}}1
{â}{{\^{a}}}1
{Â}{{\^{A}}}1
{Î}{{\^{I}}}1
} 
  • Welcome to SE. Notice that the question you answered to was very old. In this day and age, that sort of workaround is no longer necessary. I've been using plain listings with regional characters (including French) for years without any need to any configuration. So unless you're stuck on a very old system, using those workarounds should be avoided. – Miyase Jun 04 '22 at 16:35
  • On overleaf I'm still getting this issue @Miyase – Butanium Jun 15 '22 at 16:55
  • @Butanium Then you probably have a problem elsewhere in your preamble. I have my students use Overleaf and most of them have code typeset with listings, and the workaround you mention has never been necessary. You should make a pass over your packages and check with CTAN if some of them were deprecated. – Miyase Jun 15 '22 at 17:16
  • Maybe, I'm using a ieee template so I a lot of packages are included by default – Butanium Jun 15 '22 at 17:26