3

I want to render my algorithm in a way, that the reader can see the functions within an extra frame like this:

enter image description here

For writing the pseudocode I would prefer the algorithmicx package.

Any advice is greatly appreciated.

Anthon
  • 314

1 Answers1

1

Another job for \tikzmark. In each use of \Function and \EndFunction for which you want a frame, you place some marks; you then use these marks as the first and second arguments, respectively, for \Drawbox; the third mandatory argument controls the width of the frame:

\documentclass{article}
\usepackage{algpseudocode}
\usepackage{tikz}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node (#1) {};}

\algdef{SE}[FUNCTION]{Function}{EndFunction}%
   [2]{\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}\vskip3pt}%
   {\algorithmicend\ \algorithmicfunction}

\newlength\funcwd
\settowidth\funcwd{\algorithmicfunction}
\addtolength\funcwd{3pt}

\newcommand\Drawbox[3]{%
\begin{tikzpicture}[remember picture,overlay]
\coordinate (s#1) at ([xshift=-\funcwd,yshift=6pt]#1.north west);
\coordinate (e#1) at ([xshift=#3,yshift=1pt]#2.south east);
\draw (s#1) rectangle (e#1);
\draw ([yshift=-1.2\baselineskip]s#1) -- ([yshift=-1.2\baselineskip]e#1|-s#1);
\end{tikzpicture}
}

\begin{document}

\begin{algorithmic}
\Function{\tikzmark{start1}CallA}{$a$} \label{alg:a}
    \State \Call{CalcSquare}{$a$}
\Function{\tikzmark{start2}CalcSquare}{$b$} \label{alg:b}
    \State \Return $b\times b$
\EndFunction\tikzmark{end2}
\EndFunction\tikzmark{end1}
\end{algorithmic}

\Drawbox{start1}{end1}{74pt}
\Drawbox{start2}{end2}{55pt}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128