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:
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
deflst@PlaceNumber{ifnumvalue{lstnumber}=0else
llap{normalfontlst@numberstyle{thelstnumber}kernlst@numbersep}fi}
newcommand*Suppressnumber{%
lst@AddToHook{OnNewLine}{%
letthelstnumberrelax%
advancec@lstnumber-@nerelax%
}%
}
newcommand*Reactivatenumber{%
lst@AddToHook{OnNewLine}{%
letthelstnumberorigthelstnumber%
advancec@lstnumber@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
Correct answer by Elad Den on December 11, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP