How to highlight a portion of Matlab Code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
3












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 enter image description here



How can I highlight a portion of code likeenter image description here







share|improve this question
















  • 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














up vote
6
down vote

favorite
3












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 enter image description here



How can I highlight a portion of code likeenter image description here







share|improve this question
















  • 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












up vote
6
down vote

favorite
3









up vote
6
down vote

favorite
3






3





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 enter image description here



How can I highlight a portion of code likeenter image description here







share|improve this question












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 enter image description here



How can I highlight a portion of code likeenter image description here









share|improve this question











share|improve this question




share|improve this question










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












  • 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










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


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.



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


enter image description here






share|improve this answer


















  • 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 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




    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

















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.



enter image description here



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





share|improve this answer





























    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


    enter image description here






    share|improve this answer






















    • @marmot all corrected
      – BambOo
      Aug 15 at 10:10










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "85"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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


    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.



    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


    enter image description here






    share|improve this answer


















    • 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 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




      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














    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


    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.



    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


    enter image description here






    share|improve this answer


















    • 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 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




      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












    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


    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.



    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


    enter image description here






    share|improve this answer














    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


    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.



    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


    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








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




      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




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




      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










    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.



    enter image description here



    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





    share|improve this answer


























      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.



      enter image description here



      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





      share|improve this answer
























        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.



        enter image description here



        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





        share|improve this answer














        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.



        enter image description here



        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 15 at 0:43

























        answered Aug 14 at 23:35









        Milo

        5,42821345




        5,42821345




















            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


            enter image description here






            share|improve this answer






















            • @marmot all corrected
              – BambOo
              Aug 15 at 10:10














            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


            enter image description here






            share|improve this answer






















            • @marmot all corrected
              – BambOo
              Aug 15 at 10:10












            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


            enter image description here






            share|improve this answer














            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


            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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
















            • @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

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery