# File-encoding issues with \lstinputlisting

Matlab save .m file with ANSI encoding. I have a sample .m file, and want import it into LaTeX with UTF-8 encoding. How to convert ANSI file encoding to UTF-8 and input it, without changing the original file?

\documentclass{article}
\usepackage{listings}

\begin{document}

\lstinputlisting[language=Matlab]{sample.m}  % <- how to convert encoding?

\end{document}

• How about opening up the .m file in your editor, changing the encoding to UTF8 and live happily every after?
– Werner
Dec 12 '14 at 6:09
• Why do you need it in utf8? Does your code not compile? If so, please provide an MWE. Have you tried \lstset{inputencoding=utf8}? Dec 12 '14 at 7:50
• Dear Werner, I say, I can not change .m encoding, because when I open it with Matlab for compiling, Matlab change it to ANSI again. Dec 12 '14 at 7:56
• Oarfish, because I have some comment with non-English language! and I want to show them in Latex, but it will be happen if .m file have saved with UTF-8 encoding. Dec 12 '14 at 7:59
• Put feature('DefaultCharacterSet', 'UTF8') on top of your .m file or change your startup.m file. But still you don't need to do anything with your .m file. You can use matlab-prettifier package from our user Jubobs, see here tex.stackexchange.com/q/75116/3235 Dec 12 '14 at 8:15

I define some command in my latex such: \newcommand{\prb}{\rl{تابع احتمال}}, and in my .m file, I use the command \prb instead of unicode characters: تابع احتمال. So, the .m file, just consist of ASCI characters. Latex then, execute the command and view تابع احتمال in the PDF and work fine!