# matlab-prettifier only shows up black text

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}[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}


It only shows up as black and white:

where as I wanted to get:

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

• Add style=Matlab-editor to the lstlisting environment options. Without this, the listings package has no idea what style to apply to the code. – Paul Gessler Jun 5 '15 at 0:28

## 1 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:

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:

• Spot on! Thanks Paul; I've got nothing left to explain :) See you at TUG! – jub0bs Jun 5 '15 at 11:17
• Hello, I got an error: ! Package textcomp Error: Symbol \textlangle not provided by (textcomp) font family ptm in TS1 encoding. (textcomp) Default family used instead. can you tell me how to work around this? – Joe Jun 5 '15 at 14:53
• I don't get that error; maybe your installation is not fully up to date. You can check this by adding \listfiles at the top of the code from my answer and typesetting. Then look near the end of the log file for the file list. Mine is here for you to compare: gist.github.com/pdgessler/f7167781b2763cae4cab – Paul Gessler Jun 5 '15 at 15:38
• @Joe please see the edit; there's no need for yelling. – Paul Gessler Jun 5 '15 at 16:26
• @sodiumnitrate the options are just entered in a slightly different position: \lstinputlisting[style=Matlab-editor,<...>]{code.m}`. – Paul Gessler Jun 8 '15 at 14:29