アルゴリズム、指定された番号で行番号を開始する方法(algorithm2e を使用)

アルゴリズム、指定された番号で行番号を開始する方法(algorithm2e を使用)

特定の番号でコードを開始するにはどうしたらよいか疑問に思います。これは、アルゴリズムが 2 ページに分割されている場合に必要です。

1 行目ではなく 39 行目から開始したい小さな例です。それを実行する方法はありますか?

\documentclass[paper=a4,toc=bibliography,nonchapterprefix,parskip=true]{scrreprt}

\usepackage[linesnumbered]{algorithm2e}

\begin{document}

\IncMargin{1em}
\begin{algorithm}
  \SetKwData{Left}{left}
  \SetKwData{Up}{up}
  \SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}
  \SetKwInOut{Output}{output}

 \Indm
 \Input{A bitmap $Im$ of size $w\times l$}
 \Output{A partition of the bitmap}
\Indp
\BlankLine
  \For{$i\leftarrow 2$ \KwTo $l$}{
    \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
    \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\; }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
\end{algorithm}
\DecMargin{1em}

よろしくお願いします!

答え1

パッケージにはオプションalgorithm2eがありますnoresetcountが、これはすべてのアルゴリズムに連続して番号を付ける効果があります。

\rememberlines環境の終了時に現在の行番号を保存するマクロalgorithmと、\resumenumberingその値を使用するマクロを定義できます。

\documentclass[paper=a4,toc=bibliography,parskip=true]{scrreprt}

\usepackage[linesnumbered]{algorithm2e}

\newcommand{\rememberlines}{\xdef\rememberedlines{\number\value{AlgoLine}}}
\newcommand{\resumenumbering}{\setcounter{AlgoLine}{\rememberedlines}}

\begin{document}

\IncMargin{1em}
\begin{algorithm}
  \SetKwData{Left}{left}
  \SetKwData{Up}{up}
  \SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}
  \SetKwInOut{Output}{output}

  \Indm
  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
  \Indp
  \BlankLine
  \For{$i\leftarrow 2$ \KwTo $l$}{
    \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}
    \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}}
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}
  }
\rememberlines
\end{algorithm}

\begin{algorithm}
\resumenumbering
  \SetKwData{Left}{left}
  \SetKwData{Up}{up}
  \SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}
  \SetKwInOut{Output}{output}

  \Indm
  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
  \Indp
  \BlankLine
  \For{$i\leftarrow 2$ \KwTo $l$}{
    \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}
    \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}}
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}
  }
\end{algorithm}

\end{document}

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

関連情報