0

I stumbled upon the solution from this question: How to highlight sections of my code in algorithm?
and incorporated it into the following MWE which shows two flaws:

\documentclass{article}
\usepackage[linesnumbered,lined,ruled,commentsnumbered]{algorithm2e}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{fit,calc}
\newcommand*{\tikzmk}[1]{\tikz[remember picture,overlay,] \node (#1) {};\ignorespaces}
%define a boxing command, argument = colour of box
\newcommand{\boxit}[1]{\tikz[remember picture,overlay]{\node[yshift=3pt,fill=#1,opacity=.25,fit={(A)($(B)+(.95\linewidth,.8\baselineskip)$)}] {};}\ignorespaces}

\begin{document}
\begin{algorithm}[h]
 \caption{MWE}
 a $\leftarrow$ b \;
 \If{$x > 0$}{
    \tikzmk{A}\If{$x > 0$}{
        a $\leftarrow b$ \;
    }\tikzmk{B}\boxit{cyan}
 }
\end{algorithm}
\end{document}

enter image description here

  1. The solution inserts an additional line (line 6 in the image)
  2. The solution draws the box out of bounds (see right boundary)

As far as flaw 2 goes, I augmented the boxit command to also take the current indentation level as an argument, i.e.,

\documentclass{article}
\usepackage[linesnumbered,lined,ruled,commentsnumbered]{algorithm2e}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{fit,calc}
\newcommand*{\tikzmk}[1]{\tikz[remember picture,overlay,] \node (#1) {};\ignorespaces}
%define a boxing command, argument = colour of box
\newcommand{\boxit}[2]{\tikz[remember picture,overlay]{\node[yshift=3pt,fill=#1,opacity=.25,fit={(A)($(B)+(\dimexpr\algowidth-#2\algoskipindent,.8\baselineskip)$)}] {};}\ignorespaces}

\begin{document}
\begin{algorithm}[h]
 \caption{MWE}
 a $\leftarrow$ b \;
 \If{$x > 0$}{
    \tikzmk{A}\If{$x > 0$}{
        a $\leftarrow b$ \;
    }\tikzmk{B}\boxit{cyan}{2}
 }
\end{algorithm}
\end{document}

enter image description here

Which is okay for me, but I can not remove flaw 1 with the additional line, any suggestions?

1

2 Answers 2

0

When copying the algorithm block a second time, a couple of remaining problem seem to be provided by your code.

So here is a way to do it using package tcolorbox. You can set further options for the tcolorbox, e.g. to remove the black arc etc. Line 3 seems to be needed to render the highlighted area.

Same algorithm, with and without tcolorbox

\documentclass{article}
\usepackage[linesnumbered,lined,ruled,commentsnumbered]{algorithm2e}
\usepackage{tcolorbox}% <<<

\begin{document}

    \begin{algorithm}[h]
        \caption{MWE with tcolorbox}
        a $\leftarrow$ b \;

        \If{$x > 0$}{
        \begin{tcolorbox}% <<<
            \If{$x > 0$}{
                a $\leftarrow$ b ;
            }
        \end{tcolorbox}% <<<
        }
    \end{algorithm}


    \begin{algorithm}[h]
        \caption{MWE with tcolorbox}
        a $\leftarrow$ b;

        \If{$x > 0$}{
            \If{$x > 0$}{
                a $\leftarrow$ b;
            }
        }
    \end{algorithm}
\end{document}
0

If you can afford to highlight only certain lines, here's an alternative using packages xcolor and soul, where soul defines a highlight command \hl{} with some limitations:

higlighted code

\documentclass{article}
\usepackage[linesnumbered,lined,ruled,commentsnumbered]{algorithm2e}
\usepackage{xcolor, soul}% <<<
\sethlcolor{yellow}% <<<

\begin{document}
Here is \hl{some text.}

    \begin{algorithm}[h]
        \caption{MWE with color}
        a $\leftarrow$ b \;

        \If{$x > 0$}{
            \If{$x > 0$}{% can't be included in \hl{}
                \hl{a $\leftarrow$ b ;}% <<<
            }
        }
    \end{algorithm}


    \begin{algorithm}[h]
        \caption{MWE with tcolorbox}
        a $\leftarrow$ b;

        \If{$x > 0$}{
            \If{$x > 0$}{
                a $\leftarrow$ b;
            }
        }
    \end{algorithm}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .