Símbolo de grado y \ lstinputlisting

Oct 28 2020

¿Cómo puedo imprimir un símbolo de grado (°) en un listado con \lstinputlisting?

He notado las preguntas basadas en el lstlistingmedio ambiente pero no funciona \lstinputlisting.

Esto ilustra la solución que trabaja con el lstlistingmedio ambiente, no con \lstinputlisting:

MWE

\documentclass{article}

\usepackage{listings}
\usepackage{matlab-prettifier} % To highligth MATLAB code

\lstset{
    style=Matlab-editor,
    literate={{°}{\textdegree}1}
    }

\begin{document}

\begin{lstlisting}
{Street name, n° 40}
\end{lstlisting}

\lstinputlisting{test_matlab.m}
    
\end{document}

Archivo MATLAB para imprimir con \lstinputlisting

function [others] = Create_structure_others()

    others.a = struct('value', [], 'unit', '°');

end

Respuestas

2 egreg Oct 28 2020 at 17:18

Compruebe que el .marchivo esté codificado en UTF-8 y conviértalo si no lo está.

\begin{filecontents*}{\jobname.m}
function [others] = Create_structure_others()

    others.a = struct('value', [], 'unit', '°');

end
\end{filecontents*}

\documentclass{article}

\usepackage{listings}
\usepackage{matlab-prettifier} % To highligth MATLAB code

\lstset{
    style=Matlab-editor,
    literate={{°}{\textdegree}1}
    }

\begin{document}

\begin{lstlisting}
{Street name, n° 40}
\end{lstlisting}

\lstinputlisting{\jobname.m}

\end{document}

No estoy seguro de si se puede obligar a MATLAB a generar UTF-8, pero debería ser posible.

Intenté convertir el .marchivo a Latin-1 y, de hecho, el resultado es como el suyo (con mensaje de error).