TransWikia.com

Coloring the lines of an algorithm

TeX - LaTeX Asked on March 27, 2021

I’m writing a report for a class project and I need to highlight several lines within some of the algorithms in the report. I managed to do so, but I don’t like the solution I got. Here is a picture (see code of MWE below).

enter image description here

To get this, I used color{red}, then put the text I wanted to highlight, followed by color{black}. I wonder if I can highlight in red the same lines of the algorithm but not the line numbers and also not the comment a miracle in polynomial time. What solutions are there for this? This answer proposes redefining commands of the package but they are not using the same package as I am, and I wonder whether it can be made more simply or not.

If I could be given advice on how to do this, while sticking to the package algorithm2e, as far as it is possible, that would be great.

Code:

documentclass[12pt]{article}
pdfoutput=1
usepackage[linesnumbered,titlenumbered,ruled,vlined,resetcount,algosection]{algorithm2e}
usepackage[usenames,dvipsnames]{xcolor}
usepackage{color}

begin{document}
title{MWE}
maketitle
section{Algorithm}

begin{algorithm}
    caption{3-SAT in polynomial time}
    label{alg:hs}
    DontPrintSemicolon
    KwIn{List of clauses}
    KwOut{The clauses are satisfiable}
    SetKwProg{Fn}{Function}{ is}{end}
    Fn{textsc{IsSatisfiable}($C$)} {
    
        color{red} %<- my best attempt
        $kgets 0$ ;
        For {$i = 1$ bf{to} $|C|$}
        {
            $c_i gets $ $i$-th clause ;
            $L gets$ list of literals of $c_i$ ;
            $kgets f(k, L)$ tcp{a miracle in polynomial time}
        }
        color{black} %<- go back to color black
        
        If {$k$ is even} {
            Return true
        }
        Else {
            Return false
        }
        
    }
end{algorithm}

end{document}

One Answer

I've never used algorithm2e but looking at its documentation (Sec. 9.5.3) I'd say the most sensible thing is to redefine the fonts for the numbers and comments to explicitly include a directive to switch the color to black.

documentclass[12pt]{article}

usepackage[linesnumbered,titlenumbered,ruled,vlined,resetcount,algosection]{algorithm2e}
usepackage[usenames,dvipsnames]{xcolor}
usepackage{color}

%% These three lines  !!!
SetNlSty{textbf}{color{black}}{}
newcommand*{mycommentfont}[1]{textcolor{black}{ttfamily#1}}
SetCommentSty{mycommentfont}

begin{document}
title{MWE}
maketitle
section{Algorithm}

begin{algorithm}
    caption{3-SAT in polynomial time}
    label{alg:hs}
    DontPrintSemicolon
    KwIn{List of clauses}
    KwOut{The clauses are satisfiable}
    SetKwProg{Fn}{Function}{ is}{end}
    Fn{textsc{IsSatisfiable}($C$)} {
        begingroup
        color{red}
        $kgets 0$ ;
        For {$i = 1$ bf{to} $|C|$}
        {
            $c_i gets $ $i$-th clause ;
            $L gets$ list of literals of $c_i$ ;
            $kgets f(k, L)$ tcp{a miracle in polynomial time}
        }
        endgroup            
        If {$k$ is even} {
            Return true
        }
        Else {
            Return false
        }
        
    }
end{algorithm}

end{document}

enter image description here

The extra definition of mycommentfont is necessary because the font commands of algorithm2e expect a single macro name (without ) of a macro taking one argument.

Correct answer by campa on March 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP