Continuous line numbering in lstlisting, but separate lsttableoflistings entries?











up vote
1
down vote

favorite












i'd like to use the name=something,firstlinenumber=auto function for continuous line numbering throughout a couple of subsequent listings in my document. I'd also like to have each of the listings have its own separate entry in the List of Listings. The listings package seems to not like that, though: If I use the same name=something for multiple listings, only the first one gets an entry in the List of Listings, every other one does not. If I do not use name=something, each listing gets it's own entry in the List of Listings, but the continuous line numbering no longer works. Minimal example:



documentclass{article}

usepackage{listings}
lstset{numbers=left,firstnumber=auto}

begin{document}

lstlistoflistings
section{Contents}

begin{lstlisting}[name=test,label=first,caption=First Listing]
this should start at line number 1
and should be the first entry in the List of Listings
end{lstlisting}

Listing~ref{first} shows up correctly in the List of Listings.

begin{lstlisting}[name=test,label=second,caption=Second Listing]
this continues line numbers from the previous listing
but does not get an entry in the List of Listings
end{lstlisting}

Listing~ref{second} continues the line numbering, and has its own listing number
that can be referenced, but does not receive an entry in the list of Listings.

% parameter "name=test" is removed here..
begin{lstlisting}[label=third,caption=Third Listing]
this restarts the line numbers from line 1
but does get a second entry in the List of Listings
end{lstlisting}

Listing~ref{third} also has its own listing number that can be referenced, and
does get an entry in the List of Listings, but does not continue line numbers.

end{document}


Obviously I could manually count each listings' lines and then manually set the starting line number for each subsequent listing, but I'd prefer a more robust automatic solution. Does anyone know a way to get both behaviors, continuous line numbers AND separate List of Listings entries?



Thanks in advance!










share|improve this question









New contributor




MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    i'd like to use the name=something,firstlinenumber=auto function for continuous line numbering throughout a couple of subsequent listings in my document. I'd also like to have each of the listings have its own separate entry in the List of Listings. The listings package seems to not like that, though: If I use the same name=something for multiple listings, only the first one gets an entry in the List of Listings, every other one does not. If I do not use name=something, each listing gets it's own entry in the List of Listings, but the continuous line numbering no longer works. Minimal example:



    documentclass{article}

    usepackage{listings}
    lstset{numbers=left,firstnumber=auto}

    begin{document}

    lstlistoflistings
    section{Contents}

    begin{lstlisting}[name=test,label=first,caption=First Listing]
    this should start at line number 1
    and should be the first entry in the List of Listings
    end{lstlisting}

    Listing~ref{first} shows up correctly in the List of Listings.

    begin{lstlisting}[name=test,label=second,caption=Second Listing]
    this continues line numbers from the previous listing
    but does not get an entry in the List of Listings
    end{lstlisting}

    Listing~ref{second} continues the line numbering, and has its own listing number
    that can be referenced, but does not receive an entry in the list of Listings.

    % parameter "name=test" is removed here..
    begin{lstlisting}[label=third,caption=Third Listing]
    this restarts the line numbers from line 1
    but does get a second entry in the List of Listings
    end{lstlisting}

    Listing~ref{third} also has its own listing number that can be referenced, and
    does get an entry in the List of Listings, but does not continue line numbers.

    end{document}


    Obviously I could manually count each listings' lines and then manually set the starting line number for each subsequent listing, but I'd prefer a more robust automatic solution. Does anyone know a way to get both behaviors, continuous line numbers AND separate List of Listings entries?



    Thanks in advance!










    share|improve this question









    New contributor




    MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      i'd like to use the name=something,firstlinenumber=auto function for continuous line numbering throughout a couple of subsequent listings in my document. I'd also like to have each of the listings have its own separate entry in the List of Listings. The listings package seems to not like that, though: If I use the same name=something for multiple listings, only the first one gets an entry in the List of Listings, every other one does not. If I do not use name=something, each listing gets it's own entry in the List of Listings, but the continuous line numbering no longer works. Minimal example:



      documentclass{article}

      usepackage{listings}
      lstset{numbers=left,firstnumber=auto}

      begin{document}

      lstlistoflistings
      section{Contents}

      begin{lstlisting}[name=test,label=first,caption=First Listing]
      this should start at line number 1
      and should be the first entry in the List of Listings
      end{lstlisting}

      Listing~ref{first} shows up correctly in the List of Listings.

      begin{lstlisting}[name=test,label=second,caption=Second Listing]
      this continues line numbers from the previous listing
      but does not get an entry in the List of Listings
      end{lstlisting}

      Listing~ref{second} continues the line numbering, and has its own listing number
      that can be referenced, but does not receive an entry in the list of Listings.

      % parameter "name=test" is removed here..
      begin{lstlisting}[label=third,caption=Third Listing]
      this restarts the line numbers from line 1
      but does get a second entry in the List of Listings
      end{lstlisting}

      Listing~ref{third} also has its own listing number that can be referenced, and
      does get an entry in the List of Listings, but does not continue line numbers.

      end{document}


      Obviously I could manually count each listings' lines and then manually set the starting line number for each subsequent listing, but I'd prefer a more robust automatic solution. Does anyone know a way to get both behaviors, continuous line numbers AND separate List of Listings entries?



      Thanks in advance!










      share|improve this question









      New contributor




      MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      i'd like to use the name=something,firstlinenumber=auto function for continuous line numbering throughout a couple of subsequent listings in my document. I'd also like to have each of the listings have its own separate entry in the List of Listings. The listings package seems to not like that, though: If I use the same name=something for multiple listings, only the first one gets an entry in the List of Listings, every other one does not. If I do not use name=something, each listing gets it's own entry in the List of Listings, but the continuous line numbering no longer works. Minimal example:



      documentclass{article}

      usepackage{listings}
      lstset{numbers=left,firstnumber=auto}

      begin{document}

      lstlistoflistings
      section{Contents}

      begin{lstlisting}[name=test,label=first,caption=First Listing]
      this should start at line number 1
      and should be the first entry in the List of Listings
      end{lstlisting}

      Listing~ref{first} shows up correctly in the List of Listings.

      begin{lstlisting}[name=test,label=second,caption=Second Listing]
      this continues line numbers from the previous listing
      but does not get an entry in the List of Listings
      end{lstlisting}

      Listing~ref{second} continues the line numbering, and has its own listing number
      that can be referenced, but does not receive an entry in the list of Listings.

      % parameter "name=test" is removed here..
      begin{lstlisting}[label=third,caption=Third Listing]
      this restarts the line numbers from line 1
      but does get a second entry in the List of Listings
      end{lstlisting}

      Listing~ref{third} also has its own listing number that can be referenced, and
      does get an entry in the List of Listings, but does not continue line numbers.

      end{document}


      Obviously I could manually count each listings' lines and then manually set the starting line number for each subsequent listing, but I'd prefer a more robust automatic solution. Does anyone know a way to get both behaviors, continuous line numbers AND separate List of Listings entries?



      Thanks in advance!







      listings line-numbering lstlisting






      share|improve this question









      New contributor




      MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 27 at 8:11









      Stefan Pinnow

      19.2k83175




      19.2k83175






      New contributor




      MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 27 at 8:05









      MisterD

      82




      82




      New contributor




      MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      MisterD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          With this marcro from Yiannis Lazarides's cheeky answer, you can get the so-called robustness. That is with different naming, you not only continue the line numbering but also get the listings listed as they appear.



          documentclass{article}
          %https://tex.stackexchange.com/questions/95036/continue-line-numbers-in-listings-package
          usepackage{listings}

          lstnewenvironment{bash}[1]
          {lstset{language=C}lstset{%
          numbers=left,
          #1
          }}
          {}


          %
          %%% Always I forget this so I created some aliases
          defContinueLineNumber{lstset{firstnumber=last}}
          defStartLineAt#1{lstset{firstnumber=#1}}
          letnumberLineAtStartLineAt


          begin{document}

          lstlistoflistings
          ContinueLineNumber
          section{Contents}

          begin{bash}[name=test,label=first,caption=First Listing]
          this should start at line number 1
          and should be the first entry in the List of Listings
          end{bash}

          Listing~ref{first} shows up correctly in the List of Listings.

          begin{bash}[name=test5,label=second,caption=Second Listing]
          this continues line numbers from the previous listing
          but does not get an entry in the List of Listings
          end{bash}

          No more problems :)

          % parameter "name=test" is removed here..
          begin{bash}[name=test77, label=third,caption=Third Listing]
          this no more restarts the line numbers from line 1
          but does continue in the List of Listings
          end{bash}

          The macro does the job.
          end{document}


          which will give you:



          enter image description here






          share|improve this answer

















          • 1




            Ohh I see. I had seen this macro and tried using it, but it didn't work because I used it wrong. Now I see how it's supposed to be used, and it indeed does the job. Thanks!
            – MisterD
            Nov 27 at 8:44











          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',
          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
          });


          }
          });






          MisterD is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f461960%2fcontinuous-line-numbering-in-lstlisting-but-separate-lsttableoflistings-entries%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          With this marcro from Yiannis Lazarides's cheeky answer, you can get the so-called robustness. That is with different naming, you not only continue the line numbering but also get the listings listed as they appear.



          documentclass{article}
          %https://tex.stackexchange.com/questions/95036/continue-line-numbers-in-listings-package
          usepackage{listings}

          lstnewenvironment{bash}[1]
          {lstset{language=C}lstset{%
          numbers=left,
          #1
          }}
          {}


          %
          %%% Always I forget this so I created some aliases
          defContinueLineNumber{lstset{firstnumber=last}}
          defStartLineAt#1{lstset{firstnumber=#1}}
          letnumberLineAtStartLineAt


          begin{document}

          lstlistoflistings
          ContinueLineNumber
          section{Contents}

          begin{bash}[name=test,label=first,caption=First Listing]
          this should start at line number 1
          and should be the first entry in the List of Listings
          end{bash}

          Listing~ref{first} shows up correctly in the List of Listings.

          begin{bash}[name=test5,label=second,caption=Second Listing]
          this continues line numbers from the previous listing
          but does not get an entry in the List of Listings
          end{bash}

          No more problems :)

          % parameter "name=test" is removed here..
          begin{bash}[name=test77, label=third,caption=Third Listing]
          this no more restarts the line numbers from line 1
          but does continue in the List of Listings
          end{bash}

          The macro does the job.
          end{document}


          which will give you:



          enter image description here






          share|improve this answer

















          • 1




            Ohh I see. I had seen this macro and tried using it, but it didn't work because I used it wrong. Now I see how it's supposed to be used, and it indeed does the job. Thanks!
            – MisterD
            Nov 27 at 8:44















          up vote
          0
          down vote



          accepted










          With this marcro from Yiannis Lazarides's cheeky answer, you can get the so-called robustness. That is with different naming, you not only continue the line numbering but also get the listings listed as they appear.



          documentclass{article}
          %https://tex.stackexchange.com/questions/95036/continue-line-numbers-in-listings-package
          usepackage{listings}

          lstnewenvironment{bash}[1]
          {lstset{language=C}lstset{%
          numbers=left,
          #1
          }}
          {}


          %
          %%% Always I forget this so I created some aliases
          defContinueLineNumber{lstset{firstnumber=last}}
          defStartLineAt#1{lstset{firstnumber=#1}}
          letnumberLineAtStartLineAt


          begin{document}

          lstlistoflistings
          ContinueLineNumber
          section{Contents}

          begin{bash}[name=test,label=first,caption=First Listing]
          this should start at line number 1
          and should be the first entry in the List of Listings
          end{bash}

          Listing~ref{first} shows up correctly in the List of Listings.

          begin{bash}[name=test5,label=second,caption=Second Listing]
          this continues line numbers from the previous listing
          but does not get an entry in the List of Listings
          end{bash}

          No more problems :)

          % parameter "name=test" is removed here..
          begin{bash}[name=test77, label=third,caption=Third Listing]
          this no more restarts the line numbers from line 1
          but does continue in the List of Listings
          end{bash}

          The macro does the job.
          end{document}


          which will give you:



          enter image description here






          share|improve this answer

















          • 1




            Ohh I see. I had seen this macro and tried using it, but it didn't work because I used it wrong. Now I see how it's supposed to be used, and it indeed does the job. Thanks!
            – MisterD
            Nov 27 at 8:44













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          With this marcro from Yiannis Lazarides's cheeky answer, you can get the so-called robustness. That is with different naming, you not only continue the line numbering but also get the listings listed as they appear.



          documentclass{article}
          %https://tex.stackexchange.com/questions/95036/continue-line-numbers-in-listings-package
          usepackage{listings}

          lstnewenvironment{bash}[1]
          {lstset{language=C}lstset{%
          numbers=left,
          #1
          }}
          {}


          %
          %%% Always I forget this so I created some aliases
          defContinueLineNumber{lstset{firstnumber=last}}
          defStartLineAt#1{lstset{firstnumber=#1}}
          letnumberLineAtStartLineAt


          begin{document}

          lstlistoflistings
          ContinueLineNumber
          section{Contents}

          begin{bash}[name=test,label=first,caption=First Listing]
          this should start at line number 1
          and should be the first entry in the List of Listings
          end{bash}

          Listing~ref{first} shows up correctly in the List of Listings.

          begin{bash}[name=test5,label=second,caption=Second Listing]
          this continues line numbers from the previous listing
          but does not get an entry in the List of Listings
          end{bash}

          No more problems :)

          % parameter "name=test" is removed here..
          begin{bash}[name=test77, label=third,caption=Third Listing]
          this no more restarts the line numbers from line 1
          but does continue in the List of Listings
          end{bash}

          The macro does the job.
          end{document}


          which will give you:



          enter image description here






          share|improve this answer












          With this marcro from Yiannis Lazarides's cheeky answer, you can get the so-called robustness. That is with different naming, you not only continue the line numbering but also get the listings listed as they appear.



          documentclass{article}
          %https://tex.stackexchange.com/questions/95036/continue-line-numbers-in-listings-package
          usepackage{listings}

          lstnewenvironment{bash}[1]
          {lstset{language=C}lstset{%
          numbers=left,
          #1
          }}
          {}


          %
          %%% Always I forget this so I created some aliases
          defContinueLineNumber{lstset{firstnumber=last}}
          defStartLineAt#1{lstset{firstnumber=#1}}
          letnumberLineAtStartLineAt


          begin{document}

          lstlistoflistings
          ContinueLineNumber
          section{Contents}

          begin{bash}[name=test,label=first,caption=First Listing]
          this should start at line number 1
          and should be the first entry in the List of Listings
          end{bash}

          Listing~ref{first} shows up correctly in the List of Listings.

          begin{bash}[name=test5,label=second,caption=Second Listing]
          this continues line numbers from the previous listing
          but does not get an entry in the List of Listings
          end{bash}

          No more problems :)

          % parameter "name=test" is removed here..
          begin{bash}[name=test77, label=third,caption=Third Listing]
          this no more restarts the line numbers from line 1
          but does continue in the List of Listings
          end{bash}

          The macro does the job.
          end{document}


          which will give you:



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 at 8:33









          Raaja

          2,0742525




          2,0742525








          • 1




            Ohh I see. I had seen this macro and tried using it, but it didn't work because I used it wrong. Now I see how it's supposed to be used, and it indeed does the job. Thanks!
            – MisterD
            Nov 27 at 8:44














          • 1




            Ohh I see. I had seen this macro and tried using it, but it didn't work because I used it wrong. Now I see how it's supposed to be used, and it indeed does the job. Thanks!
            – MisterD
            Nov 27 at 8:44








          1




          1




          Ohh I see. I had seen this macro and tried using it, but it didn't work because I used it wrong. Now I see how it's supposed to be used, and it indeed does the job. Thanks!
          – MisterD
          Nov 27 at 8:44




          Ohh I see. I had seen this macro and tried using it, but it didn't work because I used it wrong. Now I see how it's supposed to be used, and it indeed does the job. Thanks!
          – MisterD
          Nov 27 at 8:44










          MisterD is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          MisterD is a new contributor. Be nice, and check out our Code of Conduct.













          MisterD is a new contributor. Be nice, and check out our Code of Conduct.












          MisterD is a new contributor. Be nice, and check out our Code of Conduct.
















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f461960%2fcontinuous-line-numbering-in-lstlisting-but-separate-lsttableoflistings-entries%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

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?