13

I want to add a right aligned comment in LaTeX after \For command, but it adds an undesired blank line. Please help me.

CODE:

\For(\tcp*[r]{Incremental calculation of edges}){$i \gets 1$ \textbf{\upshape{to}} $n$}{
\If {$i=1$}
{
$currentCenter \gets MEAN(x_{R'[2]},\ldots,x_{R'[k_{min}+1]})$\;
$currentSSE \gets SSE(x_{R'[2]},\ldots,x_{R'[k_{min}+1]})$\;
}}

SNAPSHOT:

enter image description here

1
  • please provide a minimal compilable example, starting with \documentclass and ending with \end{document} there is not enough context in your question to give a definitive answer. please also identify what version of algorithm2e is being used -- a recent instance of a very similar situation required code changes because an older version of the package was being used (the current version is 5.0 2013/01/06); it was possible to determine the needed changes from the package documentation. Sep 20, 2013 at 14:43

1 Answer 1

15

Use \tcp*[f] rather than \tcp*[r]. According to the algorithm2e documentation (section 10.3 comments, p 31):

\tcp*[r] right justified side comment, ends the line (default)

\tcp*[f] right justified comment, without end line; useful with "if-then-else" macros for example

enter image description here

\documentclass{article}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
\newcommand{\variable}{\textup}
\newcommand{\func}{\textsf}
\begin{document}
\begin{algorithm}
\For(\tcp*[r]{Incremental calculation of edges}){$i \gets 1$ \textbf{\upshape{to}} $n$}{
  \If {$i=1$}
  {
    $currentCenter \gets MEAN(x_{R'[2]},\ldots,x_{R'[k_{min}+1]})$\;
    $currentSSE \gets SSE(x_{R'[2]},\ldots,x_{R'[k_{min}+1]})$\;
  }}
\For(\tcp*[f]{Incremental calculation of edges}){$i \gets 1$ \textbf{\upshape{to}} $n$}{
  \If {$i=1$}
  {
    $\variable{currentCenter} \gets \func{MEAN}(x_{R'[2]},\ldots,x_{R'[k_{\min}+1]})$\;
    $\variable{currentSSE} \gets \func{SSE}(x_{R'[2]},\ldots,x_{R'[k_{\min}+1]})$\;
  }}
\end{algorithm}
\end{document}

I've added some other formatting as well, since it seems needed.

5
  • Thank you really. Do publisher really use such complex formatting when publishing a real article or book, or they use a specialized tool? That's really a hard work!
    – remo
    Sep 20, 2013 at 15:22
  • @remo: That's what they use, or perhaps listings, which can auto-format the output. However, I do it that way when working with pseudo-code, and the use of macros for formatting is to promote consistency.
    – Werner
    Sep 20, 2013 at 15:25
  • Is it a limitation that the [f] option doesn't exist for \tcp (not right justified)? There is unfortunately no way to apply your solution for \tcp instead of \tcp*
    – jmon12
    Apr 12, 2021 at 10:25
  • @jmon12: Use \tcp*[h]. See section 10.3 Comments in the algorithm2e documentation.
    – Werner
    Apr 12, 2021 at 16:15
  • Ah yes OK thanks! I've been confused with the role of * in \tcp* since I thought it was the "right justified" version of \tcp. So the logic is that \tcp* can take options (with default [r]), and \tcp cannot.
    – jmon12
    Apr 13, 2021 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.