6

I am trying to achieve something like this:

enter image description here

This image is linked from this answer. This answer also provides a solution and seems simpler. Both answers deal with the algorithmicx package however. I am interested in the solution for the algorithm2e package.

I read the documentation of the algorithm2e package but didn't find an answer.

How can I achieve the colored effect?

EDIT:

Here is sample code to create a minimum working example.

\documentclass{article}

\usepackage[ruled]{algorithm2e} % http://www.ctan.org/pkg/algorithm2e

\begin{document}

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

\end{document} 
0

1 Answer 1

12

Is this solution acceptable?

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

Result:

Result

Caveats:

  • Command \HiLi should only be used as the first non-blank in the line
  • The highlight does not fill the line up to the left margin, but instead it is indented according with the line in which it appears.
1
  • how do i fill up the highlight to the left margin ?
    – Kong
    Aug 13, 2017 at 14:23

You must log in to answer this question.

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