4

I have the following code:

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

which produces this pdf: enter image description here Where the two marked semicolons and the marked blank lines are unwanted. Can you help me what to change in the code please?

1 Answer 1

6

You have to use f as optional argument for \tcc* instead of r when the comment doesn't end the line.

MWE:

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

Output:

enter image description here

From the algorithm2e documentation:

\tcc*[r]{right justified side comment, ends the line (default)}: comment “ la” C

while

\tcc*[f]{right justified comment, without end line; useful with ”if-then-else” macros for example}: comment “ la” C

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .