首页 > 解决方案 > 如何在 Latex 中编写 do while 循环?

问题描述

我已经编写了代码,但是行号不正确,代码如下所示:

\begin{algorithm}[H]
\caption{Algorithm}
\begin{algorithmic}[1]
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\REQUIRE $Graph\ G(V, E)$

\STATE $\textbf{\textit{function}}\ $
  \Do
    \State Something
  \doWhile
\end{algorithmic}
\label{algo1}
\end{algorithm}

上述代码的输出如下所示:

在此处输入图像描述

我怎样才能得到正确的编号?或者有没有更简单的方法在乳胶中编写 do while 循环?

标签: latexdo-whilepseudocode

解决方案


试试这个algorithm2e包。;)

预习

\documentclass[a4paper, 11pt]{article}
\usepackage[norelsize, linesnumbered, ruled, lined, boxed, commentsnumbered]{algorithm2e}

\begin{document}
\begin{algorithm}[H]
 \SetAlgoLined
 \LinesNumbered
 \SetKwInOut{Input}{Input}
 \Input{$Graph\ G(V, E)$}
 \SetKwProg{Function}{function}{}{end}
 \SetKwRepeat{Do}{do}{while}
 \Function{function(param: int) : int}{
     \Do{done = false}{ something }
 }
 \caption{Algorithm}
\end{algorithm}
\end{document}

推荐阅读