[Tex/LaTex] An error appears saying that utf8 and listing are not compatible

listingslistingsutf8

If I compile the next code the following error appear.

! Package inputenc Error: Unicode char \u8:�\expandafter\lst@FillFixed@\fi not
set up for use with LaTeX.

See the inputenc package documentation for explanation.
Type H for immediate help.

l.6 …%% sI A tiene inversa esta es la soluci�n(
Vector Nulo)
?

Then when I comment "\usepackage[utf8]{inputenc}" the code compile, but I need the utf8 package. How could I fix it?

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pgf,tikz}

\usetikzlibrary{arrows}
\usepackage{float}
\usepackage{verbatim}




\usepackage{listings}



\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}

\usepackage{hyperref}
\usepackage{txfonts,pxfonts}


\begin{document}    



\section*{Matlab Code}
\begin{lstlisting}
clc, clear
syms k1 k2 y m
A= [0 1 0 1;-1*(k1+k2)/m 0 k2/m 0; 0 0 0 1; k2/m 0 0 -(k1+k2)/m];
cero=zeros(4);  
determinante=det(A); %% Determinante de A
Sol=A\cero;%% sI A tiene inversa  esta es la solución(Vector Nulo)
% El unico punto de equilibrio es el punto 0
detervalprop= det(A-y*eye(4))
[V,D]=eig(A)
f=subs(D(1),m,1)
%figure
%ezsurf(f)
%figure
%subplot(2,1,2)
h=ezplot(f)
set(h, 'Color', 'r')
\end{lstlisting}



\end{document}

Best Answer

If you can accept to escape to LaTeX for accented characters.

From page 15 in listings manual:

Similarly, if you are using UTF-8 extended characters in a listing, they must be placed within an escape to LaTeX.

Note: I've commented mcode because MikTeX didn't find it.

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\usepackage{float}
\usepackage{verbatim}

\usepackage{listings}

%\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}

\usepackage{hyperref}
\usepackage{txfonts,pxfonts}


\begin{document}    

\section*{Matlab Code}

\begin{lstlisting}[escapechar=&]
clc, clear
syms k1 k2 y m
A= [0 1 0 1;-1*(k1+k2)/m 0 k2/m 0; 0 0 0 1; k2/m 0 0 -(k1+k2)/m];
cero=zeros(4);  
determinante=det(A); %% Determinante de A
Sol=A\cero;%% si A tiene inversa  esta es la soluci&ó&n (Vector Nulo)
% El &ú&nico punto de equilibrio es el punto 0
detervalprop= det(A-y*eye(4))
[V,D]=eig(A)
f=subs(D(1),m,1)
%figure
%ezsurf(f)
%figure
%subplot(2,1,2)
h=ezplot(f)
set(h, 'Color', 'r')
\end{lstlisting}

\end{document}

enter image description here