2

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:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}

\usepackage[hyper]{listings}

\renewcommand{\lstlistingname}{Listato}
\newcommand{\lstInCaption}[1]{\lstinline[basicstyle=\ttfamily\footnotesize]|#1|}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{mydarkgreen}{rgb}{0.03,0.47,0.03}
\definecolor{mydarkblue}{rgb}{0.07,0.08,0.4}
\definecolor{mylightblue}{rgb}{.8, .8, 1}
\definecolor{mylightgray}{rgb}{0.96,0.96,0.96} %original: 0.95,0.95,0.95
\definecolor{mydarkgray}{rgb}{0.35,0.35,0.35}
\definecolor{mylightyellow}{rgb}{0.97,0.91,0.81}
\definecolor{myblue}{rgb}{.4,.4,1}
\definecolor{colKeys}{RGB}{0,0,255}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{RGB}{28,172,0}
\definecolor{colString}{RGB}{170,55,241}

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

%%%% pacchetto per centrare didascalie inserito da G.Donnarumma%
\usepackage[justification=centering]{caption}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%% pacchetto per stile e grafica Matlab inserito da G.Donnarumma%
\usepackage[numbered,framed]{matlab-prettifier}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB as default 
\lstset{%
    float=hbp,%
    language=Matlab,%
    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}, %
    keywordstyle=\color{colKeys},%
    stringstyle=\color{colString},%
    commentstyle=\color{colComments},%
    columns=[l]fixed,% flexible,%
    keepspaces=false,%        sposta la linea di separazione delle sezioni 
    %tabsize=2,%
    frame=single,% single,lines, none
    rulecolor= \color{boxgray},%specify the color of  the frame-box 
    extendedchars=true,%
    showspaces=false,%
    showstringspaces=false,%
    escapechar         = ",      %inserito da G.Donnarumma
    mlshowsectionrules = true,   %inserito da G.Donnarumma
    numbers=left,% left, none    %visualizza num. identificat. linea codice
    numberstyle=\tiny,%
    breaklines=true,     %automatic line-breaking
    lineskip=-2pt,       %
    backgroundcolor=\color{mylightgray}, %
    breakautoindent=true,       %
    numberbychapter=true,       %
    captionpos=false,    %position of caption (t/b)
    belowcaptionskip=0.2\baselineskip%\medskipamount%
}
\lstset{%
   morekeywords={cell,interp3,interpn,fnplt,fneval%
      ,odeset,ode113,ode15s,ode23s,ode23t,ode23tb,cell2mat,%
      convvel,convang,convforce,convlength,deg2rad,atmoscoesa,atmosisa,atmosnonstd,%
      datcomimport,importdata,fieldnames,assignin%
      ,fnval,csaps,fmincon%
      ,classdef,properties,methods%
   }
}
\lstset{%
   emph={myfun,myfunction2x1,f_prandtl_a_b,%
      DSVAircraft,myAC,costLongEquilibriumStaticStickFixed,myNonLinearConstraint,dummyNonLinearConstraint,%
      eqLongDynamicStickFixed,calc_CD0_k_Tmax,MassStickFree,cla,eps,},%
   emphstyle={%
      \color{red!80!black}%
         \ttfamily\bfseries%
   }
}

\lstset{%
    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
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

Test

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

\end{document}

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

%sull'orientamento
% 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
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}

% from guit forum: http://www.guit.sssup.it/phpBB2/viewtopic.php?t=3287
\makeatletter
\addto\extrasitalian{\babel@savevariable\@clubpenalty
  \@clubpenalty3000 }
\makeatother

% Mathematics and related stuff

\usepackage[intlimits]{amsmath}
\usepackage{amsfonts}
\usepackage{amsbsy}
\usepackage{fixmath}
\usepackage{mathtools}

% Fonts and related stuff

\usepackage{relsize}

\usepackage[T1]{fontenc}
\usepackage{pifont} % for dingbats
\usepackage{marvosym} % for \Keyboard etc

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

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

% Floating objects and related stuff
\usepackage{float}
% I got this somewhere on the net:
\renewcommand{\textfraction}{0.15}
\renewcommand{\topfraction}{0.85}
\renewcommand{\bottomfraction}{0.65}
\renewcommand{\floatpagefraction}{0.60}

\usepackage[above]{placeins}

5
  • 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

2

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 .