1

I use the matlab prettifier package with the matlab editor option to compile a file containing matlab code in LaTeX. Question: is it possible to somehow insert a LaTeX-like command for a single character in the original matlab file/ code and interpret it accordingly via the aforementioned package while compiling in LaTeX?

Here MWE:

\lstinputlisting[style=Matlab-editor,basicstyle=\mlttfamily\footnotesize]{data.m}

Original .m code to be imported:

Quantity X [micrometer]

Data row 1

Data row 2


Data row N

I basically don't want to have micrometer in the compiled .pdf, but the actual greek character. I am aware of some "escape" options for the listings package, but I'm not quite sure how that would work in combination with the matlab prettifier package. Another possible option would be the mcode package, which enforces literate interpretations, but that package doesn't seem to produce an output that is as attractive as the one coming out of the matlab prettifier package. Any help would be greatly appreciated!

2
  • Is that the only occurrence of the string micrometer in the file? If so, you could use the literate option. Nov 1, 2019 at 20:37
  • Hi Torbjorn! Thanks for the quick reply! Yes it is. I will try that out and come back to you! Just one question: I would in that case write $\mu$ in the source .m code or how exactly would that work? Well, probably not. Just micrometer!
    – euclides
    Nov 1, 2019 at 20:37

1 Answer 1

1

As micrometer only occurs one place (or, all occurrences of micrometer are to be replaced), you can use the literate option of the listings package. matlab-prettifier is based on listings. That is:

\lstinputlisting[literate={micrometer}{{$\mu\mathrm{m}$}}2]{foo.m}

enter image description here

\documentclass{article}
\usepackage{matlab-prettifier}
\lstset{
  style=Matlab-editor,
  basicstyle=\mlttfamily\footnotesize,
}
\begin{document}
\lstinputlisting[literate={micrometer}{{$\mu\mathrm{m}$}}2]{foo.m}
\end{document}
6
  • Well, micrometer should actually be replaced by $\mu m$, but I have micrometer between [] in the .m code, so in the .pdf the greek-roman character actually overlaps with the square brackets. What I should be doing is actually: literate={[micrometer]}{{$\mathrm{\left[\mu m\right]}$}}1; however, matlab prettifier cannot interpret this latter option. Not sure if because of the brackets or the mathrm package therein.
    – euclides
    Nov 1, 2019 at 20:49
  • @euclides True, see updated answer. Nov 1, 2019 at 20:52
  • @euclides I don't really know either why the replacement doesn't work for the brackets. Nov 1, 2019 at 20:52
  • works perfectly. Just one last question: the last digit in the option refers to the number of TeX characters replacing micrometer is that right?
    – euclides
    Nov 1, 2019 at 20:57
  • @euclides Yes, that is correct. Nov 1, 2019 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.