tags:

views:

103

answers:

2

I have a Latex document displaying C++ code. The document is saved in UTF-8.

Here is the Latex code:

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern,textcomp}
\usepackage[frenchb]{babel}
\usepackage{listings}
\lstset{
language=C++,
extendedchars=true,
inputencoding=utf8
}
\begin{document}
Voici du code :
\begin{lstlisting}
#include <iostream>

int main() {
   // Affichage des libellés des colonnes
   std::cout << "a b c\n";
}
\end{lstlisting}
\end{document}

Note the accentuated character in the C++ code sample.

When trying to generate the PDF document, I get the following error:

! Missing \endcsname inserted.
<to be read again>
                   \global
l.18     // Affichage des libellés
                                    des colonnes
?

If I remove the accentuated character, the PDF generation works fine.

What's wrong?

A: 

Maybe... because of the unescaped \n (which uses a backslash character reserved for delimiting LaTeX commands) in your code.

Delan Azabani
+2  A: 

If I remember correctly, you can’t do that. Even using the inputencoding option only works for characters which don’t use more than a single byte.

As an alternative, use the xelatex processor instead of pdflatex, since XeTeX uses UTF-8 as the internal encoding or use minted instead of listings for source code.

Konrad Rudolph
I tried to use minted, was able to install it (I'm working on OS X, with `Distribution TeX`), but I get error saying it requires the pygmentize installed which I wasn't able to do.Anyway, to solve my problem right now, I put the source code in an external file (encoded in Latin1) and used \lstinputlisting[inputencoding=latin1]{my_code.cpp}Thanks for your help !
Jérôme
@Jérôme: Yes, minted indeed requires pygmentize.
Konrad Rudolph
@Konrad : I actually didn't understand at first that `pygmentize` was not a latex package, but a python library.I installed it and could use minted (with `pdfLaTex`), but I'm facing the same problem regarding accentuated characters. I then tried to use `XeLaTex`, but then, I got the `requires pygmentize installed` error (which I don't understand why as it is installed).Too bad : the output produced by minted was more appealing to me than the one from listings !
Jérôme
@Jérôme: true, even `minted` requires XeTeX to typeset UTF-8 for pretty much the same reason mentioned in my answer. As for the error, that may be due to a known bug. To fix it, try downloading the [current build](http://minted.googlecode.com/hg/minted.sty) from the project page and putting it in the `minted` directory in the LaTeX path.
Konrad Rudolph