If EndIf 내부의 알고리즘 분할

If EndIf 내부의 알고리즘 분할

에 대한 후속 질문입니다.Latex를 사용한 알고리즘의 옵션.

코드는 다음과 같습니다.

\begin{document}
\begin{algorithm}
  \caption{My algorithme}
  \begin{algorithmic}[1]
    \Donnees: My data
    \Statex% Blank line
    \Debut
      \LState $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \LState $a\gets b$
        \LState $b\gets r$
        \LState $r\gets a\bmod b$
        \If {condition}
          \LState instruction
          \algstore{testcont} 
  \end{algorithmic}
\end{algorithm}

\begin{algorithm}[H]
  \caption{\textit{second Part}
  \begin{algorithmic}[1]
          \algrestore{testcont}  
        \EndIf
      \EndWhile\label{euclidendwhile}
      \LState \textbf{Retour} $b$\Comment{The gcd is b}
    \Fin
  \end{algorithmic}
\end{algorithm}

\end{document}

if--else 내부에서 이 알고리즘을 나누려고 하면 \algrestore{testcont}작동하지 않습니다! 좋은 생각이 있으신가요?

답변1

\algsave알고리즘을 깨는 - 방법 에 의존하는 대신 를 \algrestore통해 "페이지 나누기 알고리즘"을 사용하여 알고리즘을 깨뜨릴 수 있습니다 \vsplit. 에서 인용주제별 TeX(부분27.5\vsplit, 230페이지):

페이지 나누기 작업은 \vsplit작업을 통해 사용자가 사용할 수 있습니다.

\setbox1 = \vsplit2 to \dimen3

상자 1크기의 상단 부분을 상자에 할당합니다 . 이 물질은 실제로 상자에서 제거됩니다 .\dimen322

우리는 알고리즘을 설정합니다밖의부동 소수점 (박스 레지스터) 을 algorithm입력 한 다음 이를 (상위 10개 기준선 포함)와 나머지 (남은 것)로 분할합니다.\algbox\algboxtop\algbox

또한 "계속" 캡션을 허용하기 위해 다음을 사용할 수 있습니다 \caption*.caption) 일부 상호 참조와 함께.

여기에 이미지 설명을 입력하세요

\documentclass{article}
\usepackage{algorithm,algpseudocode,caption}

\makeatletter
\renewcommand{\ALG@name}{Algorithme}% Algorithm in French
\newcommand{\algorithmname}{\ALG@name}
\algnewcommand{\Debut}{\Statex \textbf{D\'ebut}\pushindent}
\algnewcommand{\Fin}{\Statex \textbf{Fin}\popindent}

% This is the vertical rule that is inserted
\def\therule{\makebox[\algorithmicindent][l]{\hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}%

\newtoks\therules% Contains rules
\therules={}% Start with empty token list
\def\appendto#1#2{\expandafter#1\expandafter{\the#1#2}}% Append to token list
\def\gobblefirst#1{% Remove (first) from token list
  #1\expandafter\expandafter\expandafter{\expandafter\@gobble\the#1}}%
\def\LState{\State\unskip\the\therules}% New line-state
\def\pushindent{\appendto\therules\therule}%
\def\popindent{\gobblefirst\therules}%
\def\printindent{\unskip\the\therules}%
\def\printandpush{\printindent\pushindent}%
\def\popandprint{\popindent\printindent}%

%      ***      DECLARED LOOPS      ***
% (from algpseudocode.sty)
\algdef{SE}[WHILE]{While}{EndWhile}[1]
  {\printandpush\textbf{Tant que} #1}
  {\popandprint\textbf{Fin Tant que}}%
\algdef{SE}[FOR]{For}{EndFor}[1]
  {\printandpush\algorithmicfor\ #1\ \algorithmicdo}
  {\popandprint\algorithmicend\ \algorithmicfor}%
\algdef{S}[FOR]{ForAll}[1]
  {\printindent\algorithmicforall\ #1\ \algorithmicdo}%
\algdef{SE}[LOOP]{Loop}{EndLoop}
  {\printandpush\algorithmicloop}
  {\popandprint\algorithmicend\ \algorithmicloop}%
\algdef{SE}[REPEAT]{Repeat}{Until}
  {\printandpush\algorithmicrepeat}[1]
  {\popandprint\algorithmicuntil\ #1}%
\algdef{SE}[IF]{If}{EndIf}[1]
  {\printandpush\algorithmicif\ #1\ \algorithmicthen}
  {\popandprint\algorithmicend\ \algorithmicif}%
\algdef{C}[IF]{IF}{ElsIf}[1]
  {\popandprint\pushindent\algorithmicelse\ \algorithmicif\ #1\ \algorithmicthen}%
\algdef{Ce}[ELSE]{IF}{Else}{EndIf}
  {\popandprint\pushindent\algorithmicelse}%
\algdef{SE}[PROCEDURE]{Procedure}{EndProcedure}[2]
   {\printandpush\algorithmicprocedure\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\popandprint\algorithmicend\ \algorithmicprocedure}%
\algdef{SE}[FUNCTION]{Function}{EndFunction}[2]
   {\printandpush\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\popandprint\algorithmicend\ \algorithmicfunction}%
\newcommand{\Donnees}{\State \textbf{Input}}
\makeatother

\newsavebox{\algbox}
\newsavebox{\algboxtop}
%\newsavebox{\algboxbottom}

\begin{document}


\setbox\algbox=\vbox{%
\begin{algorithmic}[1]
  \Donnees\strut: My data
  \Statex% Blank line
  \Debut
    \LState $r \gets a \bmod b$
    \While{$r \neq 0$}\Comment{We have the answer if~$r$ is~$0$}
      \LState $a \gets b$
      \LState $b \gets r$
      \LState $r \gets a \bmod b$
      \If {condition}
        \LState instruction
      \EndIf
    \EndWhile\label{euclidendwhile}
    \LState \textbf{Retour} $b$\Comment{The gcd is $b$}
  \Fin\strut
\end{algorithmic}
}
% Break algorithm into 2 parts: Top in \algoboxtop, bottom in \algbox
\global\setbox\algboxtop = \vsplit\algbox to 10\baselineskip%


\begin{algorithm}[H]
  \caption{My algorithme}\label{alg:first}
  % Capture entire algorithm inside \algbox (a \vbox)
  \usebox{\algboxtop}% Set top box
\end{algorithm}

\begin{algorithm}[H]
  % http://tex.stackexchange.com/a/33076/5764
  \caption*{\textbf{\algorithmname~\ref{alg:first}}\ My algorithme (continued)}
  \usebox{\algbox}
\end{algorithm}

\end{document}

관련 정보