[Tex/LaTex] matlab-prettifier only shows up black text

listingsMATLAB

This is my preamble for my book documentclass:

\documentclass[11pt,fleqn]{book} % Default font size and left-justified equations

\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry} % Page margins
\usepackage[titletoc]{appendix}
\usepackage[svgnames]{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25} % Define the orange color used for highlighting throughout the book
\definecolor{mygray}{RGB}{243,243,244}

% Font Settings
\usepackage{avant} % Use the Avantgarde font for headings

\usepackage{mathptmx} 
\usepackage{microtype} 
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} 
\usepackage{makeidx} 
\usepackage{calc}


%\usepackage{bigfoot} 
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{filecontents}
\usepackage{empheq}
% Index
\usepackage{calc} % For simpler calculation - used for spacing the index letter headings correctly
\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}
\usepackage[framemethod=default]{mdframed}
\usepackage{mathtools}
\usepackage[most]{tcolorbox}
\tcbset{myformula/.style={
  arc=0pt,
  outer arc=0pt,
  %colback=ocre!10,
  colback=mygray,
  colframe=ocre,
  boxrule=0.8pt,
  left=2pt,
  right=2pt,
  highlight math style={
    arc=0pt,
    outer arc=0pt,
    colback=mygray,
    colframe=red.
    }
  }
}
%\usepackage{hyperref}

\newenvironment{spread}[1]{%
  \advance\jot#1% indeed
  }{%
\ignorespacesafterend
}

When I try to put some matlab code in:

\begin{lstlisting}
% example of while loop using placeholders
while "\ph{condition}"
  if "\ph{something-bad-happens}"
    break
  else
    % do something useful
  end
  % do more things
end
\end{lstlisting}

It only shows up as black and white:

black and white Matlabcode

where as I wanted to get:

enter image description here

can you tell me how I can fix my tex file so that the Matlab code can show up with color??

Best Answer

This is quite similar to an example in the matlab-prettifier package manual.

First, we need to tell listings which style to apply, with style=Matlab-editor.

Second, if you want to use \ph as shorthand for the placeholders, you must do \newcommand\ph\mlplaceholder to define the shorthand as discussed in the manual.

Third, even with the shorthand, you still have to choose and tell listings about a suitable escape character, so it knows when to escape from verbatim processing. Here, I've used escapechar=` as in the manual.

Here's a minimal example showing all the requirements for this code listing:

\documentclass{book}

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

\begin{document}
\begin{lstlisting}[
  style=Matlab-editor,
  basicstyle=\mlttfamily,
  escapechar=`,
  caption={For educational purposes},
]
% example of while loop using placeholders
while `\ph{condition}`
  if `\ph{something-bad-happens}`
    break
  else
    % do something useful
  end
  % do more things
end
\end{lstlisting}
\end{document}

And the resulting output:

enter image description here


The specific fonts you selected do not provide \textlangle and \textrangle symbols, which are used internally by matlab-prettifier to delimit the placeholders. We can work around this by renewing the commands to use the math-mode angle brackets since the text-mode angle brackets are not provided. I do this using the following code:

\makeatletter
\renewcommand\phOpDelim@mlpr{$\langle$}
\renewcommand\phClDelim@mlpr{$\rangle$}
\makeatother

Another option would be to define \textlangle and \textrangle on your own. In that case, the code block I added above is not required.

Here's the full code using the math-mode symbols:

\documentclass{book}

\usepackage[svgnames]{xcolor}
\definecolor{ocre}{RGB}{243,102,25}
\definecolor{mygray}{RGB}{243,243,244}

\usepackage{avant}
\usepackage{mathptmx}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[numbered,framed]{matlab-prettifier}
\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}

\newcommand\ph\mlplaceholder
\makeatletter
\renewcommand\phOpDelim@mlpr{$\langle$}
\renewcommand\phClDelim@mlpr{$\rangle$}
\makeatother

\begin{document}
\begin{lstlisting}[
  style=Matlab-editor,
  basicstyle=\mlttfamily,
  escapechar=`,
  caption={For educational purposes},
]
% example of while loop using placeholders
while `\ph{condition}`
  if `\ph{something-bad-happens}`
    break
  else
    % do something useful
  end
  % do more things
end
\end{lstlisting}
\end{document}

And the output:

enter image description here

Related Question