9

I need to copy and past some UTF8 code into a lstlisting, but unfortunately, I receive the following error :

! Package inputenc Error: Unicode char \u8:�\lst@FillFixed@\lst@EC� not set up for use with LaTeX.

Keyboard character used is undefined

What is weird was that I didn't noticed any weird characters in my lstlisting code (it's some bash code). Furthermore I got the package in header:

 \usepackage[utf8]{inputenc}

What should I do?

  • 3
    Please check the following TeX.sx link: tex.stackexchange.com/q/25391/19384. It explains the problem an dhow to get UTF8 into the lstlistings – Peter Jansson Jan 6 '13 at 22:07
  • 1
    A related potential issue is with \lstinputlisting:that you've used an editor that's been too clever, and the file that you thought wasn't utf8 now is, with some odd unicode characters hidden by the editor at the start. I was getting the same error message as this on the first line, whatever I put at the start of the file. The � may be a real unicode character U+FFFD:REPLACEMENT CHARACTER in the output. – Chris H Mar 14 '14 at 13:20
13

I just solved it in using

  inputencoding=latin1

I hope it can help some in the same case than me.

| improve this answer | |
  • 2
    Sorry, but where did you put this? – Bex Jan 9 '17 at 18:54
  • 1
    @Bex You put this as an optional argument of \lstinputlisting. For instance, if my "test.m" MATLAB file just contains the text "% é", I use the following: \lstinputlisting[inputencoding=latin1]{test.m} and it works – R. Bourgeon May 27 '17 at 15:30
10

listings cannot handle UTF8-characters. Search in the docs for the keyword literate. Then you'll see how UTF-8 characters can be mapped to TeX commands, eg ü->\"u: \lstset{literate={ü}{{\"u}}1}

| improve this answer | |
3

I solved adding texcl=true to the \lstset configuration.

| improve this answer | |
0

Some of these may help (probably you don't need all these lines):

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

\lstset{literate=% Allow for German characters in lstlistings.
{Ö}{{\"O}}1
{Ä}{{\"A}}1
{Ü}{{\"U}}1
{ß}{{\ss}}2
{ü}{{\"u}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
}

Here you'll find similar code for lots of other languages and characters, and some other hints to this same problem: https://stackoverflow.com/questions/1116266/listings-in-latex-with-utf-8-or-at-least-german-umlauts/15450398

P.S. Whenever you use lstlisting, you usually want to add something like this for other reasons (they don't affect your problem):

\usepackage{listings} 
\lstset{
basicstyle=\small\ttfamily,  %Looks much better.
columns=flexible, %Without this you get fixed-width fonts, which look bad.
breaklines=true,   %End-of-line wrap - you really want this, usually.
language=tex   %Percent comments in cursive, so you distinguish them from the commands. Other languages are also available, such as C and Matlab.
}
| improve this answer | |

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.