10

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?

2
  • 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 Commented Jan 6, 2013 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
    Commented Mar 14, 2014 at 13:20

4 Answers 4

14

I just solved it in using

  inputencoding=latin1

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

2
  • 4
    Sorry, but where did you put this?
    – Bex
    Commented Jan 9, 2017 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 Commented May 27, 2017 at 15:30
12

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}

7

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

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.
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .