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:
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}
Best Answer
matlab-prettifier
is built on top oflistings
. Hence you can uselinerange={<first1>-<last1>,<first2>-<last2>, and so on}
(which is alistings
option) to print selected lines. For example:Now lines 3-6 of
sample.m
will be printed.Code (from one of Jubobs old answer):