So definieren Sie eine Bezeichnung für eine Reihe aufeinanderfolgender Zeilen in einem Algorithmus

So definieren Sie eine Bezeichnung für eine Reihe aufeinanderfolgender Zeilen in einem Algorithmus

Nehmen wir einen typischen Algorithmus wie diesen an:

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

Ich muss Beschriftungen für einige aufeinanderfolgende Zeilen des Algorithmus einfügen, und zwar etwa wie im Bild unten (eine vertikale geschweifte Klammer, um die Zeilen zwischen Zeile #n und #m abzudecken, und dann eine Beschriftung, z. B. a, davor platzieren):

Bildbeschreibung hier eingeben

Wie soll ich das bitte machen?

Antwort1

Hier eine Option mittikzmark

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

verwandte Informationen