알고리즘에서 일련의 연속 라인에 대한 레이블을 정의하는 방법

알고리즘에서 일련의 연속 라인에 대한 레이블을 정의하는 방법

다음과 같은 일반적인 알고리즘을 가정해 보겠습니다.

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

관련 정보