matlab-prettifier
is built on top of listings
. Hence you can use linerange={<first1>-<last1>,<first2>-<last2>, and so on}
(which is a listings
option) to print selected lines. For example:
\lstinputlisting[caption = {Sample code from Matlab with line range},linerange={3-6}]{sample.m}
Now lines 3-6 of sample.m
will be printed.
Code (from one of Jubobs old answer):
\documentclass{memoir}
\usepackage[T1]{fontenc}
\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{filecontents}
\begin{filecontents*}{person.m}
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
BMI = person.mass/person.massĀ²;
end
end
end
\end{filecontents*}
\begin{filecontents*}{sample.m}
%% Code sections are highlighted.
% System command are supported...
!gzip sample.m
% ... as is line continuation.
A = [1, 2, 3, ... % (mimicking the ouput is good)
4, 5, 6]
fid = fopen('testFile.text', 'w')
for i=1:10
fprintf(fid,'%6.2f \n', i);
end
x=1; %% this is just a comment, though
% Context-sensitive keywords get highlighted correctly...
p = properties(mydate); %(here, properties is a function)
x = linspace(0,1,101);
y = x(end:-1:1)
% ... even in nonsensical code.
]end()()(((end end)end ))))end (function end
%{
block comments are supported
%} even
runaway block comments
are
\end{filecontents*}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"
\lstset{
style = Matlab-editor,
basicstyle = \mlttfamily,
escapechar = ",
mlshowsectionrules = true,
}
\begin{document}
\lstlistoflistings
\lstinputlisting[caption = {Some class definition}]{person.m}
Before you use any "for"~loop, refresh your memory on Matlab blocks.%
\footnote{Any occurence of "for" must have a matching "end".}
\lstinputlisting[caption = {Sample code from Matlab with line range},linerange={3-6}]{sample.m}
%% \lstinputlisting[caption = {Sample code from Matlab with line range},linerange={3-6,10-15}]{sample.m}
\lstinputlisting[caption = {Sample code from Matlab}]{sample.m}
\pagebreak
\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}
\end{document}
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:
Best Answer
Same as your other question, you need
style=Matlab-editor
to telllistings
what style should be applied to the listing.Font size changes can be incorporated into the setting of the
basicstyle
key, such asbasicstyle=\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.