I want to include python scripts in a document, but in those scripts I use characters like á é í ó ú. I am using the literate option of \lstet. Here is an example code:
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish,es-tabla]{babel}
\usepackage{listings, pythonhighlight}
\lstset{
basicstyle=\ttfamily\small,
breakatwhitespace=false,
breaklines=true,
inputencoding=utf8,
extendedchars=false,
literate= {á}{{\'a}}1
{é}{{\'e}}1
{í}{{\'i}}1
{ó}{{\'o}}1
{ú}{{\'u}}1
}
\begin{document}
\lstinputlisting[language=Python]{example.py}
\end{document}
With a python script (example.py) that contains:
# coding = utf-8
# á é í ó ú
I get a pdf like this:
where all special characters are substituted by the last one in the literate option.
Why is this happening?
EDIT
Using the package listingsutf8 instead of listings with
basicstyle=\ttfamily\small,
breakatwhitespace=false,
breaklines=true,
inputencoding=utf8,
extendedchars=false,
literate= {á}{{\'a}}1
{é}{{\'e}}1
{í}{{\'i}}1
{ó}{{\'o}}1
{ú}{{\'u}}1
}
does NOT change anything.
Using listingsutf8 with:
\lstset{ ...
inputencoding=utf8/latin1,
...
}
allows for all the special characters I need but misplaces them (all of them appear at the beginning of the python comment and not in the place they should be in the sentence).
Using listingsutf8 with:
\lstset{ ...
inputencoding=latin1,
...
}
does not work either because my code is utf8, so it gives me errors compiling.
