围绕算法中的几行代码构建框架

围绕算法中的几行代码构建框架

我想在代码行周围放置一个框,并在框内添加注释。

这就是我想要实现的目标: 在此处输入图片描述

这是我用于该算法的代码示例。

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

答案1

以下是一些代码,可帮助您入门。想法是使用无处不在的库\tikzmark在适当的位置放置一些标记,然后用文本绘制方框;我fit为此使用了库:

\Textbox 命令有两个可选参数和三个强制参数:

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

其中<length1>指定框的附加宽度(默认值= 2.5cm);<length2>控制将排版文本的框的宽度(默认值= 2cm);<name1><name2>是先前设置的用于绘制框的标记,<text>是将排版的文本(根据需要调整设置)。

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

在此处输入图片描述

评论:

由于\tikzmark正在使用并且涉及一些坐标计算,代码需要运行三次才能产生正确的输出。

相关内容