如何修复 Latex 中具有多个程序的伪代码算法中的问题?

如何修复 Latex 中具有多个程序的伪代码算法中的问题?

我正在格式化包含多个程序的伪代码算法,但格式存在一些问题,例如endProcedure=0乳胶输出。我该如何解决这个问题?谢谢

我当前的脚本

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}

\algdef{SE}[PROCEDURE]{Procedure}{EndProcedure}%
   [2]{\algorithmicprocedure\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\algorithmicend\ \algorithmicprocedure}%
\algdef{SE}[FUNCTION]{Function}{EndFunction}%
   [2]{\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\algorithmicend\ \algorithmicfunction}%

\begin{document}
\begin{algorithm}
\caption{some text for caption}
\label{alg:mergesort}

\LineComment{ s,a: text, text, or text}
\LineComment{ $text: text, or text}
\LineComment{ text, text, or text}
\begin{algorithmic}[1]
\Procedure{sort}{s,a}
\State $\mathbf{z_a} \gets \text{srt}_s(a)$
\State $\mathbf{z_a} \gets \text{srt}_a(a)$
\State \Return $\mathbf{z_a}, \mathbf{z_a}$
\EndProcedure
\Procedure{mergesort}{s,a,t}
\State $\mathbf{z_t} \gets \text{srt}_t(t)$
\State $\mathbf{z_a, z_a} \gets \text{sort}(s,a)$
\State $\mathbf{z_a} \gets \mathbf{z_a}+\mathbf{z_t}$ \Comment{text, text, or text}
\State $\mathbf{z_a} \gets \mathbf{z_a}+\mathbf{z_t}$
\State $\mathbf{z_{\hat{a}}} \gets \text{mergesort}([\mathbf{z_a}, \mathbf{z_a}])$
\State $\mathbf{\hat{a}} \gets \text{pred}_a(\mathbf{z_{\hat{a}}})$
\State \Return $\mathbf{\hat{a}}$
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

答案1

你应该把这些放在环境\LineCommentalgorithmic

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}

\algdef{SE}[PROCEDURE]{Procedure}{EndProcedure}%
   [2]{\algorithmicprocedure\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\algorithmicend\ \algorithmicprocedure}%
\algdef{SE}[FUNCTION]{Function}{EndFunction}%
   [2]{\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\algorithmicend\ \algorithmicfunction}%

\begin{document}
\begin{algorithm}
\caption{some text for caption}
\label{alg:mergesort}

\begin{algorithmic}[1]
\LineComment{ s,a: text, text, or text}
\LineComment{ $text: text, or text$}
\LineComment{ text, text, or text}
\Procedure{sort}{s,a}
\State $\mathbf{z_a} \gets \text{srt}_s(a)$
\State $\mathbf{z_a} \gets \text{srt}_a(a)$
\State \Return $\mathbf{z_a}, \mathbf{z_a}$
\EndProcedure
\Procedure{mergesort}{s,a,t}
\State $\mathbf{z_t} \gets \text{srt}_t(t)$
\State $\mathbf{z_a, z_a} \gets \text{sort}(s,a)$
\State $\mathbf{z_a} \gets \mathbf{z_a}+\mathbf{z_t}$ \Comment{text, text, or text}
\State $\mathbf{z_a} \gets \mathbf{z_a}+\mathbf{z_t}$
\State $\mathbf{z_{\hat{a}}} \gets \text{mergesort}([\mathbf{z_a}, \mathbf{z_a}])$
\State $\mathbf{\hat{a}} \gets \text{pred}_a(\mathbf{z_{\hat{a}}})$
\State \Return $\mathbf{\hat{a}}$
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

我建议使用algpseudocodex,它更强大,例如\LComment

相关内容