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 03:28
  • As Alan said, is there any reason not to switch to XeLaTeX or LuaLaTeX? –  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
  • Since the files are to big and spread, I put all the configuration in a new file and added a link to download the .tex file and the .m file with the code. Link in EDIT 2 – L. Farinha Nov 24 '18 at 19:55
  • 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
  • Sorry, I'm not sure what you are asking that isn't already in the edit. I have the line \lstinputlisting[language=Matlab]{estrutura/dados/algoritmos/plot_bf_compare_in_out.m} that calls to source code file and the line title("Comparação entre áudio antes e depois do beamformer"); in the source code itself, with the character that causes the error. – L. Farinha Nov 24 '18 at 21:32
  • Added another EDIT. Hopefully that has the information you are looking for – L. Farinha Nov 24 '18 at 21:39
  • What do you mean "just ignores any letter is doesn't know?" It makes no sense. If there is no glyph in a font, it just doesn't use it, that's all, find another font. –  Nov 25 '18 at 23:20
  • XeLaTeX just doesn't put the letter. So if I write "Seção", all that it shows is "Seo". I'm not sure what you mean by "glyph". I'm still new to LaTeX and using a template for this kind of work that complies with all font and formatting standards. – L. Farinha Nov 27 '18 at 00:17
  • @L.Farinha, no, if you just write `\begin{document} Não nènínónnã. \end{document} and compile with XeLaTeX, no strings attached, it will render fine. That is, unless you're doing something we don't know, since you didn't show us a minimal working example. Using templates sometimes is not the best way to learn -- see https://tex.stackexchange.com/questions/390683/why-should-you-avoid-using-complex-templates –  Nov 28 '18 at 21:37
  • It was working fine for that case. The problem only happened when using the "listings" package. My EDIT 3 included the minimum working example. All those packages might not be needed, but I decided to include them, in case one could be conflicting with another. About the template, if I was not using it there would not be a reason to use LaTeX for this. Not having to worry with all the small formatting details needed for a thesis like this. – L. Farinha Nov 29 '18 at 22:33
  • I see you're loading \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} twice (is this really a "template"?). These packages wouldn't be needed if you use xelatex. Besides, there are many reasons to use LaTeX and not using that template without any modifications. –  Nov 30 '18 at 08:40
  • Not sure if "template" is the correct word for this. It's a group of files configurated and separated by section according to the rules for academic thesis where I live. Basically, everything was already created and you just delete the sample text to add your own. There are instructions and examples on how to add figures and tables and orientation on the correct way to add references to the bibliography. I'm not touching the default packages. I use LaTeX for other stuff too and start from a blank page, but in this case this was the fastest way to finish my thesis. – L. Farinha Dec 01 '18 at 13:02
  • "I'm not touching the default packages." -- Even if those packages are loaded twice, are deprecated or obsolete? –  Dec 01 '18 at 22:05

1 Answers1

-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
}