我正在尝试在算法中对齐多行。到目前为止,我的代码和结果如下所示:
\usepackage[german,onelanguage,linesnumbered,ruled]{algorithm2e}
\documentclass[pdftex,12pt,a4paper,twoside,ngerman]{article}
\begin{document}
\begin{algorithm}
\SetAlgoLined
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{Eingabe}\SetKwInOut{Output}{Ausgabe}
\SetKwBlock{Loop}{Loop}{Ende}
\Loop{
$h_G \leftarrow zusammenfassen(G_R)$\;
$P_{Obj} \leftarrow f_{Hinzu}(h_G)$\;
$ok \leftarrow stichprobe(P_{Obj})$\;
}
\end{algorithm}
\end{document}
但是,当我使用如下对齐环境时:
\begin{align*}
h_G &\leftarrow zusammenfassen(G_R) \\
P_{Obj} &\leftarrow f_{Hinzu}(h_G) \\
ok &\leftarrow stichprobe(P_{Obj}) \\
\end{align*}\;
我得到了以下非常不合适的结果:
\leftarrow
有没有办法在不影响算法布局的情况下对齐这些线?
答案1
如果您想要每个作业的行号,您需要猜测最大对象的宽度。
\documentclass[pdftex,12pt,a4paper,twoside,ngerman]{article}
\usepackage[german,onelanguage,linesnumbered,ruled]{algorithm2e}
\newcommand{\ali}[2]{\makebox[#1][r]{#2}}
\begin{document}
\begin{algorithm}
\SetAlgoLined
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{Eingabe}\SetKwInOut{Output}{Ausgabe}
\SetKwBlock{Loop}{Loop}{Ende}
\Loop{
\ali{2em}{$h_G$} ${}\leftarrow \mathit{zusammenfassen}(G_R)$\;
\ali{2em}{$P_{\mathrm{Obj}}$} ${}\leftarrow f_{\mathrm{Hinzu}}(h_G)$\;
\ali{2em}{$\mathit{ok}$} ${}\leftarrow \mathit{stichprobe}(P_{\mathrm{Obj}})$\;
}
\end{algorithm}
\end{document}
这可以自动化吗?当然可以。
\documentclass[pdftex,12pt,a4paper,twoside,ngerman]{article}
\usepackage[german,onelanguage,linesnumbered,ruled]{algorithm2e}
\ExplSyntaxOn
\NewDocumentEnvironment{alignassign}{b}
{
\tilman_alignassign:n { #1 }
}{}
\seq_new:N \l__tilman_alignassign_body_seq
\dim_new:N \l__tilman_alignassign_wd_dim
\cs_new_protected:Nn \tilman_alignassign:n
{
\seq_set_split:Nnn \l__tilman_alignassign_body_seq { \\ } { #1 }
\dim_zero:N \l__tilman_alignassign_wd_dim
\seq_map_inline:Nn \l__tilman_alignassign_body_seq
{
\__tilman_alignassign_measure:nn ##1
}
\seq_map_inline:Nn \l__tilman_alignassign_body_seq
{
\__tilman_alignassign_print:nn ##1
}
}
\cs_new_protected:Nn \__tilman_alignassign_measure:nn
{
\hbox_set:Nn \l_tmpa_box { $#1$ }
\dim_set:Nn \l__tilman_alignassign_wd_dim
{
\dim_max:nn { \l__tilman_alignassign_wd_dim } { \box_wd:N \l_tmpa_box }
}
}
\cs_new_protected:Nn \__tilman_alignassign_print:nn
{
\makebox[\l__tilman_alignassign_wd_dim][r]{ $#1$ }
${}\leftarrow #2$\;
}
\ExplSyntaxOff
\begin{document}
\begin{algorithm}
\SetAlgoLined
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{Eingabe}\SetKwInOut{Output}{Ausgabe}
\SetKwBlock{Loop}{Loop}{Ende}
\Loop{
\begin{alignassign}
{h_G}{\mathit{zusammenfassen}(G_R)}\\
{P_{\mathrm{Obj}}}{f_{\mathrm{Hinzu}}(h_G)}\\
{\mathit{ok}}{\mathit{stichprobe}(P_{\mathrm{Obj}})\label{third}}
\end{alignassign}
}
\end{algorithm}
The assignment in line~\ref{third} is important.
\end{document}
\usepackage{xparse}
如果您没有 2020-10-01 发布的 LaTeX,则可能需要添加。