删除行末的分号(algorithm2e)

删除行末的分号(algorithm2e)

使用 algorithm2e 包时,每行必须以 \; 结尾。在编译后的文档中,行末还会出现一个分号。如果将行写在宏内,则可以避免这种情况。经典示例是:

\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\; 
\While{not at end of this document}{read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

我想知道是否可以从编译后的文档中删除分号。是否有一些包选项或简单的宏可以做到这一点?谢谢!

答案1

文档中指出,每行都应以\;indeed 结尾。话虽如此,您可以使用\DontPrintSemicolon删除;输出中的 ,但似乎使用\\也可以,而不会产生太大差异。我猜这与 中隐藏的一些参数有关\;

\documentclass[]{article}
\usepackage{algorithm2e}

\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\; 
\While{not at end of this document}{read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

\begin{algorithm}[H]
\SetAlgoLined
\DontPrintSemicolon
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\; 
\While{not at end of this document}{read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\\ 
\While{not at end of this document}{read current\\
\eIf{understand}{
go to next section\\
current section becomes this one\\
}{
go back to the beginning of current section
}
}
\caption{How to write algorithms}
\end{algorithm}

\end{document}

相关内容