[Tex/LaTex] Frames around few line of code in a algorithm

algorithmsframed

I want to put a box around lines of code and to add comments inside the box.

This is what I want it to achieve:
enter image description here

This is the example of code I am using for the algorithm.

\documentclass{article}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{subcaption}% http://ctan.org/pkg/subcaption
\captionsetup{compatibility=false}
\DeclareCaptionSubType*{algorithm}
\renewcommand\thesubalgorithm{\thetable\alph{subalgorithm}}
\DeclareCaptionLabelFormat{alglabel}{Alg.~#2}
\begin{document}
\begin{table}%
\begin{subalgorithm}{.5\textwidth}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}%\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}%\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile
    \State \textbf{return} $b$%\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{algo1}
\end{subalgorithm}%
\begin{subalgorithm}{.5\textwidth}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}%\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}%\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile
    \State \textbf{return} $b$%\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{algo2}
\end{subalgorithm}
\captionsetup{labelformat=alglabel}
\caption{Two algorithms}%
\label{tab:1}%
\end{table}
\end{document}

Best Answer

Here's some code to get you started. The idea is to use the ubiquitous \tikzmark to place some marks at the appropriate locations, and then to draw the boxes with the text; I used the fit library for this purpose:

The \Textbox command has two optional arguments and three mandatory ones:

\Textbox[<length1>][<length2>]{<name1>}{<name2>}{<text>}

where <length1> specifies the additional width of the box (default=2.5cm); <length2> controls the width of the box where the text will be typeset (default=2cm); <name1> and <name2> are previously set marks that will be used to draw the box, and <text> is the text that will be typeset (adjust the settings according to your needs).

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{algorithm}
\usepackage{algpseudocode} 
\usepackage{subcaption}
\usepackage{twoopt}
\usepackage{tikz}
\usetikzlibrary{fit}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner xsep=0pt] (#1) {};}
\newcommandtwoopt\Textbox[5][2.5cm][2cm]{%
\begin{tikzpicture}[remember picture,overlay]
  \coordinate (aux) at ([xshift=#1]#4);
  \node[inner ysep=3pt,yshift=0.6ex,draw=red,thick,
    fit=(#3) (aux),baseline] 
    (box) {};
  \node[text width=#2,anchor=north east,
    font=\sffamily\footnotesize,align=right] 
    at (box.north east) {#5};
\end{tikzpicture}%
}
\captionsetup{compatibility=false}
\DeclareCaptionSubType*{algorithm}
\renewcommand\thesubalgorithm{\thetable\alph{subalgorithm}}
\DeclareCaptionLabelFormat{alglabel}{Alg.~#2}

\begin{document}

\begin{table}%
\begin{subalgorithm}{.5\textwidth}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}%\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}%\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State\tikzmark{start1}$b\gets r$
      \State $r\gets a\bmod b$\tikzmark{end1}
    \EndWhile
    \State \textbf{return} $b$%\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{algo1}
\end{subalgorithm}%
\begin{subalgorithm}{.5\textwidth}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}%\Comment{The g.c.d. of a and b}
    \State\tikzmark{start2}$r\gets a\bmod b$\tikzmark{end2}
    \While{$r\not=0$}%\Comment{We have the answer if r is 0}
      \State\tikzmark{start3}$a\gets b$
      \State $b\gets r$\tikzmark{end3}
      \State $r\gets a\bmod b$
    \EndWhile
    \State\tikzmark{start4}\textbf{return} $b$\tikzmark{end4}%\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{algo2}
\end{subalgorithm}
\captionsetup{labelformat=alglabel}
\caption{Two algorithms}%
\label{tab:1}%
\Textbox{start1}{end1}{piece of code 1}
\Textbox{start2}{end2}{piece of code 2}
\Textbox[3cm]{start3}{end3}{piece of code 3}
\Textbox{start4}{end4}{piece of code 4}
\end{table}

\end{document}

enter image description here

Remark:

Since \tikzmark is being used and some coordinate calculations are involved, the code needs three runs to produce the correct output.