我有以下代码:
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
x $\longleftarrow$ 0\tcc*[r]{initialize x}
\uIf(\tcc*[r]{if comment}){condition 1}{
x $\longleftarrow$ 1\tcc*[r]{set x as 1}
}
\uElseIf(\tcc*[r]{else comment}){condition 2}{
x $\longleftarrow$ 2\;
}
\Else{
x $\longleftarrow$ 3\tcc*[r]{set x as 3}
}
\caption{First algorithm}
\end{algorithm}
\end{document}
答案1
当注释没有结束行时,您必须使用它f
作为可选参数\tcc*
而不是。r
梅威瑟:
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
x $\longleftarrow$ 0\tcc*[r]{initialize x}
\uIf(\tcc*[f]{if comment}){condition 1}{
x $\longleftarrow$ 1\tcc*[r]{set x as 1}
}
\uElseIf(\tcc*[f]{else comment}){condition 2}{
x $\longleftarrow$ 2\;
}
\Else{
x $\longleftarrow$ 3\tcc*[r]{set x as 3}
}
\caption{First algorithm}
\end{algorithm}
\end{document}
输出:
来自algorithm2e
文档:
\tcc*[r]
{右对齐的侧边注释,结束行(默认)}:注释“ la” C
尽管
\tcc*[f]
{右对齐注释,无终点线; 例如,与 ”if-then-else” 宏一起使用}: 注释 “ la” C