How to highlight a portion of Matlab Code

The tikzmark library can be used here. Some parts are borrowed from this nice answer. UPDATE: No changes in the listing, as implicitly requested by Will Robertson.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{tikz} 
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}
\newcounter{tmkcount}

\tikzset{
  use tikzmark/.style={
    remember picture,
    overlay,
    execute at end picture={
      \stepcounter{tmkcount}
    },
  },
  tikzmark suffix={-\thetmkcount}
}

\begin{document}

\begin{lstlisting}[style=Matlab-editor,name=4Will]
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^2;
       end
   end
end
\end{lstlisting}
\begin{tikzpicture}[use tikzmark]
\draw[red,thick]
  ([shift={(-3pt,2ex)}]pic cs:line-4Will-8-first) 
    rectangle 
  ([shift={(3pt,-0.75ex)}]pic cs:line-4Will-8-end);
\end{tikzpicture}
\end{document}

enter image description here

Of course, that also works with external files. The good thing is that you do not need to put stuff in the code. (Of course, you need to know that you want to annotate line 8.) This gives the same output as above.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{tikz} 
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}
\newcounter{tmkcount}

\tikzset{
  use tikzmark/.style={
    remember picture,
    overlay,
    execute at end picture={
      \stepcounter{tmkcount}
    },
  },
  tikzmark suffix={-\thetmkcount}
}
\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^2;
       end
   end
end
\end{filecontents*}
\begin{document}
\lstinputlisting[style=Matlab-editor,name=4Will]{person.m}
\begin{tikzpicture}[use tikzmark]
\draw[red,thick]
  ([shift={(-3pt,2ex)}]pic cs:line-4Will-8-first) 
    rectangle 
  ([shift={(3pt,-0.75ex)}]pic cs:line-4Will-8-end);
\end{tikzpicture}
\end{document}

If you want to highlight several lines, you can make use of the -| syntax to draw the lines around extremal points. Alternatively, you could load the fit library. Here I present one simple example. And I would like to kindly ask you considering asking follow-up questions in case you have more requests.

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{tikz} 
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}
\newcounter{tmkcount}

\tikzset{
  use tikzmark/.style={
    remember picture,
    overlay,
    execute at end picture={
      \stepcounter{tmkcount}
    },
  },
  tikzmark suffix={-\thetmkcount}
}
\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^2;
       end
   end
end
\end{filecontents*}
\begin{document}
\lstinputlisting[style=Matlab-editor,name=4Will]{person.m}
\begin{tikzpicture}[use tikzmark]
\draw[red,thick]
  ([shift={(-3pt,2ex)}]pic cs:line-4Will-8-first) 
    rectangle 
  ([shift={(3pt,-0.75ex)}]pic cs:line-4Will-8-end);
\coordinate (aux1) at ([xshift=5pt]pic cs:line-4Will-8-end);
\coordinate (aux2) at ([yshift=-0.75ex]pic cs:line-4Will-9-end);
\draw[blue,thick]
  ([shift={(-3pt,2ex)}]pic cs:line-4Will-7-first) 
    rectangle 
  (aux1 |- aux2);
\end{tikzpicture}
\end{document}

enter image description here


Here is a method which uses fbox. You simply add this command around the line of code you want to highlight and it will draw a box around it.

However, I've actually used a modified version called mycfbox which allows you to change the colour (from How to draw a colored framebox without filling the background?).

You have to take some care with the ^ character, so I've added \string before it to ensure it outputs correctly.

Remember, you need to enclose \mycfbox{red}{....} in the escapechar which I've set to ". You can change this if needed.

Updated: you need to enclose the fbox command in smash to remove the vertical space added around it. Additionally, you can control the line thickness of the box using \fboxrule and you can control the size of the box using \fboxsep.

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{xcolor} % <--- Added

\renewcommand{\fboxsep}{2pt} % <--- Size of box
\setlength\fboxrule{1.5pt}    % <--- Line thickness of box
\newcommand{\mycfbox}[2]{%
    \colorlet{currentcolor}{.}%
    {\color{#1}%
    \smash{\fbox{\color{currentcolor}#2}}}%
}

\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)
          "\mycfbox{red}{ BMI = person.mass/person.mass\string^2;}"
       end
   end
end
\end{filecontents*}

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

\begin{document}

\lstinputlisting[caption = {Some class definition}]{person.m}

\end{document}

This question is relatively close to \fbox inside listings I merely used Martin Scharrer's answer and adapted it for the box coulouring with is other answer about \fcolorbox{<frame color>}{<background color>}{<text>}.

\documentclass{article}

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

\newsavebox{\mybox}
\begin{filecontents}{person.m}
classdef person
   properties %(here, properties is a keyword)
       mass=80;
       height=1.80;
   end
   methods
       function BMI = getBMI(height,weight)
           !\smash{\fcolorbox{red}{white}{\usebox\mybox}}!
       end
   end
end
\end{filecontents}
\begin{document}

\begin{lrbox}{\mybox}
    \lstinline[style=Matlab-editor]{BMI = person.mass/person.mass^2;}%
\end{lrbox}

\lstinputlisting[style=Matlab-editor,escapechar=!]{person.m}

\end{document}

enter image description here