5

I would like to put frames around pieces of algorithms like in the following picture. If the background of the rectangle can be changed, it would be nice.

enter image description here

Here is the LaTeX code used for the algorithm.

\documentclass[10pt,a4paper]{article}
\usepackage[ruled]{algorithm2e}

\begin{document}

\begin{algorithm}[H]
    \caption{Le crible d'Eratosthène}

    \KwData{la liste $L$ des naturels compris de $2$ à $N$.}
    \KwResult{la liste $P$ des nombres premiers compris entre $2$ et $N$.}
    \BlankLine
    \For{$i = 2$ \KwTo $N$}{
        \If{$i$ n'est pas barré dans la liste $L$}{
            Barrer dans la liste $L$ les mutltiples $ki$ où $k \geq 2$.
        }
    }
    \Return{la liste $P$ des nombres non barrés.}
\end{algorithm} 

\end{document}
10

A job for \tikzmark; the \MyBox command allows to draw the colored frame; the syntax is

\MyBox[<vertical offset>]{<mark>}{<mark>}{<color>}

An example:

\documentclass[10pt,a4paper]{article}
\usepackage[ruled]{algorithm2e}
\usepackage{tikz}
\usetikzlibrary{calc,fit}

% code by Andrew Stacey
% http://tex.stackexchange.com/a/50054/3954    
\makeatletter
\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
  save picture id/.code={%
    \edef\pgf@temp{#1}%
    \immediate\write\pgfutil@auxout{%
      \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
  },
  if picture id/.code args={#1#2#3}{%
    \@ifundefined{save@pt@#1}{%
      \pgfkeysalso{#3}%
    }{
      \pgfkeysalso{#2}%
    }
  }
}

\def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}
\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] #1;}
\makeatother
% end of code by Andrew Stacey

\newcommand\MyBox[4][-1ex]{%
  \tikz[remember picture,overlay,pin distance=0cm]
  {\draw[draw=#4,line width=1pt,fill=#4!20,rectangle,rounded corners]
( $ (pic cs:#2) + (-1ex,2ex) $ ) rectangle ( $ (pic cs:#3) + (1ex,#1) $ );
}
}

\begin{document}

\MyBox{starta}{enda}{blue}
\MyBox[-3ex]{startb}{endb}{red}
\begin{algorithm}[H]
    \caption{Le crible d'Eratosthène}

    \tikzmark{starta}\KwData{la liste $L$ des naturels compris de $2$ à $N$.}
    \KwResult{la liste $P$ des nombres premiers compris entre $2$ et $N$.\tikzmark{enda}}
    \BlankLine
    \For{$i = 2$ \KwTo $N$}{
        \tikzmark{startb}\If{$i$ n'est pas barré dans la liste $L$}{
            Barrer dans la liste $L$ les mutltiples $ki$ où $k \geq 2$.\tikzmark{endb}
        }
    }
    \Return{la liste $P$ des nombres non barrés.}
\end{algorithm} 

\end{document}

enter image description here

  • Cool ! Can I focus several pieces in the same algorithm ? In my example it could be the IF block and the DATA-RESULT block. – projetmbc Oct 21 '12 at 18:10
  • @projetmbc Sure; simply use \tikzmark to place marks at the proper locations and then \MyBox to draw the box between the marks. – Gonzalo Medina Oct 21 '12 at 18:11
  • Last question. Can I use different colors ? – projetmbc Oct 21 '12 at 18:12
  • @projetmbc answer updated. – Gonzalo Medina Oct 21 '12 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.