# Encoding problem AFTER using listings

I'm trying to insert some code in a document, and I'm using the package listings for that. I'm well aware that inserting special utf8 characters requires some precautions that can be set with \lstset and the literate parameter.

It works, except the the "regular" texts that follows the block of code. I also tried with listingsutf8, inline or with \lstinputlisting. It still fails.

Here is a minimal example.

\documentclass[a4paper,11pt]{article}

\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage[utf8x]{inputenc}
\usepackage[francais]{babel}
\usepackage{listings}

\lstset{language=c++,
keywordstyle=\bf,
frame=single,
numbers=left,
tabsize=2,
inputencoding=utf8,
extendedchars=true,
literate={é}{{\'{e}}}1
}

\begin{document}

Accentué %works fine : document in utf8

\begin{lstlisting}code working fine
int i = 2; //accentué
\end{lstlisting}

Accentué %fails (if this line is removed, the code compiles)

\end{document}


How can I avoid that ?

• Just use encoding utf8 not utf8x. Incidentally, the language option to babel is now french not francais Nov 10 '17 at 13:13

The problem is the utfx8 encoding, that has many issues. For French it is sufficient to use utf8. Incidentally, the babel option is now called french, not francais which is likely to disappear soon.

\documentclass[a4paper,11pt]{article}

\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{listings}

\lstset{language=c++,
keywordstyle=\bf,
frame=single,
numbers=left,
tabsize=2,
inputencoding=utf8,
extendedchars=true,
literate={é}{{\'{e}}}1
}

\begin{document}

Accentué

\begin{lstlisting}
code working fine
int i = 2; //accentué
\end{lstlisting}

Accentué

\end{document}