1

I am trying to write an algorithm using algorithm2e. I have to use \tcc comments at different phases of the algorithm. When I am adding \tcc comment in the code, in the output, each \tcc comments are appearing line-numbered. An example code is as below and the output is also attached herewith.

Can anyone please suggest me, how to remove line numbers from \tcc comments. In the image, I highlighted those 2 \tcc lines, where I do not want any line number. Rather I want line number starts from "Initialization", that is appearing step 2 instead of step 1 in the output.

output

\begin{algorithm}[H] 
\caption{How to write algorithms}
\tcc{Start of Example 1}
\KwData{this text}  
\KwResult{how to write algorithm with \LaTeX2e }  
initialization\;  
\While{not at end of this document}{
  read current\;
  \eIf{understand}{
   go to next section\;
   current section becomes this one\;
   }{
   go back to the beginning of current section\;
  }
 }
\tcc{End of Example 1}
\end{algorithm}
1

Based on your previous question you're using the following algorithm2e options:

\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}

Remove the commentsnumbered option if you don't want the comment lines numbered...

enter image description here

\documentclass{article}

\usepackage[linesnumbered,lined,boxed]{algorithm2e}

\begin{document}

\begin{algorithm}[H] 
  \caption{How to write algorithms}
  \tcc{Start of Example 1}
  \KwData{this text}  
  \KwResult{how to write algorithm with \LaTeX2e }  
  initialization\;  
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \tcc{End of Example 1}
\end{algorithm}

\end{document}
7
  • Werner, thank you. I have one more question. In case of very long algorithm that does not fit in one page, can you please suggest me how to flow the algorithm in next page? Thank you
    – 11187162
    Dec 24 '19 at 3:50
  • @Stan: The algorithm is contained within a box, and that box doesn't split across the page. You'll have to break it manually and work on manual indentation to line up across the break point. This is because the algorithm is considered a float, and that's how floats are handled. You could consider writing your algorithm in a more compact way, using procedures. Check the algorithm2e documentation for more details.
    – Werner
    Dec 24 '19 at 3:54
  • Werner, Thanks for your feedback. Is there any way to set the starting number of line number manually. For example, can i start line number from 15 and then normal count up like 16, 17, 18 so on. Thank you
    – 11187162
    Dec 24 '19 at 4:28
  • @Stan: You can. Can you provide a minimal example that I can use as a starting point?
    – Werner
    Dec 24 '19 at 4:41
  • Hi Werner, for example, if I want to start line numbering from 21 in the above code, where "Initialization" appeared as Step 1, what I have to do ? I meant, with above code, the line numbers are appearing as 1, 2, 3 so on. But I want it will be 21, 22, 23 .. so on where 21 is the first step. Thank you
    – 11187162
    Dec 24 '19 at 5:25

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.