[Tex/LaTex] matlab-prettifier code not showing correctly

MATLAB

In the MWE below, the text (fonts and colors) from the Matlab Code read from \lstinputlisting do not show up like that from the Matlab generated from the lstlisting code:

\documentclass{book}

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

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

\usepackage{fontspec}

\usepackage{filecontents}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

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

\newfontfamily{\lstconsolas}{Consolas}

\newcommand\ph\mlplaceholder

\begin{document}

\lstlistoflistings

\lstinputlisting{sample.m}

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

\end{document}

how can I get both the sample.m file code (the code read from the Matlab file) to show up with the colors and boxes and line numbers?

Also, how do you change the size of the Matlab code fonts?

Best Answer

Same as your other question, you need style=Matlab-editor to tell listings what style should be applied to the listing.

Font size changes can be incorporated into the setting of the basicstyle key, such as basicstyle=\lstconsolas\small.

If you want to set these keys for more than one listing at a time, you may want to look into \lstset which can be used to set these for the whole document or any group therein unless otherwise overridden.

\documentclass{book}

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

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

\usepackage{fontspec}

\usepackage{filecontents}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

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

\newfontfamily{\lstconsolas}{Consolas}

\newcommand\ph\mlplaceholder

\begin{document}

\lstlistoflistings

\lstinputlisting[style=Matlab-editor,caption=Sample code from Matlab]{sample.m}

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

\end{document}

enter image description here

Related Question