我希望复制我在其他地方看到的效果(下图),其中文本不一定位于行首,而是与其他地方的文本对齐。
我不知道哪种环境合适,但我认为各种算法环境中的某一种符合要求(wrt 结构)。我尝试过使用看似流行的软件包algorithm2e
,但找不到方法。
\documentclass[11pt]{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
\caption{Algorithm}
Let \quad $ X $ be the set of $ n $ training patterns $ x_{1}, x_{2}, \dots x_{n} $\;
\qquad $ W $ be a $ p \times q $ grid of units $ w_{ij} $ where $ i $ and $ j $ are their coordinates on that grid\;
\qquad $ \alpha $ be the learning rate, assuming values in $ [0,1] $, initialized to a given initial learning rate\;
\qquad $ r $ be the radius of the neighborhood function $ h(w_{ij},w_{mn},r $), initialized to a given initial radius\;
\Repeat{$ \alpha $ reaches 0}{
\For{$ k=1 $ to $ n $}{
For all $ w_{ij} \in W $, calculate $ d_{ij} = || x_{k} - w_{ij} || $\;
Select the unit that minimizes $d_{ij}$ as the winner $ w_{winner} $\;
Update each unit $ w_{ij} \in W: w_{ij} = w_{ij} + \alpha h(w_{winner}, w_{ij}, r) || x_{k} - w_{ij} || $\;
Decrease the value of $ \alpha $ and $ r $
}
}
\end{algorithm}
\end{document}
产生...
这说明有两个问题:1) 对齐不存在,2) 换行时没有缩进。我该怎么做?
答案1
这里tabularx
提出了实现 2 个目标的包。三列被分配了不同的属性{l>{$}c<{$}X}
。此外,.xx\textwidth
可以通过十进制数 .xx 进行更改。
代码
\documentclass[11pt]{article}
\usepackage{algorithm2e}
\usepackage{tabularx}
\begin{document}
\begin{algorithm}
\caption{Algorithm}
\begin{tabularx}{\textwidth}{l>{$}c<{$}X}
Let & X & be the set of $ n $ training patterns $ x_{1}, x_{2}, \dots x_{n} $\\
& W &be a $ p \times q $ grid of units $ w_{ij} $ where $ i $ and $ j $ are their coordinates on that grid\\
& \alpha & be the learning rate, assuming values in $ [0,1] $, initialized to a given initial learning rate\\
& r & be the radius of the neighborhood function $ h(w_{ij},w_{mn},r )$, initialized to a given initial radius
\end{tabularx}
\Repeat{$ \alpha $ reaches 0}{
\For{$ k=1 $ to $ n $}{
For all $ w_{ij} \in W $, calculate $ d_{ij} = || x_{k} - w_{ij} || $\;
Select the unit that minimizes $d_{ij}$ as the winner $ w_{winner} $\;
Update each unit $ w_{ij} \in W: w_{ij} = w_{ij} + \alpha h(w_{winner}, w_{ij}, r) || x_{k} - w_{ij} || $\;
Decrease the value of $ \alpha $ and $ r $
}
}
\end{algorithm}
\end{document}
答案2
感谢@Jesse,我找到了一种获得相当不错的表示的方法,其中对算法的一部分进行了行号标注。它看起来像这样:
表格被包裹在input
关键字内(使用\SetKwInput
--- 重新定义为“Let” 请参阅 v5.0 手册第 34 页第 11 节)。 algorithm2e 中的输入、输出和数据部分没有编号。它确实将单词“Let”设置为垂直居中显示在表格旁边,但我不能花一整天的时间来修复这样的小问题,不是吗?
代码:
\LinesNumbered
\SetKwInput{KwLet}{Let}
\begin{algorithm}[htbp]
\KwLet{ \begin{tabularx}{0.8\textwidth}{>{$}c<{$} X}
X & be the set of $ n $ training vectors $ x_{1}, x_{2}, \dots x_{n} $\\
W &be a $ p \times q $ grid of units $ w_{ij} $ where $ i $ and $ j $ are their coordinates on that grid\\
\alpha & be the learning rate, assuming values in $ [0,1] $, initialised to a given initial learning rate\\
r & be the radius of the neighbourhood function $ h(w_{ij},w_{mn},r )$, initialised to a given initial radius
\end{tabularx}}
\BlankLine
\Repeat{$ \alpha $ reaches 0}{
\For{ k=1 to n }{
For all $ w_{ij} \in W $, calculate $ d_{ij} = || x_{k} - w_{ij} || $\;
Select the unit that minimizes $d_{ij}$ as the winner $ w_{winner} $\;
Update each unit $ w_{ij} \in W: \allowbreak w_{ij} = w_{ij} + \alpha h(w_{winner}, w_{ij}, r) || x_{k} - w_{ij} || $\;
Decrease the value of $ \alpha $ and $ r $
}
}
\caption{Algorithm}
\end{algorithm}