TransWikia.com

Degree symbol and lstinputlisting

TeX - LaTeX Asked by JuCa on August 1, 2021

How can I print a degree symbol (°) in a listing with lstinputlisting?

I have noticed the questions based on the lstlisting environment but it does not work with lstinputlisting.

This illustrate the solution working with the lstlisting environment not with lstinputlisting:
enter image description here

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}

MATLAB file to print with lstinputlisting

function [others] = Create_structure_others()

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

end

One Answer

Check that the .m file is UTF-8 encoded and convert it if not.

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}

enter image description here

I'm not sure if one can coerce MATLAB into outputting UTF-8, but it should be possible.

I tried to convert the .m file into Latin-1 and, indeed, the output is like yours (with error message).

Correct answer by egreg on August 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP