I've imported a MATLAB code in my document by using the \lstinputlisting environment but I have the following errors about the accented letters:

package inputenc error: invalid utf-8 byte sequence;
package inputenc error: invalid utf-8 byte "A0;
package inputenc error: invalid utf-8 byte "A8;
package inputenc error: invalid utf-8 byte "A9;

etc... How can I fix these errors?

This is the main.tex:




\definecolor{mylightblue}{rgb}{.8, .8, 1}
\definecolor{mylightgray}{rgb}{0.96,0.96,0.96} %original: 0.95,0.95,0.95

%%%% colori inseriti da G.Donnarumma %%%%%%%%%%%%%%%%%%%
\definecolor{colTitle}{rgb}{0.83529 0.31373 0}

%%%% pacchetto per centrare didascalie inserito da G.Donnarumma%

%%%% pacchetto per stile e grafica Matlab inserito da G.Donnarumma%

% MATLAB as default 
    style=Matlab-editor,   %richiede il pacchetto matlab-prettifier (inserito da G.Donnarumma)
    basicstyle=\ttfamily\small, %font size/family/etc.for source \small
    identifierstyle=\color{colIdentifier}, %
    columns=[l]fixed,% flexible,%
    keepspaces=false,%        sposta la linea di separazione delle sezioni 
    frame=single,% single,lines, none
    rulecolor= \color{boxgray},%specify the color of  the frame-box 
    escapechar         = ",      %inserito da G.Donnarumma
    mlshowsectionrules = true,   %inserito da G.Donnarumma
    numbers=left,% left, none    %visualizza num. identificat. linea codice
    breaklines=true,     %automatic line-breaking
    lineskip=-2pt,       %
    backgroundcolor=\color{mylightgray}, %
    breakautoindent=true,       %
    numberbychapter=true,       %
    captionpos=false,    %position of caption (t/b)

    emph=[1]{interp1,pi,norm,sin,cos,disp,ode45,cd,set,get,gca,gradient,length,tan,gamma,sqrt,diag,acos,atan,asin,imag,real,subplot,sound,zeros,interp2,meshgrid,input,sprintf,xlabel,ylabel,plot,print, legend,grid,figure,hold},emphstyle=[1]\color{black}, %some words to emphasise




\vskip 0.3cm
\lstinputlisting[caption=Es.1-2 - cinematica manovra looping perfetto,label=lst:es-1-2,firstline=2,lastline=180]{code.m} 


This is the file code.m:

%% *DSV_Quaderno 3: Quaternione dell'orientamento di un velivolo*
%% Esercizi 3.1 e 3.2: cinematica e traiettoria dell'evoluzione di _looping perfetto_

clc; clear all; close all;
% Condizioni iniziali

% angoli di Eulero in rad
psi0   = convang(0,'deg','rad'); 
theta0 = convang(0,'deg','rad'); 
phi0   = convang(0,'deg','rad'); 

% in termini di quaternione
Q0 = angle2quat(psi0, theta0, phi0);

% sulla posizione
x0 = 0; y0 = 0; z0 = 0;
Pos0 = [x0; y0; z0];

% Tempi di manovra
% siccome si assegna una q di 1 rad/s per fare 
% un giro completo occorre un tempo pari a 2*pi
ti = 0; tf = 2*pi;

%Componenti della velocità inziale del baricentro in m/s
u0 = convvel(350,'km/h','m/s'); 
v0 = convvel(0,'km/h','m/s'); 
w0 = convvel(0,'km/h','m/s'); 
% Storie temporali delle velocità (funzioni anonime)

%Velocità angolari massime in rad/s
pmax = 0;
qmax = 1.00;
rmax = 0;

test: à, è, é, ù, ò

These files are loaded in my Overleaf project and the following ones are some packages that I use:

% packages and related macros

\usepackage[final]{optional}% draft, etc

% Language and encoding

% from guit forum: http://www.guit.sssup.it/phpBB2/viewtopic.php?t=3287
  \@clubpenalty3000 }

% Mathematics and related stuff


% Fonts and related stuff


\usepackage{pifont} % for dingbats
\usepackage{marvosym} % for \Keyboard etc

%\usepackage[osf]{erewhon}% extension of Utopia, osf=old-style-figures
\usepackage[varqu,varl]{inconsolata}% sans typewriter
\usepackage[scaled=.95]{cabin}% sans serif

\usepackage{textcomp}% AFTER newtxtext to avoid clashes
% font for Aspect Ratio by Claudio Beccari and Agostino De Marco
% the default variant is compatible with lmodern font

% Floating objects and related stuff
% I got this somewhere on the net:


  • Make sure you’ve loaded an encoding that contains the accented letters you want. If you’re using LuaLaTeX or XeLaTeX, you can just \usepackage{fontspec} to use Unicode, and you’re fine. In PDFTeX, you probably want \usepackage[T1]{fontenc}.
    – Davislor
    Commented Mar 28, 2021 at 19:30
  • If you’re using PDFTeX, also make sure you’ve saved your source file as UTF-8 in NFC form (that is, as precomposed characters). PDFTeX can’t handle combining accents. If it’s not in UTF-8, either save it as UTF-8 or load selinput to figure out the input encoding.
    – Davislor
    Commented Mar 28, 2021 at 19:32
  • with pdflatex the best is to use the literate key to define replacements, see e.g. tex.stackexchange.com/q/214828/2388 Commented Mar 28, 2021 at 19:47
  • Thanks to everyone for the asks. I work on an overleaf project online. I've modified my question by adding some packages that are listed in my doc_style file
    – g_don
    Commented Mar 28, 2021 at 20:11
  • (old question recently auto-bumped) Looks like duplicate of characters - How to set UTF8 in a lstlisting? (Error received) - TeX - LaTeX Stack Exchange ? Not too sure (input).
    – user202729
    Commented Dec 24, 2021 at 2:05

1 Answer 1


I solved the problem with the package listingsutf8 and then by setting the encoding using \lstset{inputencoding=utf8/latin1}.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .