[Tex/LaTex] Choosing lines from a Matlab file using matlab-prettifier

listingsMATLAB

I have been using the matlab-prettifier package a couple of times. So far I have only imported the entire .m-file to be shown in my documents using:

\usepackage[numbered,framed]{matlab-prettifier}

\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"

\lstset{
  style              = Matlab-editor,
  basicstyle         = \mlttfamily,
  escapechar         = ",
  mlshowsectionrules = true,
}

in the preamble and:

\lstinputlisting{../mFile.m}

in the documents.

In my upcoming beamer presentation I want to show only some part of my code, e.g. lines 10-15. Is this somehow (easily) possible?

I usually use the documentclass memoir for my writings. Is the solution the same/easier here? (If it exists.)

Best Answer

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}

enter image description here

Related Question