1

I'm trying to insert a matlab code file in my tex document using listings but the output is messy because I'm using % inside a string with fprintf which is also a comment character.

In the line fprintf("ess = %.2f%%\n", ess); as soon as the % is read, everything (till the of the line) after it, is considered as a comment.

Here is a MWE:

\documentclass[english]{article}
\usepackage{color}
\usepackage{listings}

\definecolor{mycomment}{rgb}{0.63,0.63,0.65} \definecolor{mygray}{rgb}{0.02,0.68,0.72} \definecolor{mymauve}{rgb}{0.77,0.64,0.2} \definecolor{mykw}{rgb}{0, 0.6, 0.87} \definecolor{myblack}{rgb}{0.22,0.23,0.26} \definecolor{myred}{rgb}{0.84,0.15,0.33} \definecolor{mynumber}{rgb}{0.81,0.2,0.75} \definecolor{row_hl}{rgb}{0.72,0.93,0.99}

\lstset{ language=Matlab, backgroundcolor={\color{white}}, basicstyle={\footnotesize\ttfamily}, breakatwhitespace=false, breaklines=true, captionpos=b, commentstyle={\color{mycomment}\small}, deletekeywords={...}, escapeinside={"}{")}, extendedchars=true, firstnumber=1, frame=tb, keepspaces=true, keywordstyle={\color{mykw}}, morekeywords={tf,step,stepinfo,damp,factorial,poissrnd,normpdf,normcdf}, numbers=left, numbersep=5pt, numberstyle={\tiny\color{mygray}}, rulecolor={\color{black}}, showspaces=false, showstringspaces=false, showtabs=false, stepnumber=1, stringstyle={\color{mymauve}}, tabsize=2, title={\lstname} }

\begin{document} % \lstinputlisting[caption={Matlab code.},label={lst:matlab-code}]{mod_sys_code.m}

\begin{lstlisting}
    fprintf("ess = %.2f%%\n", ess);
    fprintf("ess = should look like this\n");
\end{lstlisting}

\end{document}

Is there a workaround to solve this issue?

Midouj
  • 21
  • 1
    Please add a full minimal working example (MWE). You added your output, please be specific about, what's wrong with it. – MaestroGlanz May 23 '22 at 07:11
  • I hope this clarifies the issue @MaestroGlanz – Midouj May 23 '22 at 07:41
  • Lstlistings doesn't know the language Matlab. If you change the language to bash, it is displayed correctly. – MaestroGlanz May 23 '22 at 07:47
  • Unfortunately, I couldn't find out, how to add a language or correct it. – MaestroGlanz May 23 '22 at 07:49
  • Thank you that fixed the bit within a string but it creates the issue where comments aren't detected. It's a good start, so I'll try to find something to include the comments. – Midouj May 23 '22 at 08:50
  • You should figure out, how to teach lstlistings a new language. Or, if there is a language, with almost identical syntax, pick that one. I'm not proficient enough in Matlab to figure that out. – MaestroGlanz May 23 '22 at 08:53
  • 1
    @MaestroGlanz, I've got it working with the language set to Matlab. I simply had to replace double quotes with single quotes. Thank you – Midouj May 23 '22 at 09:18
  • @Midouj have you checked https://tex.stackexchange.com/questions/75116/what-can-i-use-to-typeset-matlab-code-in-my-document ? – Rmano May 23 '22 at 09:22
  • Then you should add an answer to your own question for any future roamer looking for the solution. – MaestroGlanz May 23 '22 at 09:25
  • @Rmano I tried matlab-prettier but it is a wrapper for listings so the initial problem remains the same. – Midouj May 23 '22 at 09:27
  • @MaestroGlanz will certainly do – Midouj May 23 '22 at 09:28

2 Answers2

1

So to fix this issue I had to replace the double quotes (") used in the fprintf command with single quotes (').

enter image description here

Midouj
  • 21
0

The package matlab-prettifier works if you use ' as a string delimiter.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[numbered,framed]{matlab-prettifier}
\lstset{
  style              = Matlab-editor,
  basicstyle         = \mlttfamily,
}

\begin{document} % \lstinputlisting[caption={Matlab code.},label={lst:matlab-code}]{mod_sys_code.m}

\begin{lstlisting}
    fprintf('ess = %.2f%%\n', ess);
    fprintf('ess = should look like this\n');
\end{lstlisting}

\end{document}

enter image description here

I do not know if there is an option for making it do the same with the " delimiter... you can try to ask on GitHub.

Rmano
  • 40,848
  • 3
  • 64
  • 125