![doWhile 循环缩进问题](https://linux22.com/image/456003/doWhile%20%E5%BE%AA%E7%8E%AF%E7%BC%A9%E8%BF%9B%E9%97%AE%E9%A2%98.png)
我一直在尝试使用 do-while 循环为 [1] 中所示的流程图编写多个循环语句。但是,管理缩进却很困难。有人能帮忙解决这个问题吗?
\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
也)看不到:
是什么
$g$
。如果是一些抽象语句,则应将其写为\State $g$
以便正确缩进。这同样适用于$i$
和$r s t$
。为什么你希望下面的行
$g$
不是直接缩进$g$
,而是更靠右。这只能解释为,当$g$
表示某种形式的块语句(例如while
)时,其主体延伸到其下面的行。
获得的结果(在添加缺失的\State
宏和一些\medskip
用于添加垂直空间的宏之后)反映了程序的结构algorithmicx
:
另一方面,如果您确实想在块头后面引入任意缩进,您可以为此目的定义一种新类型的块(我将其命名为\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
下面是我在 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}