# Listings: skip line number ONLY where command is defined

TeX - LaTeX Asked by SRSR333 on December 11, 2020

I have looked at the following:

However, the solution given in the latter thread only skips the line after the |Suppressnumber| command is given, and likewise, only resumes numbering after the |Reactivatenumber| command is given. In other words, this gives a range of skipped numbers.

What I’d like is:

1. the line number to be suppressed for empty lines (and the counter not incremented: this problem is solved in the first post above);
2. for specified lines only, not a range, to have their line numbers suppressed without incrementing the counter.

Using some very contrived C, for instance, this is what I would like to have:

begin{lstlisting}
int main () { |Suppressnumber|
1      printf("hellon");
2       while (1) {
3            ;
4            if (0) {
5                ;
} else {   |Suppressnumber|
6                  int b;
7                  scanf("%d", &b);
}  |Suppressnumber|
}    |Suppressnumber|
}   |Suppressnumber|
end{lstlisting}


I have experimented with most of the relevant hooks in listings, including EveryLine, EOL, EveryPar, InitVarsBOL and InitVarsEOL, and I can’t think of a way to do this.

The problem of the second solution, as you describe it, is that when you use |Suppressnumber| it suppresses the NEXT line. This would not be a problem: simply place the |Suppressnumber| on the line previous to the one you wish to suppress, then the |Reactivatenumber| on the line itself

Using this method you'd almost get the required output since you need the first line to be suppressed. If you do as the previous paragraph suggests you'll have an empty first line numbered one. Then the rest of the output would be numbered as you desire - only off by one.

The solution would be to use the solution suggested here to number the first line with 0 and remove it.

A full MWE:

documentclass{article}
usepackage{listings}

lstset{numbers=left,numberblanklines=false,firstnumber=0,escapeinside=||}
letorigthelstnumberthelstnumber
makeatletter
[email protected]{ifnumvalue{lstnumber}=0else
llap{[email protected]{thelstnumber}[email protected]}fi}
newcommand*Suppressnumber{%
[email protected]{OnNewLine}{%
letthelstnumberrelax%
[email protected]@nerelax%
}%
}

newcommand*Reactivatenumber{%
[email protected]{OnNewLine}{%
letthelstnumberorigthelstnumber%
[email protected]@nerelax}%
}
makeatother

begin{document}

begin{lstlisting}
|Suppressnumber|
int main () { |Reactivatenumber|
printf("hellon");
while (1) {
;
if (0)
;{|Suppressnumber|
} else {   |Reactivatenumber|
int b;
scanf("%d", &b);|Suppressnumber|
}
}
}
end{lstlisting}

end{document}


And the output

## Related Questions

### Setting paragraph indention

1  Asked on September 27, 2021 by nino-oostrom

### Why getting a larger image box when rotating?

1  Asked on September 27, 2021 by rob41

### Make two figures aligned at top

5  Asked on September 27, 2021 by dplanet

### How to Arrange Graphics (TikZ) If Using Input Command?

0  Asked on September 26, 2021

### Creating a very long timeline

0  Asked on September 26, 2021 by texomat

### clefval.sty: ! Package inputenc Error: Invalid UTF-8 byte “A0

2  Asked on September 26, 2021 by edd-barrett

### MikTex – pdflatex does not compile – initexmf.exe

1  Asked on September 26, 2021 by francois

### Undefined Control Sequence while using setmathdigitfont in xepersian

0  Asked on September 26, 2021 by ali-kazemkhanloo

### How do we make new command like “newcommand{eql}[2]{begin{align}label(#2)}#1end{align}}”

0  Asked on September 26, 2021 by latexatha

### Converting LaTeX to Word format? Better yet, with equation-editor format equations?

3  Asked on September 26, 2021 by carl-smith

### Problem listening to a WAV file embedded into a beamer presentation using Adobe Reader

0  Asked on September 26, 2021 by user221292

### Inline descriptions and automatic sorted glossary entries

1  Asked on September 26, 2021 by scott-lovell

### Jewish and Muslim holidays formula

0  Asked on September 26, 2021

### Print a list of selected enumerated list labels

2  Asked on September 26, 2021

### Height filling minipages in Beamer

1  Asked on September 26, 2021 by bconic

### Histogram with multiple plots of same data

1  Asked on September 26, 2021 by user221282

### Circle above a letter

4  Asked on September 26, 2021

### PDF output works in an embedded window, but not in the separate window

0  Asked on September 26, 2021 by koval-boris

### How do I define a new function juse

1  Asked on September 26, 2021

### Putting two images beside each other

6  Asked on September 26, 2021 by simplicity