0

I want to append some matlab code to a tex document and I use the matlab-prettifier package for that. I updated my miktex installation yesterday and I am now forced to use the \UseRawInputEncoding code line. Everything works fine for the German and Latin symbols I have in my matlab code, except for the degree sign, which comes out as an r with an inverted hat above, regardless of whether I use the literate substitution approach or not. Any suggestions on how to solve this elegantly would be welcome.

Here is my MWE:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[framed,numbered]{matlab-prettifier}
\usepackage{textcomp}


\UseRawInputEncoding

\begin{document}

\lstset{
  language=Matlab,
  style=Matlab-editor,
  basicstyle=\mlttfamily\footnotesize,
  literate=%
    {°}{\textdegree{}}1%
}

\lstinputlisting[]{matlab_code.m}

\end{document}

and my matlab_code.m file reads as:

% ----------------------------    
% Rö and Mé (T = 23°C)
var_1 = 10;

Obviously, I can write degree instead of ° in the matlab file and substitute that by \textdegree, but I would like to bring changes to the matlab code to a minimum and actually use the degree symbol, whose use comes much more naturally than that of the corresponding text. Is it possible to directly read in tex and via matlab-prettifier a ° symbol written on a matlab file, rather than "going around" by substituting the corresponding degree text by the actual symbol?

1 Answer 1

2

There's no coverage of UTF-8 by listings, unfortunately. But \UseRawInputEncoding is not the right tool.

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[framed,numbered]{matlab-prettifier}

\begin{document}

\lstset{
  language=Matlab,
  style=Matlab-editor,
  basicstyle=\mlttfamily\footnotesize,
  literate=
    {°}{\textdegree{}}1
    {ö}{{\"o}}1
    {é}{{\'e}}1
}

\lstinputlisting[]{matlab_code.m}

\end{document}

enter image description here

For completeness, here's what I get with minted.

\documentclass{article}

\usepackage[T1]{fontenc}
%\usepackage[framed,numbered]{matlab-prettifier}
\usepackage{minted}

\begin{document}

\inputminted[frame=single,linenos]{matlab}{matlab_code.m}

\end{document}

enter image description here

9
  • thanks a lot @egreg! However, for some unknown reason, I get a compilation error with the matlab-prettifier package, if I do not use \UseRawInputEncoding, namely a Package inputenc Error: Invalid UTF-8 byte "F6 type of error. This all started after I reinstalled miktex, before that I didn't have that kind of error. As for the minted package it looks good, but I am not able to compile as I first need to be able to invoke -shell-escape. Normally, I compile with cygwin, not with TexStudio, so I would be grateful if you could tell me how to compile the minted-related code on cygwin
    – Euclides
    Jul 9, 2020 at 23:07
  • Alright. Meanwhile, I could almost! compile the minted-related code by using the command pdflatex --shell-escape document_name. However, it seems I need to have pygmentize installed. Does that mean I have to install Python first? Or is there a shorter way to get to those pygments? Not familiar with Python..............
    – Euclides
    Jul 9, 2020 at 23:14
  • coming back to the matlab-prettifier approach: the problem clearly is that my latex architecture cannot make sense of ö, é and ° on the left handside of each literate entry! I would be grateful if I could get any help on how to fix this..............
    – Euclides
    Jul 10, 2020 at 6:57
  • I will in the meantime, try to install Python, set it to the appropriate path and get that pygmentize.exe going..............
    – Euclides
    Jul 10, 2020 at 6:58
  • still on the matlab-prettifier approach: the only way it currently works for me is if I have text, not "special characters" written in the matlab code, i.e. if I for instance have "oe" instead of "ö" in the matlab code, and then use literate with {oe}{{\"o}}1 to finally have latex putting the ö in the .pdf document; which is less than ideal..............
    – Euclides
    Jul 10, 2020 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .