I am currently working on a LaTeX beamer
presentation where I have to explain an algorithm. So I wrote this algorithm with
\usepackage{algorithm,algpseudocode}
This is the current algorithm:
\begin{frame}
\begin{algorithm}[H]
\begin{algorithmic}
\Function{tarjan}{Node* node}
\State $node.visited \gets $ \textbf{true}
\State $node.index \gets indexCounter$
\State $s.push(node)$
\ForAll{$successor$ in $node.successors$}
\If{$!node.visited$}
\Call{tarjan}{successor}
\EndIf
\State $node.lowlink \gets$ \Call{min}{$node.lowlink, successor.lowlink$}
\EndFor
\If{$node.lowlink == node.index$}
\Repeat
\State $successor \gets stack.pop()$
\Until{$successor == node$}
\EndIf
\EndFunction
\end{algorithmic}
\label{alg:seq2}
\end{algorithm}
\end{frame}
It looks like that:
Now I would like to mark a block and write a comment to it:
I made the box with Gimp. How do I get this result with LaTeX?
(Notes / hints for a better usage of the algorithmic
environment are also welcome. It's the first time I use it.)
Best Answer
My solution leads to:
The idea is based on the
tikzmark
macro and actually it is an adaptation from Background coloring with overlay specification in algorithm2e + beamer package with the difference that you require an annotation.The code is:
Thanks to the command
\boxto
and\tikzmark
you define the delimeters to the zone to be highlighted. Notice that you need to compile twice. Subsequently, inside thetikzpicture
it is possible to use the anchora
as base to define the rectangle for the annotation, inserted as a node.If you want to create some animation, for example display the note after having highlighted the algorithm, you can slightly modify the previous code into: