How to highlight a portion of Matlab Code
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Consider following code posted for What can I use to typeset MATLAB code in my document?
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents
beginfilecontents*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
endfilecontents*
enddocument
and its output is
How can I highlight a portion of code like
highlighting matlab
add a comment |Â
up vote
6
down vote
favorite
Consider following code posted for What can I use to typeset MATLAB code in my document?
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents
beginfilecontents*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
endfilecontents*
enddocument
and its output is
How can I highlight a portion of code like
highlighting matlab
3
@Nasi, your current MWE does not reflect the visual output, could you please update your snippet ?
– BambOo
Aug 14 at 23:21
4
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...
– Will Robertson
Aug 15 at 0:53
1
@WillRobertson That's a good point, I updated my answer accordingly.
– marmot
Aug 15 at 1:29
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Consider following code posted for What can I use to typeset MATLAB code in my document?
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents
beginfilecontents*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
endfilecontents*
enddocument
and its output is
How can I highlight a portion of code like
highlighting matlab
Consider following code posted for What can I use to typeset MATLAB code in my document?
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents
beginfilecontents*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
endfilecontents*
enddocument
and its output is
How can I highlight a portion of code like
highlighting matlab
asked Aug 14 at 22:40
NAASI
1,16521324
1,16521324
3
@Nasi, your current MWE does not reflect the visual output, could you please update your snippet ?
– BambOo
Aug 14 at 23:21
4
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...
– Will Robertson
Aug 15 at 0:53
1
@WillRobertson That's a good point, I updated my answer accordingly.
– marmot
Aug 15 at 1:29
add a comment |Â
3
@Nasi, your current MWE does not reflect the visual output, could you please update your snippet ?
– BambOo
Aug 14 at 23:21
4
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...
– Will Robertson
Aug 15 at 0:53
1
@WillRobertson That's a good point, I updated my answer accordingly.
– marmot
Aug 15 at 1:29
3
3
@Nasi, your current MWE does not reflect the visual output, could you please update your snippet ?
– BambOo
Aug 14 at 23:21
@Nasi, your current MWE does not reflect the visual output, could you please update your snippet ?
– BambOo
Aug 14 at 23:21
4
4
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...
– Will Robertson
Aug 15 at 0:53
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...
– Will Robertson
Aug 15 at 0:53
1
1
@WillRobertson That's a good point, I updated my answer accordingly.
– marmot
Aug 15 at 1:29
@WillRobertson That's a good point, I updated my answer accordingly.
– marmot
Aug 15 at 1:29
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
12
down vote
accepted
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
begindocument
beginlstlisting[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
endlstlisting
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassbeamer
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
1
I tried this solution but it does not work if I change documentclassarticle to documentclassbeamer. I know my MWE did not make any mention of beamer but I would really appreciate if you could provide solution for it.
– NAASI
Aug 15 at 14:09
1
@NAASI I just replacedarticle
bybeamer
in the second example and it worked without problems. What precisely did you try?
– marmot
Aug 15 at 14:11
1
for me the red box is showing up at the bottom of slide. Thats why I asked
– NAASI
Aug 15 at 14:12
1
@NAASI You need to compile 2 or 3 times, did you do that?
– marmot
Aug 15 at 14:13
1
I am using overleaf.com and it complies on the go. do you think its the online editor thats causing the issue
– NAASI
Aug 15 at 14:15
 |Â
show 5 more comments
up vote
8
down vote
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 mycfboxred....
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
.
documentclassarticle
usepackage[T1]fontenc
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor % <--- Added
renewcommandfboxsep2pt % <--- Size of box
setlengthfboxrule1.5pt % <--- Line thickness of box
newcommandmycfbox[2]%
colorletcurrentcolor.%
color#1%
smashfboxcolorcurrentcolor#2%
usepackagefilecontents
beginfilecontents*person.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
"mycfboxred BMI = person.mass/person.massstring^2;"
end
end
end
endfilecontents*
lstset
style = Matlab-editor,
basicstyle = mlttfamily,
escapechar = ",
mlshowsectionrules = true,
begindocument
lstinputlisting[caption = Some class definition]person.m
enddocument
add a comment |Â
up vote
7
down vote
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>
.
documentclassarticle
usepackagelistings
usepackagenewverbs
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor
newsaveboxmybox
beginfilecontentsperson.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
!smashfcolorboxredwhiteuseboxmybox!
end
end
end
endfilecontents
begindocument
beginlrboxmybox
lstinline[style=Matlab-editor]BMI = person.mass/person.mass^2;%
endlrbox
lstinputlisting[style=Matlab-editor,escapechar=!]person.m
enddocument
@marmot all corrected
– BambOo
Aug 15 at 10:10
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
begindocument
beginlstlisting[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
endlstlisting
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassbeamer
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
1
I tried this solution but it does not work if I change documentclassarticle to documentclassbeamer. I know my MWE did not make any mention of beamer but I would really appreciate if you could provide solution for it.
– NAASI
Aug 15 at 14:09
1
@NAASI I just replacedarticle
bybeamer
in the second example and it worked without problems. What precisely did you try?
– marmot
Aug 15 at 14:11
1
for me the red box is showing up at the bottom of slide. Thats why I asked
– NAASI
Aug 15 at 14:12
1
@NAASI You need to compile 2 or 3 times, did you do that?
– marmot
Aug 15 at 14:13
1
I am using overleaf.com and it complies on the go. do you think its the online editor thats causing the issue
– NAASI
Aug 15 at 14:15
 |Â
show 5 more comments
up vote
12
down vote
accepted
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
begindocument
beginlstlisting[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
endlstlisting
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassbeamer
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
1
I tried this solution but it does not work if I change documentclassarticle to documentclassbeamer. I know my MWE did not make any mention of beamer but I would really appreciate if you could provide solution for it.
– NAASI
Aug 15 at 14:09
1
@NAASI I just replacedarticle
bybeamer
in the second example and it worked without problems. What precisely did you try?
– marmot
Aug 15 at 14:11
1
for me the red box is showing up at the bottom of slide. Thats why I asked
– NAASI
Aug 15 at 14:12
1
@NAASI You need to compile 2 or 3 times, did you do that?
– marmot
Aug 15 at 14:13
1
I am using overleaf.com and it complies on the go. do you think its the online editor thats causing the issue
– NAASI
Aug 15 at 14:15
 |Â
show 5 more comments
up vote
12
down vote
accepted
up vote
12
down vote
accepted
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
begindocument
beginlstlisting[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
endlstlisting
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassbeamer
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
begindocument
beginlstlisting[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
endlstlisting
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassarticle
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
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.
documentclassbeamer
usepackage[T1]fontenc
usepackagebigfoot % to allow verbatim in footnote
usepackage[numbered,framed]matlab-prettifier
usepackagetikz
usetikzlibrarytikzmark
usetikzmarklibrarylistings
newcountertmkcount
tikzset
use tikzmark/.style=
remember picture,
overlay,
execute at end picture=
stepcountertmkcount
,
,
tikzmark suffix=-thetmkcount
usepackagefilecontents
beginfilecontents*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
endfilecontents*
begindocument
lstinputlisting[style=Matlab-editor,name=4Will]person.m
begintikzpicture[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);
endtikzpicture
enddocument
edited Aug 15 at 15:12
answered Aug 14 at 23:28


marmot
55.1k459119
55.1k459119
1
I tried this solution but it does not work if I change documentclassarticle to documentclassbeamer. I know my MWE did not make any mention of beamer but I would really appreciate if you could provide solution for it.
– NAASI
Aug 15 at 14:09
1
@NAASI I just replacedarticle
bybeamer
in the second example and it worked without problems. What precisely did you try?
– marmot
Aug 15 at 14:11
1
for me the red box is showing up at the bottom of slide. Thats why I asked
– NAASI
Aug 15 at 14:12
1
@NAASI You need to compile 2 or 3 times, did you do that?
– marmot
Aug 15 at 14:13
1
I am using overleaf.com and it complies on the go. do you think its the online editor thats causing the issue
– NAASI
Aug 15 at 14:15
 |Â
show 5 more comments
1
I tried this solution but it does not work if I change documentclassarticle to documentclassbeamer. I know my MWE did not make any mention of beamer but I would really appreciate if you could provide solution for it.
– NAASI
Aug 15 at 14:09
1
@NAASI I just replacedarticle
bybeamer
in the second example and it worked without problems. What precisely did you try?
– marmot
Aug 15 at 14:11
1
for me the red box is showing up at the bottom of slide. Thats why I asked
– NAASI
Aug 15 at 14:12
1
@NAASI You need to compile 2 or 3 times, did you do that?
– marmot
Aug 15 at 14:13
1
I am using overleaf.com and it complies on the go. do you think its the online editor thats causing the issue
– NAASI
Aug 15 at 14:15
1
1
I tried this solution but it does not work if I change documentclassarticle to documentclassbeamer. I know my MWE did not make any mention of beamer but I would really appreciate if you could provide solution for it.
– NAASI
Aug 15 at 14:09
I tried this solution but it does not work if I change documentclassarticle to documentclassbeamer. I know my MWE did not make any mention of beamer but I would really appreciate if you could provide solution for it.
– NAASI
Aug 15 at 14:09
1
1
@NAASI I just replaced
article
by beamer
in the second example and it worked without problems. What precisely did you try?– marmot
Aug 15 at 14:11
@NAASI I just replaced
article
by beamer
in the second example and it worked without problems. What precisely did you try?– marmot
Aug 15 at 14:11
1
1
for me the red box is showing up at the bottom of slide. Thats why I asked
– NAASI
Aug 15 at 14:12
for me the red box is showing up at the bottom of slide. Thats why I asked
– NAASI
Aug 15 at 14:12
1
1
@NAASI You need to compile 2 or 3 times, did you do that?
– marmot
Aug 15 at 14:13
@NAASI You need to compile 2 or 3 times, did you do that?
– marmot
Aug 15 at 14:13
1
1
I am using overleaf.com and it complies on the go. do you think its the online editor thats causing the issue
– NAASI
Aug 15 at 14:15
I am using overleaf.com and it complies on the go. do you think its the online editor thats causing the issue
– NAASI
Aug 15 at 14:15
 |Â
show 5 more comments
up vote
8
down vote
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 mycfboxred....
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
.
documentclassarticle
usepackage[T1]fontenc
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor % <--- Added
renewcommandfboxsep2pt % <--- Size of box
setlengthfboxrule1.5pt % <--- Line thickness of box
newcommandmycfbox[2]%
colorletcurrentcolor.%
color#1%
smashfboxcolorcurrentcolor#2%
usepackagefilecontents
beginfilecontents*person.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
"mycfboxred BMI = person.mass/person.massstring^2;"
end
end
end
endfilecontents*
lstset
style = Matlab-editor,
basicstyle = mlttfamily,
escapechar = ",
mlshowsectionrules = true,
begindocument
lstinputlisting[caption = Some class definition]person.m
enddocument
add a comment |Â
up vote
8
down vote
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 mycfboxred....
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
.
documentclassarticle
usepackage[T1]fontenc
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor % <--- Added
renewcommandfboxsep2pt % <--- Size of box
setlengthfboxrule1.5pt % <--- Line thickness of box
newcommandmycfbox[2]%
colorletcurrentcolor.%
color#1%
smashfboxcolorcurrentcolor#2%
usepackagefilecontents
beginfilecontents*person.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
"mycfboxred BMI = person.mass/person.massstring^2;"
end
end
end
endfilecontents*
lstset
style = Matlab-editor,
basicstyle = mlttfamily,
escapechar = ",
mlshowsectionrules = true,
begindocument
lstinputlisting[caption = Some class definition]person.m
enddocument
add a comment |Â
up vote
8
down vote
up vote
8
down vote
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 mycfboxred....
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
.
documentclassarticle
usepackage[T1]fontenc
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor % <--- Added
renewcommandfboxsep2pt % <--- Size of box
setlengthfboxrule1.5pt % <--- Line thickness of box
newcommandmycfbox[2]%
colorletcurrentcolor.%
color#1%
smashfboxcolorcurrentcolor#2%
usepackagefilecontents
beginfilecontents*person.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
"mycfboxred BMI = person.mass/person.massstring^2;"
end
end
end
endfilecontents*
lstset
style = Matlab-editor,
basicstyle = mlttfamily,
escapechar = ",
mlshowsectionrules = true,
begindocument
lstinputlisting[caption = Some class definition]person.m
enddocument
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 mycfboxred....
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
.
documentclassarticle
usepackage[T1]fontenc
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor % <--- Added
renewcommandfboxsep2pt % <--- Size of box
setlengthfboxrule1.5pt % <--- Line thickness of box
newcommandmycfbox[2]%
colorletcurrentcolor.%
color#1%
smashfboxcolorcurrentcolor#2%
usepackagefilecontents
beginfilecontents*person.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
"mycfboxred BMI = person.mass/person.massstring^2;"
end
end
end
endfilecontents*
lstset
style = Matlab-editor,
basicstyle = mlttfamily,
escapechar = ",
mlshowsectionrules = true,
begindocument
lstinputlisting[caption = Some class definition]person.m
enddocument
edited Aug 15 at 0:43
answered Aug 14 at 23:35


Milo
5,42821345
5,42821345
add a comment |Â
add a comment |Â
up vote
7
down vote
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>
.
documentclassarticle
usepackagelistings
usepackagenewverbs
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor
newsaveboxmybox
beginfilecontentsperson.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
!smashfcolorboxredwhiteuseboxmybox!
end
end
end
endfilecontents
begindocument
beginlrboxmybox
lstinline[style=Matlab-editor]BMI = person.mass/person.mass^2;%
endlrbox
lstinputlisting[style=Matlab-editor,escapechar=!]person.m
enddocument
@marmot all corrected
– BambOo
Aug 15 at 10:10
add a comment |Â
up vote
7
down vote
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>
.
documentclassarticle
usepackagelistings
usepackagenewverbs
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor
newsaveboxmybox
beginfilecontentsperson.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
!smashfcolorboxredwhiteuseboxmybox!
end
end
end
endfilecontents
begindocument
beginlrboxmybox
lstinline[style=Matlab-editor]BMI = person.mass/person.mass^2;%
endlrbox
lstinputlisting[style=Matlab-editor,escapechar=!]person.m
enddocument
@marmot all corrected
– BambOo
Aug 15 at 10:10
add a comment |Â
up vote
7
down vote
up vote
7
down vote
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>
.
documentclassarticle
usepackagelistings
usepackagenewverbs
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor
newsaveboxmybox
beginfilecontentsperson.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
!smashfcolorboxredwhiteuseboxmybox!
end
end
end
endfilecontents
begindocument
beginlrboxmybox
lstinline[style=Matlab-editor]BMI = person.mass/person.mass^2;%
endlrbox
lstinputlisting[style=Matlab-editor,escapechar=!]person.m
enddocument
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>
.
documentclassarticle
usepackagelistings
usepackagenewverbs
usepackage[numbered,framed]matlab-prettifier
usepackagexcolor
newsaveboxmybox
beginfilecontentsperson.m
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
!smashfcolorboxredwhiteuseboxmybox!
end
end
end
endfilecontents
begindocument
beginlrboxmybox
lstinline[style=Matlab-editor]BMI = person.mass/person.mass^2;%
endlrbox
lstinputlisting[style=Matlab-editor,escapechar=!]person.m
enddocument
edited Aug 15 at 10:10
answered Aug 14 at 23:44


BambOo
2,365323
2,365323
@marmot all corrected
– BambOo
Aug 15 at 10:10
add a comment |Â
@marmot all corrected
– BambOo
Aug 15 at 10:10
@marmot all corrected
– BambOo
Aug 15 at 10:10
@marmot all corrected
– BambOo
Aug 15 at 10:10
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f446078%2fhow-to-highlight-a-portion-of-matlab-code%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
@Nasi, your current MWE does not reflect the visual output, could you please update your snippet ?
– BambOo
Aug 14 at 23:21
4
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...
– Will Robertson
Aug 15 at 0:53
1
@WillRobertson That's a good point, I updated my answer accordingly.
– marmot
Aug 15 at 1:29