删除 \如果且只有算法中有“do”

删除 \如果且只有算法中有“do”

我有以下算法代码:

\documentclass{article}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath}
\begin{document}
\begin{algorithm}
    \caption{Pseudo-cod}
    \begin{algorithmic}
        \Statex \textbf{Step 1:} Solve Model.
        \Statex \textbf{Step 2:} Let $t\leftarrow 0$
        \For{$i \in \mathcal{I}$}
        \State Let $t \leftarrow t+1$
        \If{$\theta_{i} <-  r^{tot}$}
        \State $r^{tot}\times 0.9$
        \EndIf
        \EndFor
        \Statex \textbf{Step 3:}
        \If{$\theta_{i} \geq  r^{tot}$}
        \State stop and go to \textbf{Step 4}
        \EndIf
        \Statex \textbf{Step 4:} Solve the updated Model as IP.
    \end{algorithmic}
\end{algorithm}
\end{document}

但是,我现在想删除该If条件,只保留do该行。我已经尝试过了,但没有得到想要的结果。

\documentclass{article}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath}
\begin{document}
\begin{algorithm}
    \caption{Pseudo-cod}
    \begin{algorithmic}
        \Statex \textbf{Step 1:} Solve Model.
        \Statex \textbf{Step 2:} Let $t\leftarrow 0$
        \Statex \textbf{do}
        \qquad \State Let $t \leftarrow t+1$
        \Statex \qquad \If{$\theta_{i} <-  r^{tot}$}
        \Statex \qquad \qquad $r^{tot}\times 0.9$
        \Statex \qquad \EndIf
        \Statex \textbf{Step 3:}
        \If{$\theta_{i} \geq  r^{tot}$}
        \State stop and go to \textbf{Step 4}
        \EndIf
        \Statex \textbf{Step 4:} Solve the updated Model as IP.
    \end{algorithmic}
\end{algorithm}
\end{document}

它看起来应该是这样的: 在此处输入图片描述

答案1

快速破解:定义一个新块并将格式更改为你喜欢的格式

\documentclass{article}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath}

\algblockdefx[NAME]{Do}{EndDo}{\textbf{Do}}{\vspace*{-\baselineskip}}

\begin{document}
\begin{algorithm}
    \caption{Pseudo-cod}
    \begin{algorithmic}
        \Statex \textbf{Step 1:} Solve Model.
        \Statex \textbf{Step 2:} Let $t\leftarrow 0$
        \Do
          \State Let $t \leftarrow t+1$ 
          \If{$\theta_{i} <-  r^{tot}$}
          \State $r^{tot}\times 0.9$
          \EndIf
        \EndDo
        \Statex \textbf{Step 3:}
        \If{$\theta_{i} \geq  r^{tot}$}
        \State stop and go to \textbf{Step 4}
        \EndIf
        \Statex \textbf{Step 4:} Solve the updated Model as IP.
    \end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

相关内容