lstlisting - line number gapsHow to skip lines in lstlisting with dots?First line number in lstinputlisting environmentCustom line numbers in lstlisting environmentlistings line numbers work with one lstlisting but not anotherLine number problemChanges sides of lstlisting line numbering when switching pagesSkipping line numbers in lstlistingListings: Skip line number on current lineSkip line number in listing doesn't worklstlisting - bold numbers, remove gaps between lineslstnewenvironment/lstset : using colors conditionally emph/emphstyle - SAS: (in=_x) versus if x in (1,2)

Is `x >> pure y` equivalent to `liftM (const y) x`

Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?

How does the UK government determine the size of a mandate?

Pole-zeros of a real-valued causal FIR system

How did Doctor Strange see the winning outcome in Avengers: Infinity War?

Arithmetic mean geometric mean inequality unclear

Method to test if a number is a perfect power?

Different result between scanning in Epson's "color negative film" mode and scanning in positive -> invert curve in post?

Trouble understanding the speech of overseas colleagues

Would a high gravity rocky planet be guaranteed to have an atmosphere?

Is exact Kanji stroke length important?

Would this custom Sorcerer variant that can only learn any verbal-component-only spell be unbalanced?

Is there a problem with hiding "forgot password" until it's needed?

What does "I’d sit this one out, Cap," imply or mean in the context?

Do sorcerers' Subtle Spells require a skill check to be unseen?

Implement the Thanos sorting algorithm

Why not increase contact surface when reentering the atmosphere?

Is it appropriate to ask a job candidate if we can record their interview?

Roman Numeral Treatment of Suspensions

Why does indent disappear in lists?

Is this apparent Class Action settlement a spam message?

How to safely derail a train during transit?

when is out of tune ok?

How can a function with a hole (removable discontinuity) equal a function with no hole?



lstlisting - line number gaps


How to skip lines in lstlisting with dots?First line number in lstinputlisting environmentCustom line numbers in lstlisting environmentlistings line numbers work with one lstlisting but not anotherLine number problemChanges sides of lstlisting line numbering when switching pagesSkipping line numbers in lstlistingListings: Skip line number on current lineSkip line number in listing doesn't worklstlisting - bold numbers, remove gaps between lineslstnewenvironment/lstset : using colors conditionally emph/emphstyle - SAS: (in=_x) versus if x in (1,2)













2















Is it possible to add non-number characters on the left of a code block?



Specifically, I want to add ... to correspond with some source code (see image).



enter image description here



I am currently using commands to stop / start the line numbers.
Here is the code:



documentclass[12pt]article

usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
usepackagexcolor, soul
usepackagelistings

definecolorcodebackgroundrgb0.95, 0.95, 0.92

lstset

frame = tb, % hrule above and below
keepspaces = true,
columns = flexible,
basicstyle = normalsizettfamily,
escapeinside = (*@@*), % for escaping
backgroundcolor = colorcodebackground,
showstringspaces = false,
language = C,
keywordstyle = colorblue,
stringstyle = colorred,
commentstyle = colorteal,
numbers = left, % none, left, right
firstnumber = 1,
numberstyle = scriptsizecolorblack,
numbersep = 5pt


letorigthelstnumberthelstnumber
makeatletter
% for stopping line numbers
newcommand*stopnumber

lst@AddToHookOnNewLine

letthelstnumberrelax
advancec@lstnumber-@nerelax


% for starting line numbers
newcommand*startnumber[1]

setcounterlstnumbernumexpr#1-1relax
lst@AddToHookOnNewLine

letthelstnumberorigthelstnumber
refstepcounterlstnumber


makeatother

begindocument
beginlstlisting[firstnumber = 456]
int third_function(const int c)

/* error checking */
if(c < 0)

return;
(*@ stopnumber @*)

...
(*@ startnumber512 @*)
/* other stuff */
int d = 4;
char* word = "third function"; (*@ stopnumber @*)

...
(*@ startnumber678 @*)
return (c * d);

endlstlisting
enddocument









share|improve this question




























    2















    Is it possible to add non-number characters on the left of a code block?



    Specifically, I want to add ... to correspond with some source code (see image).



    enter image description here



    I am currently using commands to stop / start the line numbers.
    Here is the code:



    documentclass[12pt]article

    usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
    usepackagexcolor, soul
    usepackagelistings

    definecolorcodebackgroundrgb0.95, 0.95, 0.92

    lstset

    frame = tb, % hrule above and below
    keepspaces = true,
    columns = flexible,
    basicstyle = normalsizettfamily,
    escapeinside = (*@@*), % for escaping
    backgroundcolor = colorcodebackground,
    showstringspaces = false,
    language = C,
    keywordstyle = colorblue,
    stringstyle = colorred,
    commentstyle = colorteal,
    numbers = left, % none, left, right
    firstnumber = 1,
    numberstyle = scriptsizecolorblack,
    numbersep = 5pt


    letorigthelstnumberthelstnumber
    makeatletter
    % for stopping line numbers
    newcommand*stopnumber

    lst@AddToHookOnNewLine

    letthelstnumberrelax
    advancec@lstnumber-@nerelax


    % for starting line numbers
    newcommand*startnumber[1]

    setcounterlstnumbernumexpr#1-1relax
    lst@AddToHookOnNewLine

    letthelstnumberorigthelstnumber
    refstepcounterlstnumber


    makeatother

    begindocument
    beginlstlisting[firstnumber = 456]
    int third_function(const int c)

    /* error checking */
    if(c < 0)

    return;
    (*@ stopnumber @*)

    ...
    (*@ startnumber512 @*)
    /* other stuff */
    int d = 4;
    char* word = "third function"; (*@ stopnumber @*)

    ...
    (*@ startnumber678 @*)
    return (c * d);

    endlstlisting
    enddocument









    share|improve this question


























      2












      2








      2








      Is it possible to add non-number characters on the left of a code block?



      Specifically, I want to add ... to correspond with some source code (see image).



      enter image description here



      I am currently using commands to stop / start the line numbers.
      Here is the code:



      documentclass[12pt]article

      usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
      usepackagexcolor, soul
      usepackagelistings

      definecolorcodebackgroundrgb0.95, 0.95, 0.92

      lstset

      frame = tb, % hrule above and below
      keepspaces = true,
      columns = flexible,
      basicstyle = normalsizettfamily,
      escapeinside = (*@@*), % for escaping
      backgroundcolor = colorcodebackground,
      showstringspaces = false,
      language = C,
      keywordstyle = colorblue,
      stringstyle = colorred,
      commentstyle = colorteal,
      numbers = left, % none, left, right
      firstnumber = 1,
      numberstyle = scriptsizecolorblack,
      numbersep = 5pt


      letorigthelstnumberthelstnumber
      makeatletter
      % for stopping line numbers
      newcommand*stopnumber

      lst@AddToHookOnNewLine

      letthelstnumberrelax
      advancec@lstnumber-@nerelax


      % for starting line numbers
      newcommand*startnumber[1]

      setcounterlstnumbernumexpr#1-1relax
      lst@AddToHookOnNewLine

      letthelstnumberorigthelstnumber
      refstepcounterlstnumber


      makeatother

      begindocument
      beginlstlisting[firstnumber = 456]
      int third_function(const int c)

      /* error checking */
      if(c < 0)

      return;
      (*@ stopnumber @*)

      ...
      (*@ startnumber512 @*)
      /* other stuff */
      int d = 4;
      char* word = "third function"; (*@ stopnumber @*)

      ...
      (*@ startnumber678 @*)
      return (c * d);

      endlstlisting
      enddocument









      share|improve this question
















      Is it possible to add non-number characters on the left of a code block?



      Specifically, I want to add ... to correspond with some source code (see image).



      enter image description here



      I am currently using commands to stop / start the line numbers.
      Here is the code:



      documentclass[12pt]article

      usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
      usepackagexcolor, soul
      usepackagelistings

      definecolorcodebackgroundrgb0.95, 0.95, 0.92

      lstset

      frame = tb, % hrule above and below
      keepspaces = true,
      columns = flexible,
      basicstyle = normalsizettfamily,
      escapeinside = (*@@*), % for escaping
      backgroundcolor = colorcodebackground,
      showstringspaces = false,
      language = C,
      keywordstyle = colorblue,
      stringstyle = colorred,
      commentstyle = colorteal,
      numbers = left, % none, left, right
      firstnumber = 1,
      numberstyle = scriptsizecolorblack,
      numbersep = 5pt


      letorigthelstnumberthelstnumber
      makeatletter
      % for stopping line numbers
      newcommand*stopnumber

      lst@AddToHookOnNewLine

      letthelstnumberrelax
      advancec@lstnumber-@nerelax


      % for starting line numbers
      newcommand*startnumber[1]

      setcounterlstnumbernumexpr#1-1relax
      lst@AddToHookOnNewLine

      letthelstnumberorigthelstnumber
      refstepcounterlstnumber


      makeatother

      begindocument
      beginlstlisting[firstnumber = 456]
      int third_function(const int c)

      /* error checking */
      if(c < 0)

      return;
      (*@ stopnumber @*)

      ...
      (*@ startnumber512 @*)
      /* other stuff */
      int d = 4;
      char* word = "third function"; (*@ stopnumber @*)

      ...
      (*@ startnumber678 @*)
      return (c * d);

      endlstlisting
      enddocument






      listings line-numbering






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 24 at 22:09







      user9506231

















      asked Feb 22 at 4:25









      user9506231user9506231

      264




      264




















          2 Answers
          2






          active

          oldest

          votes


















          1














          Here's a simple solution that makes use of the mathescape option to modify the counter and printing of line numbers. You could also use escapeinside if you want to use different escape characters than the standard $...$.



          documentclassarticle
          usepackagelistings

          lstsetbasicstyle=ttfamily, language=c, numbers=left

          makeatletter
          letorig@lstnumber=thelstnumber

          newcommandlstsetnumber[1]gdefthelstnumber#1
          newcommandlstresetnumbergloballetthelstnumber=orig@lstnumber
          makeatother

          begindocument
          beginlstlisting[firstnumber=100, mathescape=true]
          int foo(int x)
          // some code$lstsetnumberldots$
          ...$lstresetnumbersetcounterlstnumber199$

          // more code with extra spacing$lstsetnumber$
          $lstsetnumberldots$
          ...$lstsetnumber$
          $lstresetnumbersetcounterlstnumber299$

          return x;

          endlstlisting
          enddocument


          enter image description here



          The lstsetnumber macro redefines the output macro for line numbers, in the above example to the fixed value ldots. lstresetnumber resets the output to the standard definition.



          Note that you have to subtract 1 from the next line number if you modify the line counter via setcounterlstnumber as listings automatically increases the counter on the next line.



          The code for adding extra empty lines is a bit clumsy, not sure how to add vertical space in a more flexible way here.






          share|improve this answer























          • I have updated the question to show what approach I am using, as this method uses mathescape

            – user9506231
            Feb 24 at 22:25


















          0














          So here's another, easier to use approach that is syntactically the same as in your example:



          documentclass[12pt]article

          usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
          usepackagexcolor, soul
          usepackagelistings

          definecolorcodebackgroundrgb0.95, 0.95, 0.92
          lstset ...

          letorigthelstnumberthelstnumber
          makeatletter

          renewcommandthelstnumber%
          ifnumvaluelstnumber>0
          origthelstnumber
          else
          ifnumvaluelstnumber=-1
          ldots
          fi
          fi


          % for starting line numbers
          newcommand*startnumber[1]%
          setcounterlstnumbernumexpr#1-1relax%


          % for stopping line numbers
          newcommand*stopnumber%
          startnumber-2%


          makeatother

          begindocument
          beginlstlisting[firstnumber = 456]
          int third_function(const int c)

          /* error checking */
          if(c < 0)

          return;
          (*@ stopnumber @*)

          ...
          (*@ startnumber512 @*)
          /* other stuff */
          int d = 4;
          char* word = "third function"; (*@ stopnumber @*)

          ...
          (*@ startnumber678 @*)
          return (c * d);

          endlstlisting
          enddocument


          enter image description here



          In your code you are using lst@AddToHook multiple times to change thelstnumber. This is a bit problematic because adding hooks is accumulative, i.e. it adds the same commands everytime you use it. Also, the listings documentation explicitly doesn't guarantee that the order of hook execution is the same as the order of definition.



          My approach is based on redefining thelstnumber only once and (mis)using negative line numbers for the ellipses in your code. Positive numbers are printed as usual, -1 denotes ldots, other negative numbers don't yield any output at all. So in stopnumbers we just init lstnumber with -2 to map the following sequence of -2, -1, 0 to empty, ldots, empty.



          In case you'd want a short ellipsis without empty lines around, you could define a new command that inits lstnumber with -1.






          share|improve this answer






















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "85"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f476100%2flstlisting-line-number-gaps%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Here's a simple solution that makes use of the mathescape option to modify the counter and printing of line numbers. You could also use escapeinside if you want to use different escape characters than the standard $...$.



            documentclassarticle
            usepackagelistings

            lstsetbasicstyle=ttfamily, language=c, numbers=left

            makeatletter
            letorig@lstnumber=thelstnumber

            newcommandlstsetnumber[1]gdefthelstnumber#1
            newcommandlstresetnumbergloballetthelstnumber=orig@lstnumber
            makeatother

            begindocument
            beginlstlisting[firstnumber=100, mathescape=true]
            int foo(int x)
            // some code$lstsetnumberldots$
            ...$lstresetnumbersetcounterlstnumber199$

            // more code with extra spacing$lstsetnumber$
            $lstsetnumberldots$
            ...$lstsetnumber$
            $lstresetnumbersetcounterlstnumber299$

            return x;

            endlstlisting
            enddocument


            enter image description here



            The lstsetnumber macro redefines the output macro for line numbers, in the above example to the fixed value ldots. lstresetnumber resets the output to the standard definition.



            Note that you have to subtract 1 from the next line number if you modify the line counter via setcounterlstnumber as listings automatically increases the counter on the next line.



            The code for adding extra empty lines is a bit clumsy, not sure how to add vertical space in a more flexible way here.






            share|improve this answer























            • I have updated the question to show what approach I am using, as this method uses mathescape

              – user9506231
              Feb 24 at 22:25















            1














            Here's a simple solution that makes use of the mathescape option to modify the counter and printing of line numbers. You could also use escapeinside if you want to use different escape characters than the standard $...$.



            documentclassarticle
            usepackagelistings

            lstsetbasicstyle=ttfamily, language=c, numbers=left

            makeatletter
            letorig@lstnumber=thelstnumber

            newcommandlstsetnumber[1]gdefthelstnumber#1
            newcommandlstresetnumbergloballetthelstnumber=orig@lstnumber
            makeatother

            begindocument
            beginlstlisting[firstnumber=100, mathescape=true]
            int foo(int x)
            // some code$lstsetnumberldots$
            ...$lstresetnumbersetcounterlstnumber199$

            // more code with extra spacing$lstsetnumber$
            $lstsetnumberldots$
            ...$lstsetnumber$
            $lstresetnumbersetcounterlstnumber299$

            return x;

            endlstlisting
            enddocument


            enter image description here



            The lstsetnumber macro redefines the output macro for line numbers, in the above example to the fixed value ldots. lstresetnumber resets the output to the standard definition.



            Note that you have to subtract 1 from the next line number if you modify the line counter via setcounterlstnumber as listings automatically increases the counter on the next line.



            The code for adding extra empty lines is a bit clumsy, not sure how to add vertical space in a more flexible way here.






            share|improve this answer























            • I have updated the question to show what approach I am using, as this method uses mathescape

              – user9506231
              Feb 24 at 22:25













            1












            1








            1







            Here's a simple solution that makes use of the mathescape option to modify the counter and printing of line numbers. You could also use escapeinside if you want to use different escape characters than the standard $...$.



            documentclassarticle
            usepackagelistings

            lstsetbasicstyle=ttfamily, language=c, numbers=left

            makeatletter
            letorig@lstnumber=thelstnumber

            newcommandlstsetnumber[1]gdefthelstnumber#1
            newcommandlstresetnumbergloballetthelstnumber=orig@lstnumber
            makeatother

            begindocument
            beginlstlisting[firstnumber=100, mathescape=true]
            int foo(int x)
            // some code$lstsetnumberldots$
            ...$lstresetnumbersetcounterlstnumber199$

            // more code with extra spacing$lstsetnumber$
            $lstsetnumberldots$
            ...$lstsetnumber$
            $lstresetnumbersetcounterlstnumber299$

            return x;

            endlstlisting
            enddocument


            enter image description here



            The lstsetnumber macro redefines the output macro for line numbers, in the above example to the fixed value ldots. lstresetnumber resets the output to the standard definition.



            Note that you have to subtract 1 from the next line number if you modify the line counter via setcounterlstnumber as listings automatically increases the counter on the next line.



            The code for adding extra empty lines is a bit clumsy, not sure how to add vertical space in a more flexible way here.






            share|improve this answer













            Here's a simple solution that makes use of the mathescape option to modify the counter and printing of line numbers. You could also use escapeinside if you want to use different escape characters than the standard $...$.



            documentclassarticle
            usepackagelistings

            lstsetbasicstyle=ttfamily, language=c, numbers=left

            makeatletter
            letorig@lstnumber=thelstnumber

            newcommandlstsetnumber[1]gdefthelstnumber#1
            newcommandlstresetnumbergloballetthelstnumber=orig@lstnumber
            makeatother

            begindocument
            beginlstlisting[firstnumber=100, mathescape=true]
            int foo(int x)
            // some code$lstsetnumberldots$
            ...$lstresetnumbersetcounterlstnumber199$

            // more code with extra spacing$lstsetnumber$
            $lstsetnumberldots$
            ...$lstsetnumber$
            $lstresetnumbersetcounterlstnumber299$

            return x;

            endlstlisting
            enddocument


            enter image description here



            The lstsetnumber macro redefines the output macro for line numbers, in the above example to the fixed value ldots. lstresetnumber resets the output to the standard definition.



            Note that you have to subtract 1 from the next line number if you modify the line counter via setcounterlstnumber as listings automatically increases the counter on the next line.



            The code for adding extra empty lines is a bit clumsy, not sure how to add vertical space in a more flexible way here.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 22 at 5:57









            siracusasiracusa

            5,16511429




            5,16511429












            • I have updated the question to show what approach I am using, as this method uses mathescape

              – user9506231
              Feb 24 at 22:25

















            • I have updated the question to show what approach I am using, as this method uses mathescape

              – user9506231
              Feb 24 at 22:25
















            I have updated the question to show what approach I am using, as this method uses mathescape

            – user9506231
            Feb 24 at 22:25





            I have updated the question to show what approach I am using, as this method uses mathescape

            – user9506231
            Feb 24 at 22:25











            0














            So here's another, easier to use approach that is syntactically the same as in your example:



            documentclass[12pt]article

            usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
            usepackagexcolor, soul
            usepackagelistings

            definecolorcodebackgroundrgb0.95, 0.95, 0.92
            lstset ...

            letorigthelstnumberthelstnumber
            makeatletter

            renewcommandthelstnumber%
            ifnumvaluelstnumber>0
            origthelstnumber
            else
            ifnumvaluelstnumber=-1
            ldots
            fi
            fi


            % for starting line numbers
            newcommand*startnumber[1]%
            setcounterlstnumbernumexpr#1-1relax%


            % for stopping line numbers
            newcommand*stopnumber%
            startnumber-2%


            makeatother

            begindocument
            beginlstlisting[firstnumber = 456]
            int third_function(const int c)

            /* error checking */
            if(c < 0)

            return;
            (*@ stopnumber @*)

            ...
            (*@ startnumber512 @*)
            /* other stuff */
            int d = 4;
            char* word = "third function"; (*@ stopnumber @*)

            ...
            (*@ startnumber678 @*)
            return (c * d);

            endlstlisting
            enddocument


            enter image description here



            In your code you are using lst@AddToHook multiple times to change thelstnumber. This is a bit problematic because adding hooks is accumulative, i.e. it adds the same commands everytime you use it. Also, the listings documentation explicitly doesn't guarantee that the order of hook execution is the same as the order of definition.



            My approach is based on redefining thelstnumber only once and (mis)using negative line numbers for the ellipses in your code. Positive numbers are printed as usual, -1 denotes ldots, other negative numbers don't yield any output at all. So in stopnumbers we just init lstnumber with -2 to map the following sequence of -2, -1, 0 to empty, ldots, empty.



            In case you'd want a short ellipsis without empty lines around, you could define a new command that inits lstnumber with -1.






            share|improve this answer



























              0














              So here's another, easier to use approach that is syntactically the same as in your example:



              documentclass[12pt]article

              usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
              usepackagexcolor, soul
              usepackagelistings

              definecolorcodebackgroundrgb0.95, 0.95, 0.92
              lstset ...

              letorigthelstnumberthelstnumber
              makeatletter

              renewcommandthelstnumber%
              ifnumvaluelstnumber>0
              origthelstnumber
              else
              ifnumvaluelstnumber=-1
              ldots
              fi
              fi


              % for starting line numbers
              newcommand*startnumber[1]%
              setcounterlstnumbernumexpr#1-1relax%


              % for stopping line numbers
              newcommand*stopnumber%
              startnumber-2%


              makeatother

              begindocument
              beginlstlisting[firstnumber = 456]
              int third_function(const int c)

              /* error checking */
              if(c < 0)

              return;
              (*@ stopnumber @*)

              ...
              (*@ startnumber512 @*)
              /* other stuff */
              int d = 4;
              char* word = "third function"; (*@ stopnumber @*)

              ...
              (*@ startnumber678 @*)
              return (c * d);

              endlstlisting
              enddocument


              enter image description here



              In your code you are using lst@AddToHook multiple times to change thelstnumber. This is a bit problematic because adding hooks is accumulative, i.e. it adds the same commands everytime you use it. Also, the listings documentation explicitly doesn't guarantee that the order of hook execution is the same as the order of definition.



              My approach is based on redefining thelstnumber only once and (mis)using negative line numbers for the ellipses in your code. Positive numbers are printed as usual, -1 denotes ldots, other negative numbers don't yield any output at all. So in stopnumbers we just init lstnumber with -2 to map the following sequence of -2, -1, 0 to empty, ldots, empty.



              In case you'd want a short ellipsis without empty lines around, you could define a new command that inits lstnumber with -1.






              share|improve this answer

























                0












                0








                0







                So here's another, easier to use approach that is syntactically the same as in your example:



                documentclass[12pt]article

                usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
                usepackagexcolor, soul
                usepackagelistings

                definecolorcodebackgroundrgb0.95, 0.95, 0.92
                lstset ...

                letorigthelstnumberthelstnumber
                makeatletter

                renewcommandthelstnumber%
                ifnumvaluelstnumber>0
                origthelstnumber
                else
                ifnumvaluelstnumber=-1
                ldots
                fi
                fi


                % for starting line numbers
                newcommand*startnumber[1]%
                setcounterlstnumbernumexpr#1-1relax%


                % for stopping line numbers
                newcommand*stopnumber%
                startnumber-2%


                makeatother

                begindocument
                beginlstlisting[firstnumber = 456]
                int third_function(const int c)

                /* error checking */
                if(c < 0)

                return;
                (*@ stopnumber @*)

                ...
                (*@ startnumber512 @*)
                /* other stuff */
                int d = 4;
                char* word = "third function"; (*@ stopnumber @*)

                ...
                (*@ startnumber678 @*)
                return (c * d);

                endlstlisting
                enddocument


                enter image description here



                In your code you are using lst@AddToHook multiple times to change thelstnumber. This is a bit problematic because adding hooks is accumulative, i.e. it adds the same commands everytime you use it. Also, the listings documentation explicitly doesn't guarantee that the order of hook execution is the same as the order of definition.



                My approach is based on redefining thelstnumber only once and (mis)using negative line numbers for the ellipses in your code. Positive numbers are printed as usual, -1 denotes ldots, other negative numbers don't yield any output at all. So in stopnumbers we just init lstnumber with -2 to map the following sequence of -2, -1, 0 to empty, ldots, empty.



                In case you'd want a short ellipsis without empty lines around, you could define a new command that inits lstnumber with -1.






                share|improve this answer













                So here's another, easier to use approach that is syntactically the same as in your example:



                documentclass[12pt]article

                usepackage[a4paper, margin = 20mm, top = 30mm, bottom = 25mm]geometry
                usepackagexcolor, soul
                usepackagelistings

                definecolorcodebackgroundrgb0.95, 0.95, 0.92
                lstset ...

                letorigthelstnumberthelstnumber
                makeatletter

                renewcommandthelstnumber%
                ifnumvaluelstnumber>0
                origthelstnumber
                else
                ifnumvaluelstnumber=-1
                ldots
                fi
                fi


                % for starting line numbers
                newcommand*startnumber[1]%
                setcounterlstnumbernumexpr#1-1relax%


                % for stopping line numbers
                newcommand*stopnumber%
                startnumber-2%


                makeatother

                begindocument
                beginlstlisting[firstnumber = 456]
                int third_function(const int c)

                /* error checking */
                if(c < 0)

                return;
                (*@ stopnumber @*)

                ...
                (*@ startnumber512 @*)
                /* other stuff */
                int d = 4;
                char* word = "third function"; (*@ stopnumber @*)

                ...
                (*@ startnumber678 @*)
                return (c * d);

                endlstlisting
                enddocument


                enter image description here



                In your code you are using lst@AddToHook multiple times to change thelstnumber. This is a bit problematic because adding hooks is accumulative, i.e. it adds the same commands everytime you use it. Also, the listings documentation explicitly doesn't guarantee that the order of hook execution is the same as the order of definition.



                My approach is based on redefining thelstnumber only once and (mis)using negative line numbers for the ellipses in your code. Positive numbers are printed as usual, -1 denotes ldots, other negative numbers don't yield any output at all. So in stopnumbers we just init lstnumber with -2 to map the following sequence of -2, -1, 0 to empty, ldots, empty.



                In case you'd want a short ellipsis without empty lines around, you could define a new command that inits lstnumber with -1.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 11 mins ago









                siracusasiracusa

                5,16511429




                5,16511429



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f476100%2flstlisting-line-number-gaps%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Isabella Eugénie Boyer Biographie | Références | Menu de navigationmodifiermodifier le codeComparator to Compute the Relative Value of a U.S. Dollar Amount – 1774 to Present.

                    Lioubotyn Sommaire Géographie | Histoire | Population | Notes et références | Liens externes | Menu de navigationlubotin.kharkov.uamodifier« Recensements et estimations de la population depuis 1897 »« Office des statistiques d'Ukraine : population au 1er janvier 2010, 2011 et 2012 »« Office des statistiques d'Ukraine : population au 1er janvier 2011, 2012 et 2013 »Informations officiellesCartes topographiquesCarte routièrem

                    Mpande kaSenzangakhona Biographie | Références | Menu de navigationmodifierMpande kaSenzangakhonavoir la liste des auteursm