17

enter image description here

\documentclass{article}
\usepackage[ruled]{algorithm2e} % http://www.ctan.org/pkg/algorithm2e
\usepackage{xcolor} % http://www.ctan.org/tex-archive/macros/latex/contrib/xcolor

\def\HiLi{\leavevmode\rlap{\hbox to \hsize{\color{yellow!50}\leaders\hrule height .8\baselineskip depth .5ex\hfill}}}

\begin{document}
\begin{algorithm}[h]
 \caption{Evolutionary algorithm}
 initialize population \;
 \HiLi\For( \emph{Evolutionary loop}){$g := 1$ to $G_{max}$}
 {
    \HiLi do things \;
    evolve population \;
 }
 celebrate \;
\end{algorithm}

\end{document}

The code gives me the image below. How can i fill up the highlight to the left margin like in the 1st picture and remove the whitespace between highlights ?

enter image description here

2 Answers 2

20

I would do this using TikZ. The idea is to use TikZ to mark the top and bottom of the region to be marked, and then use a boxing macro which uses the fit library to make a node that fits the marked nodes, with the width calculated automatically.

Because this uses the [remember picture] functionality of TikZ, two compilations will be needed to get the boxes in their correct position.

\documentclass{article}
\usepackage[ruled]{algorithm2e} % http://www.ctan.org/pkg/algorithm2e
\usepackage{xcolor} % http://www.ctan.org/tex-archive/macros/latex/contrib/xcolor
\usepackage{tikz}
\usetikzlibrary{fit,calc}
%define a marking command
\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}
%define some colours according to algorithm parts (or any other method you like)
\colorlet{mypink}{red!40}
\colorlet{myblue}{cyan!60}

\begin{document}
\begin{algorithm}[h]
 \caption{Evolutionary algorithm}
 initialize population \;
 \tikzmk{A}\For( \emph{Evolutionary loop}){$g := 1$ to $G_{max}$}
 {
    do things \;
    evolve population \;
 }\tikzmk{B}
 \boxit{mypink}
 celebrate \;
 \tikzmk{A}\For( \emph{Evolutionary loop}){$g := 1$ to $G_{max}$}
 {
    do things \;
    evolve population \;
 }\tikzmk{B}
 \boxit{myblue}
\end{algorithm}
\end{document}

enter image description here

0
0

Inspired by Alan Munn's answer, we propose one more simple way to make it, with the help of ChatGPT. Enjoy this one :)

The command \highlight is convenient and easy-to-use. You can adjust the shift parameters to meet your requirements.

\documentclass{article}
\usepackage[ruled, lined,linesnumbered, longend]{algorithm2e}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newcommand{\highlight}[4]{
    \tikz[overlay, remember picture]{
        \fill[#2!50, opacity=0.5] 
        ([xshift={#3[0]}, yshift={#3[1]}]pic cs:#1-start) rectangle ([xshift={#4[0]}, yshift={#4[1]}]pic cs:#1-end);
    }
}

\begin{document}

%%%%%%%%% Adjust the shifts parameters %%%%%%%%%%%%%%%

\begin{algorithm}[h]
    \caption{Evolutionary algorithm}
    initialize population \;
    \tikzmark{highlight1-start}\For( \emph{Evolutionary loop}){$g := 1$ to $G_{max}$}
    {
        do things \;
        evolve population \tikzmark{highlight1-end}\;
    }
    celebrate \;
    \tikzmark{highlight2-start}\For( \emph{Evolutionary loop}){$g := 1$ to $G_{max}$}
    {
        do things \;
        evolve population \tikzmark{highlight2-end}\;
    }
\end{algorithm}
    
\highlight{highlight1}{red}{{-0.2em,1em}}{{20em,-1.5em}}
\highlight{highlight2}{blue}{{-0.2em,1em}}{{20em,-1.5em}}
   
\end{document}

The results are as follows (without/with adjustment)

You must log in to answer this question.

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