Problem with 'é' character in listings

I have this MWE

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{colortbl}
\usepackage{lmodern}
\usepackage{listings}
\usepackage{color}

\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
breaklines=true,
breakatwhitespace=true,
tabsize=3,
extendedchars=true,
postbreak=\space, breakindent=5pt, breaklines,
literate={à}{{\a}}1 {è}{{\e}}1 {é}{{\'e}}1 {ê}{{\^e}}1 {ù}{{\u}}1,
}

\begin{document}

\begin{lstlisting}
//...
API api = new API(url);
CultureIntermediaire cultureIntermediaire = resultat.getCultureIntermediaire();

//On affiche le nom de la méthode de travail du sol la plus érosive

\end{lstlisting}

\end{document}


Result

I have a problem : the "é" character is alone

Is there a way to tell listing to wrap when it find a point and not only a space. I want to get something like this :

CultureIntermediaire cultureIntermediaire = resultat.
getCultureIntermediaire();

• Unfortunately listings doesn't really support UTF-8 characters in comments. Maybe using listingsutf8 can help; however it requires the listing is in a separate file and is input with \lstinputlisting. Jul 4 '14 at 10:40
• I think that's the only solution I have. The problem is that I will have many file of code and each file will contain only about 4 lines of code. Jul 4 '14 at 11:57

For the line break at period, you can do as the follow:

literate= {.}{}{1\discretionary{.}{}{.}},


Which does the line break at the ., however, it doesn't do it so beautifully. To tweak the output, have a look at this answer. This part of the answer tells you something: literate is some sort of hack to put unicode characters. Another solution could be to escape as Gonzalo suggests in this answer.

\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
breaklines=true,
breakatwhitespace=true,
tabsize=3,
extendedchars=true,
postbreak=\space, breakindent=5pt, breaklines,
escapeinside={\%*}{*)}, % I'm new here!
literate= {.}{}{1\discretionary{.}{}{.}}, % So am I
}


with \usepackage{listingsutf8} as was suggested in the comment by egreg. You then have to replace your comment with the following

// %*On affiche le nom de la méthode de travail du sol la plus érosive!*)


that renders to

P.S. It is a good practice to have your code in a separate file and add it to listings with lstinputlisting. This not only makes the project more organizable, but also prevents some of the headaches related to listings. (for example, check the code in this answer to see how to use that command).

• When I escape my comment, the comment are no longer considered as comment and are printed in black (not in green in my case). Jul 4 '14 at 11:55
• What comes between escape characters are considered as latex code and you can use latex commands to colorize escaped comments (\textcolor{green}{your comment}. Unescaped comments would be fine btw). I know this is an inconvenience but as egreg mentioned, listings is not very friendly with utf8. You may consider minted package but in all honesty, I'm not familiar with that. Jul 4 '14 at 12:11
• @user230137 If you can work with latin1 encoding (which in my case works, I just need to change the encoding in the text editor and use \usepackage[latin1]{inputenc}), then you will see that many of those problems go away (e.g., accents are supported by default, without literate). Jul 4 '14 at 15:45

At first I thought that with a comment was enough, but I think this deserves an answer. Since is something I learned rather late (a few weeks ago).

listings is much more friendly if you use latin1 as input encoding. For instance, accents work without literate, that is, out of the box (and also non-breaking spaces).

What could go wrong? I don't know. What are the downsides/upsides of latin1? I don't know. Feel free to edit in case you know.

Remember to save the document with latin1 encoding.

\documentclass[a4paper,12pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{colortbl}
\usepackage{lmodern}
\usepackage{listings}
\usepackage{color}

\lstset%
{frame = tb,
language = Java,
aboveskip = 3mm,
belowskip = 3mm,
showstringspaces = false,
columns = flexible,
basicstyle = {\small\ttfamily},
numbers = none,
breaklines = true,
breakatwhitespace = true,
tabsize = 3,
extendedchars = true,
postbreak = \space, breakindent = 5pt, breaklines,}

\begin{document}

\begin{lstlisting}
//...
API api = new API(url);
CultureIntermediaire cultureIntermediaire = resultat.getCultureIntermediaire();

//On affiche le nom de la méthode de travail du sol la plus érosive
\end{lstlisting}

\end{document}


As commented by @Pouya, in particular, french (e.g., œ, æ) is not fully supported (here is the Wikipedia article). However, it seems that latin9 (ISO-8859-15) does fully support it, so you may need to use it. Is it better? Again, I don't know. It would be great to have a complete reference about pros and cons of different encodings.

• I said latin1 because since I discovered it, I use it when I have problems (is the one used in the liked question). Is the best choice? Again: I don't know. May be other encodings work as well, and even better. Feel free to add extra info ;) Jul 4 '14 at 16:11
• I think (OP can correct me if I'm wrong) the comments are going to be in French and French is not fully supported by latin1. For example, this WikipediA article suggests that œ is not actually part of latin1 encoding. Now, granted that I don't know French, there is a chance that those characters are rarely used and latin1 may work properly. Please note that I'm not questioning the validity of your answer, it's just a side note that one might take into consideration. Cheers :)`. Jul 4 '14 at 17:21