Algorithm2e 中不需要的空行和分號

Algorithm2e 中不需要的空行和分號

我有以下程式碼:

\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}

產生這個pdf: 在此輸入影像描述 其中兩個標記的分號和標記的空白行是不需要的。您能幫我更改程式碼中的哪些內容嗎?

答案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

相關內容