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?

optional
  • 161
  • 5

2 Answers2

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 &gt; 0$}{
    \begin{tcolorbox}% &lt;&lt;&lt;
        \If{$x &gt; 0$}{
            a $\leftarrow$ b ;
        }
    \end{tcolorbox}% &lt;&lt;&lt;
    }
\end{algorithm}


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

    \If{$x &gt; 0$}{
        \If{$x &gt; 0$}{
            a $\leftarrow$ b;
        }
    }
\end{algorithm}

\end{document}

MS-SPO
  • 11,519
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 &gt; 0$}{
        \If{$x &gt; 0$}{% can't be included in \hl{}
            \hl{a $\leftarrow$ b ;}% &lt;&lt;&lt;
        }
    }
\end{algorithm}


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

    \If{$x &gt; 0$}{
        \If{$x &gt; 0$}{
            a $\leftarrow$ b;
        }
    }
\end{algorithm}

\end{document}

MS-SPO
  • 11,519