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?
If I remember correctly, you can’t do that. Even using the
inputencodingoption only works for characters which don’t use more than a single byte.As an alternative, use the
xelatexprocessor instead ofpdflatex, since XeTeX uses UTF-8 as the internal encoding or use minted instead of listings for source code.