アルゴリズム内の連続する一連の行にラベルを定義する方法

アルゴリズム内の連続する一連の行にラベルを定義する方法

次のような典型的なアルゴリズムを想定してみましょう。

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{enumerate} 
  \item Some text\ldots\par
  \vspace{-\baselineskip}

  \begin{minipage}{\linewidth}
  \begin{algorithm}[H]
    \caption{Euclid’s algorithm}\label{euclid}
    \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\label{euclidendwhile}
        \State \textbf{return} $b$\Comment{The gcd is b}
      \EndProcedure
    \end{algorithmic}
  \end{algorithm}
  \end{minipage}

  Some more text\ldots
\end{enumerate}
\end{document}

アルゴリズムの連続する行にラベルを挿入する必要があります。具体的には、以下の画像のようなものです (行 #n から #m の間の行をカバーする垂直の中括弧、次にaその前にたとえばラベルを配置します)。

ここに画像の説明を入力してください

どうすればいいでしょうか?

答え1

ここでオプションtikzmark

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usepackage{algorithm,algpseudocode}


\newcommand{\tikzmark}[2][]{\tikz[remember picture,overlay]\node[inner xsep=0pt,inner ysep=1ex,#1](#2){};}

\begin{document}
\begin{enumerate} 
  \item Some text\ldots\par
  \vspace{-\baselineskip}

  \begin{minipage}{\linewidth}
  \begin{algorithm}[H]
    \caption{Euclid’s algorithm}\label{euclid}
    \begin{algorithmic}[1]
      \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
        \State $r\gets a\bmod b$
        \While{\tikzmark[xshift=-1.2cm]{A}$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\tikzmark{B}\label{euclidendwhile}
        \State \textbf{return} $b$\Comment{The gcd is b}
      \EndProcedure
    \end{algorithmic}
  \end{algorithm}
  \end{minipage}

\begin{tikzpicture}[remember picture,overlay]
\draw[red,thick,decorate,decoration={brace,amplitude=5pt,mirror}](A.north)--node[left=.3em]{a}(A|-B);
\end{tikzpicture}

  Some more text\ldots
\end{enumerate}
\end{document}

関連情報