Símbolo de grau e \ lstinputlisting

Oct 28 2020

Como posso imprimir um símbolo de grau (°) em uma listagem com \lstinputlisting?

Percebi as questões com base no lstlistingambiente, mas não funciona \lstinputlisting.

Isso ilustra a solução que trabalha com o lstlistingambiente, não com \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}

Arquivo MATLAB para impressão \lstinputlisting

function [others] = Create_structure_others()

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

end

Respostas

2 egreg Oct 28 2020 at 17:18

Verifique se o .marquivo está codificado em UTF-8 e converta-o se não estiver.

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

Não tenho certeza se alguém pode coagir o MATLAB a gerar UTF-8, mas deveria ser possível.

Tentei converter o .marquivo para Latin-1 e, de fato, a saída é como a sua (com mensagem de erro).