![如何修复 Latex 中具有多个程序的伪代码算法中的问题?](https://linux22.com/image/465948/%E5%A6%82%E4%BD%95%E4%BF%AE%E5%A4%8D%20Latex%20%E4%B8%AD%E5%85%B7%E6%9C%89%E5%A4%9A%E4%B8%AA%E7%A8%8B%E5%BA%8F%E7%9A%84%E4%BC%AA%E4%BB%A3%E7%A0%81%E7%AE%97%E6%B3%95%E4%B8%AD%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%9F.png)
我正在格式化包含多个程序的伪代码算法,但格式存在一些问题,例如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
你应该把这些放在环境\LineComment
中algorithmic
。
\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
。