2

when closing the tcolorbox and adjustbox inside an algorithm, the lines of codes corresponding to their commands are displayed numbered. How can I prevent the 1 beside the caption (see the below figure) from being displayed in the following code?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[linesnumbered]{algorithm2e}
\usepackage{ float, tcolorbox, adjustbox}
\begin{document}

\begin{algorithm}[htp!]
\begin{adjustbox}{width=0.9\textwidth} \begin{tcolorbox}[left=20pt] 
\setcounter{AlgoLine}{0}
\KwData{X: some description o fthe data}
\KwData{Y: other data to describe}
\vspace*{5pt}
\hrule
\vspace*{5pt}
here is the first lone of the code \;
\For{ condition }{
    wow for loop!! \;
    \For {condition}{
        \While{condition}{
        that's intriguing;
        \While{indx in indxs or empty}{indx  $\leftarrow$ rblabla\;}}
    blabla\;
    \For{condition}{
      blabla\;
      blabla\;}}}
      %here I should put something to stop the numbering
      \end{tcolorbox}  \end{adjustbox} 
\caption{my caption}\label{alg:this}
\end{algorithm}
\end{document}

code output

1

The following might be sufficient to remove the orphaned line numbering:

enter image description here

\documentclass{article}

\usepackage[linesnumbered]{algorithm2e}
\usepackage{tcolorbox, adjustbox}

\begin{document}

\begin{algorithm}
  \SetNlSty{@gobble}{}{}% Remove line numbering
  \begin{adjustbox}{width=0.9\textwidth}
    \SetNlSty{textbf}{}{}% Default line numbering style
    \setcounter{AlgoLine}{0}% Restore default line numbering start
    \begin{tcolorbox}[left=20pt]
      \KwData{X: some description o fthe data}
      \KwData{Y: other data to describe}
      \vspace*{5pt}
      \hrule
      \vspace*{5pt}
      here is the first lone of the code \;
      \For{ condition }{
        wow for loop!! \;
        \For {condition}{
            \While{condition}{
            that's intriguing;
            \While{indx in indxs or empty}{indx  $\leftarrow$ rblabla\;}}
        blabla\;
        \For{condition}{
          blabla\;
          blabla\;}}}
          %here I should put something to stop the numbering
    \end{tcolorbox}
  \end{adjustbox} 
  \caption{my caption}
\end{algorithm}

\end{document}

We set \NlSty to \@gobble the line numbering outside the tcolorbox, and restore it inside. Crude, but functional.

| improve this answer | |
  • Thank you for the solution. this works fine! – matlio Oct 21 at 18:49

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.