
Eu tenho o seguinte código:
\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}
que produz este pdf:
Onde os dois pontos e vírgulas marcados e as linhas em branco marcadas são indesejados. Você pode me ajudar o que mudar no código, por favor?
Responder1
Você deve usar f
como argumento opcional \tcc*
em vez de r
quando o comentário não termina a linha.
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}
Saída:
Da algorithm2e
documentação:
\tcc*[r]
{comentário justificado à direita,termina a linha(padrão)}: comentário “la” C
enquanto
\tcc*[f]
{comentário justificado à direita,sem linha final; útil com macros “if-then-else”, por exemplo}: comentário “la” C