0

I'm using TeXstudio and pdfLaTeX. I'm trying to use the listings package to add some Matlab source code to my document. However, I'm having trouble with some portuguese letters used in the code (graphics captions, actually).

The common answer to this known encoding problem is shown here and here. However, even after adding the \lstset{literate=} with the correct list, the problem persists. I verified that the problematic letters and indeed in the list to be substituted, but no change.

The \lstset and the \lstinputlisting[language=Matlab]{estrutura/dados/algoritmos/main.m} I'm calling to add the code are in different files, but that was not a problem for any other package. I also verified that I can include code without any special characters.

What could be causing this problem and what would be a fix or an workaround?

Thanks for any help

EDIT for sample code: This part is the one being used to substitute letters in the listings.

\lstset{language=Matlab}
\lstset{
    inputencoding=utf8,
    extendedchars=true,
    literate=
    {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 
    {ó}{{\'o}}1 {ú}{{\'u}}1 {Á}{{\'A}}1 
    {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1
    {Ú}{{\'U}}1 {à}{{\`a}}1 {è}{{\`e}}1 
    {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
    {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1
    {Ò}{{\`O}}1 {Ù}{{\`U}}1 {ä}{{\"a}}1
    {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1
    {ü}{{\"u}}1 {Ä}{{\"A}}1 {Ë}{{\"E}}1 
    {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
    {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1
    {ô}{{\^o}}1 {û}{{\^u}}1 {Â}{{\^A}}1
    {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1
    {Û}{{\^U}}1 {œ}{{\oe}}1 {Œ}{{\OE}}1
    {æ}{{\ae}}1 {Æ}{{\AE}}1 {ű}{{\H{u}}}1 
    {Ű}{{\H{U}}}1 {ő}{{\H{o}}}1 {Ő}{{\H{O}}}1
    {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1
    {å}{{\r a}}1 {Å}{{\r A}}1 {£}{{\pounds}}1
    {«}{{\guillemotleft}}1 {»}{{\guillemotright}}1
    {ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1
}

This is the line calling listing of the source code

\lstinputlisting[language=Matlab]{estrutura/dados/algoritmos/plot_bf_compare_in_out.m}  

Specifically, the section of Matlab code throwing the error is

title("Comparação entre áudio antes e depois do beamformer");

EDIT 2: Since the complete text I am working is spread across multriple files and folders, I recreated the minimum document with everything that seems relevant to this problem and throws the same error. OneDrive download link here with the files.

EDIT 3: Compilable LaTeX code

\documentclass[%twoside,                   % Impressão em frente e verso
oneside,                   % Impressão apenas frente
]{utfpr-abntex2}


\usepackage{nicefrac}
\usepackage[utf8]{inputenc}                                 
\usepackage[T1]{fontenc}   
\usepackage{nicefrac}
\usepackage[utf8]{inputenc}                                 
\usepackage[T1]{fontenc}                                   
\usepackage{booktabs}                                      
\usepackage{color, colortbl}                                
\usepackage{float}                                          
\usepackage{graphicx}                                       
\usepackage{icomma}                                         
\usepackage{indentfirst}                                    
\usepackage{microtype}                                      
\usepackage{multirow, array}                                
\usepackage{subeqnarray}                                    
\usepackage{lastpage}                                       
\usepackage{pdfpages}
\usepackage{verbatim}                                       
\usepackage{amsfonts, amssymb, amsmath}                     
\usepackage[algoruled, portuguese]{algorithm2e}             
\usepackage{times}                                  
\usepackage[bottom]{footmisc}                       
\usepackage{ae, aecompl}                                    
\usepackage{latexsym}                                      
\usepackage{lscape}                                         
\usepackage{picinpar}                                      
\usepackage{scalefnt}                                      
\usepackage{upgreek}                                      
\usepackage{listings}

% Parte utilizada para que códigos fonte possam conter caracteres em português
\lstset{language=Matlab}
\lstset{
    inputencoding=utf8,
    extendedchars=true,
    literate=
    {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1
    {ó}{{\'o}}1 {ú}{{\'u}}1 {Á}{{\'A}}1 
    {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1
    {Ú}{{\'U}}1 {à}{{\`a}}1 {è}{{\`e}}1 
    {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
    {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1
    {Ò}{{\`O}}1 {Ù}{{\`U}}1 {ä}{{\"a}}1
    {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1
    {ü}{{\"u}}1 {Ä}{{\"A}}1 {Ë}{{\"E}}1 
    {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
    {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1
    {ô}{{\^o}}1 {û}{{\^u}}1 {Â}{{\^A}}1
    {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1
    {Û}{{\^U}}1 {œ}{{\oe}}1 {Œ}{{\OE}}1
    {æ}{{\ae}}1 {Æ}{{\AE}}1 {ű}{{\H{u}}}1 
    {Ű}{{\H{U}}}1 {ő}{{\H{o}}}1 {Ő}{{\H{O}}}1
    {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1
    {å}{{\r a}}1 {Å}{{\r A}}1 {£}{{\pounds}}1
    {ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1
}

\begin{document}

\lstinputlisting[language=Matlab]{plot_bf_compare_in_out.m} 

\end{document}

Sample of problematic source code file (.m):

figure;
title("Comparação entre áudio antes e depois do beamformer");
  • 1
    Welcome to TeX.se. It will be quite hard to help you without seeing some example code. Can you please edit your question to include a minimal compilable document that reproduces the problem you're having. (Also, the easiest way to deal with UTF-8 listings is to use LuaLaTeX or XeLaTeX instead of pdfLaTeX.) – Alan Munn Nov 24 '18 at 3:28
  • As Alan said, is there any reason not to switch to XeLaTeX or LuaLaTeX? – Joseph Nov 24 '18 at 17:02
  • XeLaTeX doesn't throw errors, but just ignores any letter it doesn't know. LuaLaTeX gives the same error. – L. Farinha Nov 24 '18 at 19:02
  • 1
    Thanks for the edit, but this isn't compilable code. Can you put it into a document that begins with \documentclass{...} and ends with \end{document}. And we can't see your actual \lstinputlisting file, so please include some of it directly in the document or add a small sample file to the question too. – Alan Munn Nov 24 '18 at 19:19
  • 1
    No, we don't need your exact files. You need to show us a small file in your question (not a link) that reproduces the problem. – Alan Munn Nov 24 '18 at 20:53
-1

I solved this problem by changing the input encoding from utf8 to latin1 only in the listings. In the end, I got this as the settings for the listings and it worked.

\lstset{
language=Matlab,
inputencoding=latin1,
extendedchars=true,
literate=
    {é}{{\'{e}}}1
    {è}{{\`{e}}}1
    {ê}{{\^{e}}}1
    {ë}{{\¨{e}}}1
    {É}{{\'{E}}}1
    {Ê}{{\^{E}}}1
    {û}{{\^{u}}}1
    {ù}{{\`{u}}}1
    {ú}{{\'{u}}}1
    {â}{{\^{a}}}1
    {à}{{\`{a}}}1
    {á}{{\'{a}}}1
    {ã}{{\~{a}}}1
    {Á}{{\'{A}}}1
    {Â}{{\^{A}}}1
    {Ã}{{\~{A}}}1
    {ç}{{\c{c}}}1
    {Ç}{{\c{C}}}1
    {õ}{{\~{o}}}1
    {ó}{{\'{o}}}1
    {ô}{{\^{o}}}1
    {Õ}{{\~{O}}}1
    {Ó}{{\'{O}}}1
    {Ô}{{\^{O}}}1
    {î}{{\^{i}}}1
    {Î}{{\^{I}}}1
    {í}{{\'{i}}}1
    {Í}{{\~{Í}}}1
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.