doWhile 루프 들여쓰기 문제

doWhile 루프 들여쓰기 문제

저는 [1]에 표시된 흐름도에 대해 do-while 루프를 사용하여 여러 루프 문을 작성하려고 했습니다. 그러나 들여쓰기를 관리하는 것은 어렵습니다. 누구든지 이 문제에 대해 도움을 줄 수 있나요?

\documentclass{article}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%

\begin{document}
\begin{algorithmic}
  \Do
    \State a,b,c,d,e,m,n
  \doWhile{$!f$} % <--- use \doWhile for the "while" at the end
  
  $g$
  
    \Do
    \State abc
    \doWhile{$!h$}
    
    $i$
    \Do
    \State Pv
    \doWhile{$!j$}
    
    $r s t $
  %  \doWhile{$!$}
  %   \State l
\end{algorithmic}
\end{document}

문제는 들여쓰기입니다.

답변1

프로그램 텍스트의 공백은 일반적으로 프로그래밍 언어에서 무시되지만 일반적인 관행에 따르면 들여쓰기는 프로그램을 더 읽기 쉽게 만드는 데 사용됩니다. 이러한 목적을 달성하려면 들여쓰기는 일반적으로 프로그램의 구조를 반영하는 일관된 방식으로 일부 스타일을 따라야 합니다.

당신이 가지고 있는 코드에서는 프로그램의 구조를 따라갈 수 없습니다. 불행하게도 algorithmicx패키지(참조문서) 또한 구조를 따르지 못하고 예상과 다른 방식으로 코드를 들여쓰기합니다.

특히, 나는(그리고 algorithmicx또한) 다음을 볼 수 없습니다:

  1. 무엇 $g$인가요. 추상적인 문장이라면 들여쓰기가 \State $g$적절하게 되도록 작성해야 합니다. $i$및 에도 동일하게 적용됩니다 $r s t$.

  2. 왜 아래 줄이 바로 아래 가 아니라 오른쪽으로 $g$들여쓰기되기를 기대하는가 ? 이는 본문이 그 아래 줄까지 확장되는 블록 명령문(예: a )의 형태를 나타내는 $g$경우에만 설명될 수 있습니다 .$g$while

\State(누락된 매크로를 추가하고 수직 공간을 추가한 후) 얻은 결과는 \medskip프로그램의 구조를 algorithmicx인식하는 대로 반영합니다.

결과 1

반면에 실제로 블록 헤더 뒤에 임의의 들여쓰기를 도입하려는 경우 이 목적을 위해 새로운 유형의 블록을 정의할 수 있습니다(저는 이름을 로 지정했습니다 \Arbitrary{header} ... \endArbitrary).

\documentclass{article}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%
\algblockdefx[ARBITRARY]{Arbitrary}{endArbitrary}[1]{#1}

\begin{document}
\begin{algorithmic}
  \Do
    \State a,b,c,d,e,m,n
  \doWhile{$!f$} % <--- use \doWhile for the "while" at the end
  \medskip
  \Arbitrary{$g$}
    \medskip
    \Do
    \State abc
    \doWhile{$!h$}
    \medskip
    \State $i$
    \Do
    \State Pv
    \doWhile{$!j$}
    \medskip
    \State $r s t $
  \endArbitrary
\end{algorithmic}
\end{document}

그 결과는 다음과 같습니다.

결과 2

답변2

다음은 내가 LaTex에 작성한 중첩된 Do-While 기반 의사코드입니다. 누군가가 이를 확인할 수 있고, 나중에 같은 문제가 발생할 수 있는 다른 사람들을 위해 사용될 수 있습니다.

\documentclass[11pt]{article}
\usepackage[fleqn]{amsmath}
\usepackage[ruled,vlined]{algorithm2e}
\PassOptionsToPackage{noend}{algpseudocode}
\usepackage{algpseudocode}

    %%%%%%%%%%%%%%%%%%
    %% DoWhile algorithm macro definition 
    \makeatletter
    \newcommand*{\algrule}[1][\algorithmicindent]{\makebox[#1][l]{\hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}
    
    \newcount\ALG@printindent@tempcnta
    \def\ALG@printindent{%
        \ifnum \theALG@nested>0% is there anything to print
        \ifx\ALG@text\ALG@x@notext% is this an end group without any text?
        % do nothing
        \addvspace{-3pt}% FUDGE for cases where no text is shown, to make the rules line up
        \else
        \unskip
        % draw a rule for each indent level
        \ALG@printindent@tempcnta=1
        \loop
        \algrule[\csname ALG@ind@\the\ALG@printindent@tempcnta\endcsname]%
        \advance \ALG@printindent@tempcnta 1
        \ifnum \ALG@printindent@tempcnta<\numexpr\theALG@nested+1\relax% can't do <=, so add one to RHS and use < instead
        \repeat
        \fi
        \fi
    }
\usepackage{etoolbox}
% the following line injects our new indent handling code in place of the default spacing
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\makeatother
% end vertical rule patch for algorithmicx
\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%

\begin{algorithm}%\captionsetup{labelfont={sc,bf}, labelsep=newline}
    \caption{Workspace boundary determination}
    \label{alg:PoEG}    
    \begin{algorithmic}
        \State Input $z_0,~z_f,~n,~m$
        \State \quad \quad ~ $\Delta_z = \dfrac{z_0-z_f}{n}, \textit{tol}$
        \State \quad \quad ~ $\varepsilon_f= 2\pi~, \Delta\varepsilon = \dfrac{\varepsilon_f}{m}$
        \State \quad \quad ~ $k_{\textup{max}}, \Delta\alpha_0, i = j=1$
        \medskip
        \State Output $\boldsymbol{\theta}, \boldsymbol{\psi}, \boldsymbol{z} $
        \medskip
        \State initialize ~$z \gets z_0,~ \psi=\theta =\gets \emptyset$
        \Do
            \Do 
                \Do
                    \Do 
                        \State $\Delta\psi = \psi + \Delta\alpha c\varepsilon$
                        \State $\Delta\theta = \theta + \Delta\alpha s\varepsilon $ 
                        \State $\psi = \psi +\Delta\varepsilon$
                        \State $\theta = \psi + \Delta\varepsilon$ 
                        \State Compute $\boldsymbol{J}_{dh}$ 
                        \State $k = cond(\boldsymbol{J}_{dh})$
        
                    \doWhile{$k<k_{max}$}
        
                    \State $\psi = \psi -\Delta\varepsilon$
                    \State $\theta = \psi - \Delta\varepsilon$ 
        
                    \State $\Delta\alpha = \Delta\alpha/2$
        
                \doWhile{$\Delta\alpha > tol $}
                \State $\psi(i,j) = \psi $
                \State $\theta(i,j) = \theta $
        
                \State $ j =j+1$
                \State $\Delta\alpha = \Delta\alpha_0$
                \State $\varepsilon = \varepsilon + \Delta\varepsilon$
            \doWhile{$\varepsilon < 2\pi$}
        
            \State $ \theta =0$
            \State $\psi =0$
            \State $\varepsilon =0$
            \State $\Delta\alpha=\Delta\alpha_0$
            \State $i=i+1$
            \State $z_i=z+\Delta_z$

        \doWhile{$z<z_f$} \\ % <--- use \doWhile for the "while" at the end
        plot$(\boldsymbol{\psi},\boldsymbol{\theta},\boldsymbol{z})$\\ \medskip Note: $s$ and $c$ stands for sine and cosine, respectively.
        %\endArbitrary
    \end{algorithmic}
\end{algorithm}

\end{document} 

관련 정보