# How to highlight a portion of Matlab Code

Consider following code posted for What can I use to typeset MATLAB code in my document?

\documentclass{article}

\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^2;
end
end
end
\end{filecontents*}
\end{document}


and its output is

How can I highlight a portion of code like

• @Nasi, your current MWE does not reflect the visual output, could you please update your snippet ? Aug 14, 2018 at 23:21
• I would be very interested to see a solution to this that doesn't require marking up the listings — triggered either by a comment in the source or a line number passed as an option in the listings setup... Aug 15, 2018 at 0:53
• @WillRobertson That's a good point, I updated my answer accordingly.
– user121799
Aug 15, 2018 at 1:29

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}


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}


• I tried this solution but it does not work if I change \documentclass{article} to \documentclass{beamer}. I know my MWE did not make any mention of beamer but I would really appreciate if you could provide solution for it. Aug 15, 2018 at 14:09
• @NAASI I just replaced article by beamer in the second example and it worked without problems. What precisely did you try?
– user121799
Aug 15, 2018 at 14:11
• for me the red box is showing up at the bottom of slide. Thats why I asked Aug 15, 2018 at 14:12
• @NAASI You need to compile 2 or 3 times, did you do that?
– user121799
Aug 15, 2018 at 14:13
• I am using overleaf.com and it complies on the go. do you think its the online editor thats causing the issue Aug 15, 2018 at 14:15

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.

\documentclass{article}

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

\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}


• @marmot all corrected Aug 15, 2018 at 10:10