度記号と\ lstinputlisting

Oct 28 2020

のリストに度記号(°)を印刷するにはどうすればよい\lstinputlistingですか?

lstlisting環境に基づいた質問に気づきましたが、では機能しません\lstinputlisting

これは、以下ではlstlistingなく環境で機能するソリューションを示しています\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}

印刷するMATLABファイル \lstinputlisting

function [others] = Create_structure_others()

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

end

回答

2 egreg Oct 28 2020 at 17:18

.mファイルがUTF-8でエンコードされていることを確認し、エンコードされていない場合は変換します。

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

MATLABにUTF-8の出力を強制できるかどうかはわかりませんが、可能であるはずです。

.mファイルをLatin-1に変換しようとしましたが、実際、出力はあなたのようなものです(エラーメッセージが表示されます)。