2

I'm trying to skip some line numbers in my listing but it doesn't work. I followed this guide Skip line numbers and resume from specific number ... don't know why it's not working.

The new line number always shows in my border. Hope someone can help me.

This is my code:

\documentclass{article} 
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}

\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}

\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
     \advance\c@lstnumber-\@ne\relax%
    }%
}

\newcommand*\Reactivatenumber{%
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
   \advance\c@lstnumber\@ne\relax}%
}

\makeatother
\begin{document}
\begin{lstlisting}[language=c++,basicstyle=\footnotesize\sffamily,
    showstringspaces=false,
    breaklines=true,
    frame={leftline,bottomline,rightline,topline},
    backgroundcolor={\color{background}},numbers=left,xleftmargin=15pt, firstnumber=195,caption={This is a test beginning with line 195 },label=lst:code,captionpos=b,escapeinside=||]
while (player->moveNext()) {
    StringName term = player->get();

    if (bla.exp) {
        bla.exp *= 2;
    } else {
        bla.exp = 1;
    }
    term++;
}|\Suppressnumber|

// ...
|\Reactivatenumber{214}|
// this is a very (SHOULD START AT 214)
// long
// comment

double etc = 0.6 * (x/y) - speedlimit;
\end{lstlisting}
\end{document}

And it's always pushing the new line number inside the box like this: enter image description here

Hope someone can help me :)

2
  • 1
    Your \Reactivatenumber command has no argument, but your using it with {214}, that's why the number 214 appears in the listing text – user31729 Aug 24 '17 at 18:02
  • Oh .. yes i see.. what a horrible mistake thanks @ChristianHupfer It's working now! – mtzE Aug 24 '17 at 18:09
1

There are two logic errors in there:

  • |\Reactivatenumber{214}| is used as a macro with argument but it is not defined as such, so 214 is no argument but just a token that is left in the input stream, appearing as text in the listings.
  • The other one is that the lstnumber counter is never set to 214 there, so this must be done with \setcounter{lstnumber}{214} or \global\c@lstnumber=#1, followed by a decrease by one then.

    \newcommand\Reactivatenumber[1]{%
      \global\c@lstnumber#1%
      \global\advance\c@lstnumber\m@ne\relax%
      \lst@AddToHook{OnNewLine}{%
      \let\thelstnumber\origthelstnumber%
      }%
    }
    

Here is the final code:

\documentclass{article} 
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}

\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}

\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
%    \advance\c@lstnumber-\@ne\relax% Not really necessary
  }%
}

\newcommand\Reactivatenumber[1]{%
  \global\c@lstnumber#1%
  \global\advance\c@lstnumber\m@ne\relax%
  \lst@AddToHook{OnNewLine}{%
  \let\thelstnumber\origthelstnumber%
  }%
}
\makeatother

\begin{document}
\begin{lstlisting}[language=c++,basicstyle=\footnotesize\sffamily,
    showstringspaces=false,
    breaklines=true,
    frame={leftline,bottomline,rightline,topline},
    backgroundcolor={\color{background}},numbers=left,xleftmargin=15pt, firstnumber=195,caption={This is a test beginning with line 195 },label=lst:code,captionpos=b,escapeinside=||]
while (player->moveNext()) {
    StringName term = player->get();

    if (bla.exp) {
        bla.exp *= 2;
    } else {
        bla.exp = 1;
    }
    term++;
}|\Suppressnumber|
// ...
|\Reactivatenumber{214}|
// this is a very (SHOULD START AT 214)
// long
// comment

double etc = 0.6 * (x/y) - speedlimit;
\end{lstlisting}
\end{document}

enter image description here

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.