I'm having some problems with listings and UTF-8 in my document. Maybe someone can help me? Some characters work, like é and ó, but á and others appear at the beginning of words…
\documentclass[12pt,a4paper]{scrbook}
\KOMAoptions{twoside=false,open=any,chapterprefix=on,parskip=full,fontsize=14pt}
\usepackage[portuguese]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listingsutf8}
\usepackage{inconsolata}
\lstset{
language=bash, %% Troque para PHP, C, Java, etc... bash é o padrão
basicstyle=\ttfamily\small,
numberstyle=\footnotesize,
numbers=left,
backgroundcolor=\color{gray!10},
frame=single,
tabsize=2,
rulecolor=\color{black!30},
title=\lstname,
escapeinside={\%*}{*)},
breaklines=true,
breakatwhitespace=true,
framextopmargin=2pt,
framexbottommargin=2pt,
extendedchars=false,
inputencoding=utf8
}
\begin{document}
\begin{lstlisting}
<?php
echo 'Olá mundo!';
print 'Olá mundo!';
\end{lstlisting}
\end{document}
\end{lstlisting}
Best Answer
One way to get around this limitation of
listings
is to use the optionextendedchars=true
and then to use theliterate
option for each accents you're going to be using (it's a bit tedious to do, but once you've done all the accents of your language, you never have to worry about them again). The syntax isFor each accent you must put the real character inside braces (e.g.
{á}
) then you put what you want this character to be inside double braces (e.g.{{\'a}}
) and finally you put the number one (1
); between two entries, you can put a space for clarity.Here's your example modified to use this: