我想在 Latex 中编写伪代码算法,如附图所示。我已经编写了代码来实现与上述算法相同的效果,但它对我来说不起作用。这个代码正确吗?
\IncMargin{1em}
\begin{algorithm}
\SetKwInOut{Part 1}{input}
\SetKwInOut{Part 2}{output}
\Part 1{}
\BlankLine
\emph{abcdefgh $D$ ijklmnop}\;
\emph{ abcdefgh $A$}\;
\For{$k\leftarrow 1$ \KwTo $M$}{
\For {each $m$ $ ∈ $ {1, 2, …, J}{
\emph{ abcdefgh $D$}\;
\emph{abcdef }\;
}
\Part 2{}
\emph{abcdefgh $A$ ijklmnop}\;
\emph{ abcdefgh $A$}\;
\For {each $m$ $ ∈ $ {1, 2, …, J}{
\emph{ abcdefgh $D$}\;
\emph{abcdef }\;
\emph{abcdef }\;
}
}
\For{$k\leftarrow 1$ \KwTo $M$}{
\emph{ abcdefghklmop}\;
\emph{ abcdefghklmop}\;
\emph{ abcdefghklmop}\;
}
\caption { Algorithm Sample}
\end{algorithm}
\DecMargin{1em}
答案1
以下更符合常规语法algorithm2e
:
\documentclass{article}
\usepackage{amsmath}
\usepackage[ruled,vlined]{algorithm2e}
\DontPrintSemicolon
\newcommand{\To}{\mbox{\upshape\bfseries to}}
\begin{document}
\begin{algorithm}
\caption{Algorithm sample}
\textbf{Part 1}\;
\nl abcdefgh $\{ D_i \}_{i = 1}^M$ ijklmnop\;
\nl abcdefgh $A_m$\;
\nl \For{$K = 1$ \To{} $M$}{%
\ForEach{$m \in \{1, 2, \dots, J\}$}{
abcdefgh $\{ D \}_m^t$\;
abcdefgh\;
}
\textbf{Part 2}\;
abcdefgh $\{ G_i \}_{i = 1}^K$\;
abcdefgh $D_m$\;
\ForEach{$m \in \{1, 2, \dots, J\}$}{
abcdefgh $\{ G \}_m^y$\;
abcdefgh\;
abcdefgh\;
}
}
\nl \For{$K = 1$ \To{} $M$}{
abcdefghijklmno\;
abcdefghijklmno\;
abcdefghijklmno\;
}
\end{algorithm}
\end{document}