如何删除算法中的 end-if

如何删除算法中的 end-if

我正在用 latex 编写算法,但是算法中有很多end-ifend-while,这使得它不专业且难看。有没有办法从 latex 输出中删除它们?

\IF{condition}  
      \IF{another condition}
          \STATE do something
          \STATE do something else
      \ELSE
          \STATE do it
      \ENDIF
\ELSE
      \IF{condition}
          \STATE do something
          \STATE do another thing
      \ELSE
          \IF{condition}
              \STATE function
          \ENDIF
      \ENDIF
\ENDIF

答案1

我建议你换成更通用的algorithmicx包;使用noend选项,你可以实现你想要的:

\documentclass{article}
\usepackage[noend]{algcompatible}

\begin{document}

\begin{algorithmic}
\IF{condition}  
      \IF{another condition}
          \STATE do something
          \STATE do something else
      \ELSE
          \STATE do it
      \ENDIF
\ELSE
      \IF{condition}
          \STATE do something
          \STATE do another thing
      \ELSE
          \IF{condition}
              \STATE function
          \ENDIF
      \ENDIF
\ENDIF
\end{algorithmic}

\end{document}

在此处输入图片描述

我使用了algcompatible包的变体来完全兼容的algorithmic语法,但我还建议您更改为algpseudocode(语法非常相似);这是您的代码algpseudocode(唯一的区别是使用的大小写类型):

\documentclass{article}
\usepackage[noend]{algpseudocode}

\begin{document}

\begin{algorithmic}
\If{condition}  
      \If{another condition}
          \State do something
          \State do something else
      \Else
          \State do it
      \EndIf
\Else
      \If{condition}
          \State do something
          \State do another thing
      \Else
          \If{condition}
              \State function
          \EndIf
      \EndIf
\EndIf
\end{algorithmic}

\end{document}

相关内容