Listings line numbers that match the linerange specification











up vote
10
down vote

favorite












This is a follow-up question to First line number in lstinputlisting environment. Consider the MWE:



documentclass{article}
%usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
usepackage{filecontents,listings}% http://ctan.org/pkg/{filecontents,listings}
lstset{
numbers=left,
basicstyle=ttfamilyfootnotesize
}
begin{filecontents*}{mycode.txt}
Line 1
Line 2
Line 3
Line 4
Line 5
end{filecontents*}

makeatletter
% https://tex.stackexchange.com/q/26828/5764
%patchcmd{lst@GLI@}% <command>
% {deflst@firstline{#1relax}}% <search>
% {deflst@firstline{#1relax}deflst@firstnumber{#1relax}}% <replace>
% {typeout{listings firstnumber=firstline}}% <success>
% {typeout{listings firstnumber not set}}% <failure>
makeatother

begin{document}
lstinputlisting[linerange={1-3}]{mycode.txt}
lstinputlisting[linerange={2-4}]{mycode.txt}
lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
end{document}


Without the aid of the etoolbox patch (or the default), the output looks like that of the leftmost column. With the aid of the patch (a "firstnumber correction), the output looks like that of the middle side. However, I'd like the output to look like that of the rightmost column.



enter image description here



That is, the eventual output of line numbers should match that of the linerange specification.



The trivial solution I'm hoping to avoid is the inclusion of separate lstinputlistings for each range with the appropriate vertical adjustment of -2medskipamount (default space above/below an included listing). That was how the rightmost column was obtained in the first place.










share|improve this question




























    up vote
    10
    down vote

    favorite












    This is a follow-up question to First line number in lstinputlisting environment. Consider the MWE:



    documentclass{article}
    %usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
    usepackage{filecontents,listings}% http://ctan.org/pkg/{filecontents,listings}
    lstset{
    numbers=left,
    basicstyle=ttfamilyfootnotesize
    }
    begin{filecontents*}{mycode.txt}
    Line 1
    Line 2
    Line 3
    Line 4
    Line 5
    end{filecontents*}

    makeatletter
    % https://tex.stackexchange.com/q/26828/5764
    %patchcmd{lst@GLI@}% <command>
    % {deflst@firstline{#1relax}}% <search>
    % {deflst@firstline{#1relax}deflst@firstnumber{#1relax}}% <replace>
    % {typeout{listings firstnumber=firstline}}% <success>
    % {typeout{listings firstnumber not set}}% <failure>
    makeatother

    begin{document}
    lstinputlisting[linerange={1-3}]{mycode.txt}
    lstinputlisting[linerange={2-4}]{mycode.txt}
    lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
    end{document}


    Without the aid of the etoolbox patch (or the default), the output looks like that of the leftmost column. With the aid of the patch (a "firstnumber correction), the output looks like that of the middle side. However, I'd like the output to look like that of the rightmost column.



    enter image description here



    That is, the eventual output of line numbers should match that of the linerange specification.



    The trivial solution I'm hoping to avoid is the inclusion of separate lstinputlistings for each range with the appropriate vertical adjustment of -2medskipamount (default space above/below an included listing). That was how the rightmost column was obtained in the first place.










    share|improve this question


























      up vote
      10
      down vote

      favorite









      up vote
      10
      down vote

      favorite











      This is a follow-up question to First line number in lstinputlisting environment. Consider the MWE:



      documentclass{article}
      %usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
      usepackage{filecontents,listings}% http://ctan.org/pkg/{filecontents,listings}
      lstset{
      numbers=left,
      basicstyle=ttfamilyfootnotesize
      }
      begin{filecontents*}{mycode.txt}
      Line 1
      Line 2
      Line 3
      Line 4
      Line 5
      end{filecontents*}

      makeatletter
      % https://tex.stackexchange.com/q/26828/5764
      %patchcmd{lst@GLI@}% <command>
      % {deflst@firstline{#1relax}}% <search>
      % {deflst@firstline{#1relax}deflst@firstnumber{#1relax}}% <replace>
      % {typeout{listings firstnumber=firstline}}% <success>
      % {typeout{listings firstnumber not set}}% <failure>
      makeatother

      begin{document}
      lstinputlisting[linerange={1-3}]{mycode.txt}
      lstinputlisting[linerange={2-4}]{mycode.txt}
      lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
      end{document}


      Without the aid of the etoolbox patch (or the default), the output looks like that of the leftmost column. With the aid of the patch (a "firstnumber correction), the output looks like that of the middle side. However, I'd like the output to look like that of the rightmost column.



      enter image description here



      That is, the eventual output of line numbers should match that of the linerange specification.



      The trivial solution I'm hoping to avoid is the inclusion of separate lstinputlistings for each range with the appropriate vertical adjustment of -2medskipamount (default space above/below an included listing). That was how the rightmost column was obtained in the first place.










      share|improve this question















      This is a follow-up question to First line number in lstinputlisting environment. Consider the MWE:



      documentclass{article}
      %usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
      usepackage{filecontents,listings}% http://ctan.org/pkg/{filecontents,listings}
      lstset{
      numbers=left,
      basicstyle=ttfamilyfootnotesize
      }
      begin{filecontents*}{mycode.txt}
      Line 1
      Line 2
      Line 3
      Line 4
      Line 5
      end{filecontents*}

      makeatletter
      % https://tex.stackexchange.com/q/26828/5764
      %patchcmd{lst@GLI@}% <command>
      % {deflst@firstline{#1relax}}% <search>
      % {deflst@firstline{#1relax}deflst@firstnumber{#1relax}}% <replace>
      % {typeout{listings firstnumber=firstline}}% <success>
      % {typeout{listings firstnumber not set}}% <failure>
      makeatother

      begin{document}
      lstinputlisting[linerange={1-3}]{mycode.txt}
      lstinputlisting[linerange={2-4}]{mycode.txt}
      lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
      end{document}


      Without the aid of the etoolbox patch (or the default), the output looks like that of the leftmost column. With the aid of the patch (a "firstnumber correction), the output looks like that of the middle side. However, I'd like the output to look like that of the rightmost column.



      enter image description here



      That is, the eventual output of line numbers should match that of the linerange specification.



      The trivial solution I'm hoping to avoid is the inclusion of separate lstinputlistings for each range with the appropriate vertical adjustment of -2medskipamount (default space above/below an included listing). That was how the rightmost column was obtained in the first place.







      listings external-files line-numbering






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 13 '17 at 12:35









      Community

      1




      1










      asked Apr 23 '13 at 6:25









      Werner

      430k599481624




      430k599481624






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          My previous solution had problems with the numbering of blank lines. I have added a new solution and kept the old one further down.



          New Solution



          What one really needs to do is reset the counter lstnumber at the beginning of each range. To achieve this one needs to hook into an internal command lst@SkipToFirst. One approach is to define a new key matchrangestart initialised as false via



          lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}


          and then add the following test at the beginning of lst@SkiptoFirst:



          lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi


          This the general way listings set-up keys with true/false values and how it handles the counter lstnumber. This code says if the matchrangestart key is true then when skipping forward to the beginning of a rang set lstnumber to one less than the first line number of the range. When the line gets printed this number is then incremented before being printed so we get the desired output.



          New sample output



          documentclass{article}

          usepackage{filecontents,listings}
          lstset{
          basicstyle=ttfamilyfootnotesize
          }

          begin{filecontents*}{mycode.txt}
          Line 1
          Line 2
          Line 3


          Line 6
          Line 7

          Line 9
          Line 10
          end{filecontents*}

          makeatletter
          lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}
          deflst@SkipToFirst{%
          lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi
          ifnum lst@lineno<lst@firstline
          deflst@next{lst@BeginDropInputlst@Pmode
          lst@Let{13}lst@MSkipToFirst
          lst@Let{10}lst@MSkipToFirst}%
          expandafterlst@next
          else
          expandafterlst@BOLGobble
          fi}
          makeatother

          begin{document}
          begin{minipage}{0.45linewidth}
          Standard
          lstset{numbers=left}
          lstinputlisting[linerange={1-3}]{mycode.txt}

          lstinputlisting[linerange={2-6}]{mycode.txt}

          lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
          end{minipage}
          begin{minipage}{0.45linewidth}
          Literal
          lstset{numbers=left,matchrangestart=t}
          lstinputlisting[linerange={1-3}]{mycode.txt}

          lstinputlisting[linerange={2-6}]{mycode.txt}

          lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
          end{minipage}

          end{document}


          Old Solution



          This places wrong numbers on blank lines



          The listings package has an internal plain counter lst@lineno holding the line number you are after. Now the numbers=left option, runs the code



          deflst@PlaceNumber{llap{normalfont
          lst@numberstyle{thelstnumber}kernlst@numbersep}}


          so all we need to do is take this code and replace the printed latex counter thelstnumber by thelist@lineno:



          Sample output



          documentclass{article}

          usepackage{filecontents,listings}
          lstset{
          basicstyle=ttfamilyfootnotesize
          }

          begin{filecontents*}{mycode.txt}
          Line 1
          Line 2
          Line 3
          Line 4
          Line 5
          end{filecontents*}

          makeatletter
          deflst@PlaceNumber{llap{normalfont
          lst@numberstyle{thelst@lineno}kernlst@numbersep}}
          makeatother

          begin{document}
          lstinputlisting[linerange={1-3}]{mycode.txt}
          lstinputlisting[linerange={2-4}]{mycode.txt}
          lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
          end{document}


          The above coding sets this style globally. If you wish to have local switches then you can extend the listings definition of the numbers option, adding a leftliteral type as follows:



          documentclass{article}

          usepackage{filecontents,listings}

          makeatletter
          lst@Key{numbers}{none}{%
          letlst@PlaceNumber@empty
          lstKV@SwitchCases{#1}%
          {none&\%
          left&deflst@PlaceNumber{llap{normalfont
          lst@numberstyle{thelstnumber}kernlst@numbersep}}\%
          leftliteral&deflst@PlaceNumber{llap{normalfont
          lst@numberstyle{thelst@lineno}kernlst@numbersep}}\%
          right&deflst@PlaceNumber{rlap{normalfont
          kernlinewidth kernlst@numbersep
          lst@numberstyle{thelstnumber}}}%
          }{PackageError{Listings}{Numbers #1 unknown}@ehc}}
          makeatother

          lstset{
          basicstyle=ttfamilyfootnotesize
          }

          begin{filecontents*}{mycode.txt}
          Line 1
          Line 2
          Line 3
          Line 4
          Line 5
          end{filecontents*}

          begin{document}
          begin{minipage}{0.3linewidth}
          Standard
          lstset{numbers=left}
          lstinputlisting[linerange={1-3}]{mycode.txt}
          lstinputlisting[linerange={2-4}]{mycode.txt}
          lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
          end{minipage}
          begin{minipage}{0.3linewidth}
          Literal
          lstset{numbers=leftliteral}
          lstinputlisting[linerange={1-3}]{mycode.txt}
          lstinputlisting[linerange={2-4}]{mycode.txt}
          lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
          end{minipage}
          begin{minipage}{0.3linewidth}
          Standard
          lstset{numbers=left}
          lstinputlisting[linerange={1-3}]{mycode.txt}
          lstinputlisting[linerange={2-4}]{mycode.txt}
          lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
          end{minipage}
          end{document}


          Sample switch






          share|improve this answer























          • Well, that was easy! :) Thanks Andrew.
            – Werner
            Apr 23 '13 at 14:16










          • This seems to overwrite other listingsoptions set by lstset like showlines=true– is there a way to keep those as well?
            – k.stm
            Jan 12 '14 at 0:05










          • @K.Stm. It is not overwriting these options, but there is interaction with them that one may not wish. The problem is that different options reference different counters.
            – Andrew Swann
            Jan 12 '14 at 14:15










          • @AndrewSwann True. I’m having problems with showlines that are unrelated. However, the solution here doesn’t go nicely with the lstlinebgrd package. If one wants to keep background colors, one has to imitate its implementation and add lst@linebgrd between the last two closing curly braces in the definitions of the key cases left, leftliteral, right (and rightliteral if one has defined it), e.g. …kernlst@numbersep}lst@linebgrd}\%.
            – k.stm
            Jan 12 '14 at 14:19












          • @K.Stm. Not surprising; that package is also changing the same command. Your code suggestion seems to be the right sort approach. If you have open problems, you should ask a separate question on this site.
            – Andrew Swann
            Jan 12 '14 at 15:06


















          up vote
          2
          down vote













          Based upon Andrew's answer I found this to be a better match to the problem:



          makeatletter
          deflst@MSkipToFirst{%
          globaladvancelst@lineno@ne
          ifnum lst@lineno=lst@firstline
          deflst@next{lst@LeaveMode globallst@newlinesz@
          lst@OnceAtEOL globalletlst@OnceAtEOL@empty
          lst@InitLstNumber % Added to work with modified lsthk@PreInit.
          lsthk@InitVarsBOL
          c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
          lst@BOLGobble}%
          expandafterlst@next
          fi}
          makeatother


          The advantage over his answer is that it supports multi-range constructs correctly without the need to introduce a key. This is done by overriding the method which actually takes care of range-skipping and already containing a hook for the first line of a new range.



          Tested against listings 1.6 (2015/06/04). As always when overriding a function pay attention to changes of the package's source (shouldn't be to hard as only one line was added).



          Bonus:



          2mm whitespace between line-blocks:



          makeatletter
          deflst@MSkipToFirst{%
          globaladvancelst@lineno@ne
          ifnum lst@lineno=lst@firstline
          deflst@next{lst@LeaveMode globallst@newlinesz@
          lst@OnceAtEOL globalletlst@OnceAtEOL@empty
          ifnum c@lstnumber>0
          vspace{2 mm}
          fi
          lst@InitLstNumber % Added to work with modified lsthk@PreInit.
          lsthk@InitVarsBOL
          c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
          lst@BOLGobble}%
          expandafterlst@next
          fi}
          makeatother





          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',
            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%2f110187%2flistings-line-numbers-that-match-the-linerange-specification%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








            up vote
            7
            down vote



            accepted










            My previous solution had problems with the numbering of blank lines. I have added a new solution and kept the old one further down.



            New Solution



            What one really needs to do is reset the counter lstnumber at the beginning of each range. To achieve this one needs to hook into an internal command lst@SkipToFirst. One approach is to define a new key matchrangestart initialised as false via



            lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}


            and then add the following test at the beginning of lst@SkiptoFirst:



            lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi


            This the general way listings set-up keys with true/false values and how it handles the counter lstnumber. This code says if the matchrangestart key is true then when skipping forward to the beginning of a rang set lstnumber to one less than the first line number of the range. When the line gets printed this number is then incremented before being printed so we get the desired output.



            New sample output



            documentclass{article}

            usepackage{filecontents,listings}
            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3


            Line 6
            Line 7

            Line 9
            Line 10
            end{filecontents*}

            makeatletter
            lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}
            deflst@SkipToFirst{%
            lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi
            ifnum lst@lineno<lst@firstline
            deflst@next{lst@BeginDropInputlst@Pmode
            lst@Let{13}lst@MSkipToFirst
            lst@Let{10}lst@MSkipToFirst}%
            expandafterlst@next
            else
            expandafterlst@BOLGobble
            fi}
            makeatother

            begin{document}
            begin{minipage}{0.45linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}

            lstinputlisting[linerange={2-6}]{mycode.txt}

            lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.45linewidth}
            Literal
            lstset{numbers=left,matchrangestart=t}
            lstinputlisting[linerange={1-3}]{mycode.txt}

            lstinputlisting[linerange={2-6}]{mycode.txt}

            lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
            end{minipage}

            end{document}


            Old Solution



            This places wrong numbers on blank lines



            The listings package has an internal plain counter lst@lineno holding the line number you are after. Now the numbers=left option, runs the code



            deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelstnumber}kernlst@numbersep}}


            so all we need to do is take this code and replace the printed latex counter thelstnumber by thelist@lineno:



            Sample output



            documentclass{article}

            usepackage{filecontents,listings}
            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3
            Line 4
            Line 5
            end{filecontents*}

            makeatletter
            deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelst@lineno}kernlst@numbersep}}
            makeatother

            begin{document}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{document}


            The above coding sets this style globally. If you wish to have local switches then you can extend the listings definition of the numbers option, adding a leftliteral type as follows:



            documentclass{article}

            usepackage{filecontents,listings}

            makeatletter
            lst@Key{numbers}{none}{%
            letlst@PlaceNumber@empty
            lstKV@SwitchCases{#1}%
            {none&\%
            left&deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelstnumber}kernlst@numbersep}}\%
            leftliteral&deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelst@lineno}kernlst@numbersep}}\%
            right&deflst@PlaceNumber{rlap{normalfont
            kernlinewidth kernlst@numbersep
            lst@numberstyle{thelstnumber}}}%
            }{PackageError{Listings}{Numbers #1 unknown}@ehc}}
            makeatother

            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3
            Line 4
            Line 5
            end{filecontents*}

            begin{document}
            begin{minipage}{0.3linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.3linewidth}
            Literal
            lstset{numbers=leftliteral}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.3linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            end{document}


            Sample switch






            share|improve this answer























            • Well, that was easy! :) Thanks Andrew.
              – Werner
              Apr 23 '13 at 14:16










            • This seems to overwrite other listingsoptions set by lstset like showlines=true– is there a way to keep those as well?
              – k.stm
              Jan 12 '14 at 0:05










            • @K.Stm. It is not overwriting these options, but there is interaction with them that one may not wish. The problem is that different options reference different counters.
              – Andrew Swann
              Jan 12 '14 at 14:15










            • @AndrewSwann True. I’m having problems with showlines that are unrelated. However, the solution here doesn’t go nicely with the lstlinebgrd package. If one wants to keep background colors, one has to imitate its implementation and add lst@linebgrd between the last two closing curly braces in the definitions of the key cases left, leftliteral, right (and rightliteral if one has defined it), e.g. …kernlst@numbersep}lst@linebgrd}\%.
              – k.stm
              Jan 12 '14 at 14:19












            • @K.Stm. Not surprising; that package is also changing the same command. Your code suggestion seems to be the right sort approach. If you have open problems, you should ask a separate question on this site.
              – Andrew Swann
              Jan 12 '14 at 15:06















            up vote
            7
            down vote



            accepted










            My previous solution had problems with the numbering of blank lines. I have added a new solution and kept the old one further down.



            New Solution



            What one really needs to do is reset the counter lstnumber at the beginning of each range. To achieve this one needs to hook into an internal command lst@SkipToFirst. One approach is to define a new key matchrangestart initialised as false via



            lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}


            and then add the following test at the beginning of lst@SkiptoFirst:



            lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi


            This the general way listings set-up keys with true/false values and how it handles the counter lstnumber. This code says if the matchrangestart key is true then when skipping forward to the beginning of a rang set lstnumber to one less than the first line number of the range. When the line gets printed this number is then incremented before being printed so we get the desired output.



            New sample output



            documentclass{article}

            usepackage{filecontents,listings}
            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3


            Line 6
            Line 7

            Line 9
            Line 10
            end{filecontents*}

            makeatletter
            lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}
            deflst@SkipToFirst{%
            lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi
            ifnum lst@lineno<lst@firstline
            deflst@next{lst@BeginDropInputlst@Pmode
            lst@Let{13}lst@MSkipToFirst
            lst@Let{10}lst@MSkipToFirst}%
            expandafterlst@next
            else
            expandafterlst@BOLGobble
            fi}
            makeatother

            begin{document}
            begin{minipage}{0.45linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}

            lstinputlisting[linerange={2-6}]{mycode.txt}

            lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.45linewidth}
            Literal
            lstset{numbers=left,matchrangestart=t}
            lstinputlisting[linerange={1-3}]{mycode.txt}

            lstinputlisting[linerange={2-6}]{mycode.txt}

            lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
            end{minipage}

            end{document}


            Old Solution



            This places wrong numbers on blank lines



            The listings package has an internal plain counter lst@lineno holding the line number you are after. Now the numbers=left option, runs the code



            deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelstnumber}kernlst@numbersep}}


            so all we need to do is take this code and replace the printed latex counter thelstnumber by thelist@lineno:



            Sample output



            documentclass{article}

            usepackage{filecontents,listings}
            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3
            Line 4
            Line 5
            end{filecontents*}

            makeatletter
            deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelst@lineno}kernlst@numbersep}}
            makeatother

            begin{document}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{document}


            The above coding sets this style globally. If you wish to have local switches then you can extend the listings definition of the numbers option, adding a leftliteral type as follows:



            documentclass{article}

            usepackage{filecontents,listings}

            makeatletter
            lst@Key{numbers}{none}{%
            letlst@PlaceNumber@empty
            lstKV@SwitchCases{#1}%
            {none&\%
            left&deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelstnumber}kernlst@numbersep}}\%
            leftliteral&deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelst@lineno}kernlst@numbersep}}\%
            right&deflst@PlaceNumber{rlap{normalfont
            kernlinewidth kernlst@numbersep
            lst@numberstyle{thelstnumber}}}%
            }{PackageError{Listings}{Numbers #1 unknown}@ehc}}
            makeatother

            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3
            Line 4
            Line 5
            end{filecontents*}

            begin{document}
            begin{minipage}{0.3linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.3linewidth}
            Literal
            lstset{numbers=leftliteral}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.3linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            end{document}


            Sample switch






            share|improve this answer























            • Well, that was easy! :) Thanks Andrew.
              – Werner
              Apr 23 '13 at 14:16










            • This seems to overwrite other listingsoptions set by lstset like showlines=true– is there a way to keep those as well?
              – k.stm
              Jan 12 '14 at 0:05










            • @K.Stm. It is not overwriting these options, but there is interaction with them that one may not wish. The problem is that different options reference different counters.
              – Andrew Swann
              Jan 12 '14 at 14:15










            • @AndrewSwann True. I’m having problems with showlines that are unrelated. However, the solution here doesn’t go nicely with the lstlinebgrd package. If one wants to keep background colors, one has to imitate its implementation and add lst@linebgrd between the last two closing curly braces in the definitions of the key cases left, leftliteral, right (and rightliteral if one has defined it), e.g. …kernlst@numbersep}lst@linebgrd}\%.
              – k.stm
              Jan 12 '14 at 14:19












            • @K.Stm. Not surprising; that package is also changing the same command. Your code suggestion seems to be the right sort approach. If you have open problems, you should ask a separate question on this site.
              – Andrew Swann
              Jan 12 '14 at 15:06













            up vote
            7
            down vote



            accepted







            up vote
            7
            down vote



            accepted






            My previous solution had problems with the numbering of blank lines. I have added a new solution and kept the old one further down.



            New Solution



            What one really needs to do is reset the counter lstnumber at the beginning of each range. To achieve this one needs to hook into an internal command lst@SkipToFirst. One approach is to define a new key matchrangestart initialised as false via



            lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}


            and then add the following test at the beginning of lst@SkiptoFirst:



            lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi


            This the general way listings set-up keys with true/false values and how it handles the counter lstnumber. This code says if the matchrangestart key is true then when skipping forward to the beginning of a rang set lstnumber to one less than the first line number of the range. When the line gets printed this number is then incremented before being printed so we get the desired output.



            New sample output



            documentclass{article}

            usepackage{filecontents,listings}
            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3


            Line 6
            Line 7

            Line 9
            Line 10
            end{filecontents*}

            makeatletter
            lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}
            deflst@SkipToFirst{%
            lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi
            ifnum lst@lineno<lst@firstline
            deflst@next{lst@BeginDropInputlst@Pmode
            lst@Let{13}lst@MSkipToFirst
            lst@Let{10}lst@MSkipToFirst}%
            expandafterlst@next
            else
            expandafterlst@BOLGobble
            fi}
            makeatother

            begin{document}
            begin{minipage}{0.45linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}

            lstinputlisting[linerange={2-6}]{mycode.txt}

            lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.45linewidth}
            Literal
            lstset{numbers=left,matchrangestart=t}
            lstinputlisting[linerange={1-3}]{mycode.txt}

            lstinputlisting[linerange={2-6}]{mycode.txt}

            lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
            end{minipage}

            end{document}


            Old Solution



            This places wrong numbers on blank lines



            The listings package has an internal plain counter lst@lineno holding the line number you are after. Now the numbers=left option, runs the code



            deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelstnumber}kernlst@numbersep}}


            so all we need to do is take this code and replace the printed latex counter thelstnumber by thelist@lineno:



            Sample output



            documentclass{article}

            usepackage{filecontents,listings}
            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3
            Line 4
            Line 5
            end{filecontents*}

            makeatletter
            deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelst@lineno}kernlst@numbersep}}
            makeatother

            begin{document}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{document}


            The above coding sets this style globally. If you wish to have local switches then you can extend the listings definition of the numbers option, adding a leftliteral type as follows:



            documentclass{article}

            usepackage{filecontents,listings}

            makeatletter
            lst@Key{numbers}{none}{%
            letlst@PlaceNumber@empty
            lstKV@SwitchCases{#1}%
            {none&\%
            left&deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelstnumber}kernlst@numbersep}}\%
            leftliteral&deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelst@lineno}kernlst@numbersep}}\%
            right&deflst@PlaceNumber{rlap{normalfont
            kernlinewidth kernlst@numbersep
            lst@numberstyle{thelstnumber}}}%
            }{PackageError{Listings}{Numbers #1 unknown}@ehc}}
            makeatother

            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3
            Line 4
            Line 5
            end{filecontents*}

            begin{document}
            begin{minipage}{0.3linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.3linewidth}
            Literal
            lstset{numbers=leftliteral}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.3linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            end{document}


            Sample switch






            share|improve this answer














            My previous solution had problems with the numbering of blank lines. I have added a new solution and kept the old one further down.



            New Solution



            What one really needs to do is reset the counter lstnumber at the beginning of each range. To achieve this one needs to hook into an internal command lst@SkipToFirst. One approach is to define a new key matchrangestart initialised as false via



            lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}


            and then add the following test at the beginning of lst@SkiptoFirst:



            lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi


            This the general way listings set-up keys with true/false values and how it handles the counter lstnumber. This code says if the matchrangestart key is true then when skipping forward to the beginning of a rang set lstnumber to one less than the first line number of the range. When the line gets printed this number is then incremented before being printed so we get the desired output.



            New sample output



            documentclass{article}

            usepackage{filecontents,listings}
            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3


            Line 6
            Line 7

            Line 9
            Line 10
            end{filecontents*}

            makeatletter
            lst@Key{matchrangestart}{f}{lstKV@SetIf{#1}lst@ifmatchrangestart}
            deflst@SkipToFirst{%
            lst@ifmatchrangestartc@lstnumber=numexpr-1+lst@firstlinefi
            ifnum lst@lineno<lst@firstline
            deflst@next{lst@BeginDropInputlst@Pmode
            lst@Let{13}lst@MSkipToFirst
            lst@Let{10}lst@MSkipToFirst}%
            expandafterlst@next
            else
            expandafterlst@BOLGobble
            fi}
            makeatother

            begin{document}
            begin{minipage}{0.45linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}

            lstinputlisting[linerange={2-6}]{mycode.txt}

            lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.45linewidth}
            Literal
            lstset{numbers=left,matchrangestart=t}
            lstinputlisting[linerange={1-3}]{mycode.txt}

            lstinputlisting[linerange={2-6}]{mycode.txt}

            lstinputlisting[linerange={2-2,4-6,8-9}]{mycode.txt}
            end{minipage}

            end{document}


            Old Solution



            This places wrong numbers on blank lines



            The listings package has an internal plain counter lst@lineno holding the line number you are after. Now the numbers=left option, runs the code



            deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelstnumber}kernlst@numbersep}}


            so all we need to do is take this code and replace the printed latex counter thelstnumber by thelist@lineno:



            Sample output



            documentclass{article}

            usepackage{filecontents,listings}
            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3
            Line 4
            Line 5
            end{filecontents*}

            makeatletter
            deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelst@lineno}kernlst@numbersep}}
            makeatother

            begin{document}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{document}


            The above coding sets this style globally. If you wish to have local switches then you can extend the listings definition of the numbers option, adding a leftliteral type as follows:



            documentclass{article}

            usepackage{filecontents,listings}

            makeatletter
            lst@Key{numbers}{none}{%
            letlst@PlaceNumber@empty
            lstKV@SwitchCases{#1}%
            {none&\%
            left&deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelstnumber}kernlst@numbersep}}\%
            leftliteral&deflst@PlaceNumber{llap{normalfont
            lst@numberstyle{thelst@lineno}kernlst@numbersep}}\%
            right&deflst@PlaceNumber{rlap{normalfont
            kernlinewidth kernlst@numbersep
            lst@numberstyle{thelstnumber}}}%
            }{PackageError{Listings}{Numbers #1 unknown}@ehc}}
            makeatother

            lstset{
            basicstyle=ttfamilyfootnotesize
            }

            begin{filecontents*}{mycode.txt}
            Line 1
            Line 2
            Line 3
            Line 4
            Line 5
            end{filecontents*}

            begin{document}
            begin{minipage}{0.3linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.3linewidth}
            Literal
            lstset{numbers=leftliteral}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            begin{minipage}{0.3linewidth}
            Standard
            lstset{numbers=left}
            lstinputlisting[linerange={1-3}]{mycode.txt}
            lstinputlisting[linerange={2-4}]{mycode.txt}
            lstinputlisting[linerange={2-2,4-5}]{mycode.txt}
            end{minipage}
            end{document}


            Sample switch







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 27 '14 at 22:16

























            answered Apr 23 '13 at 8:39









            Andrew Swann

            75.8k9125321




            75.8k9125321












            • Well, that was easy! :) Thanks Andrew.
              – Werner
              Apr 23 '13 at 14:16










            • This seems to overwrite other listingsoptions set by lstset like showlines=true– is there a way to keep those as well?
              – k.stm
              Jan 12 '14 at 0:05










            • @K.Stm. It is not overwriting these options, but there is interaction with them that one may not wish. The problem is that different options reference different counters.
              – Andrew Swann
              Jan 12 '14 at 14:15










            • @AndrewSwann True. I’m having problems with showlines that are unrelated. However, the solution here doesn’t go nicely with the lstlinebgrd package. If one wants to keep background colors, one has to imitate its implementation and add lst@linebgrd between the last two closing curly braces in the definitions of the key cases left, leftliteral, right (and rightliteral if one has defined it), e.g. …kernlst@numbersep}lst@linebgrd}\%.
              – k.stm
              Jan 12 '14 at 14:19












            • @K.Stm. Not surprising; that package is also changing the same command. Your code suggestion seems to be the right sort approach. If you have open problems, you should ask a separate question on this site.
              – Andrew Swann
              Jan 12 '14 at 15:06


















            • Well, that was easy! :) Thanks Andrew.
              – Werner
              Apr 23 '13 at 14:16










            • This seems to overwrite other listingsoptions set by lstset like showlines=true– is there a way to keep those as well?
              – k.stm
              Jan 12 '14 at 0:05










            • @K.Stm. It is not overwriting these options, but there is interaction with them that one may not wish. The problem is that different options reference different counters.
              – Andrew Swann
              Jan 12 '14 at 14:15










            • @AndrewSwann True. I’m having problems with showlines that are unrelated. However, the solution here doesn’t go nicely with the lstlinebgrd package. If one wants to keep background colors, one has to imitate its implementation and add lst@linebgrd between the last two closing curly braces in the definitions of the key cases left, leftliteral, right (and rightliteral if one has defined it), e.g. …kernlst@numbersep}lst@linebgrd}\%.
              – k.stm
              Jan 12 '14 at 14:19












            • @K.Stm. Not surprising; that package is also changing the same command. Your code suggestion seems to be the right sort approach. If you have open problems, you should ask a separate question on this site.
              – Andrew Swann
              Jan 12 '14 at 15:06
















            Well, that was easy! :) Thanks Andrew.
            – Werner
            Apr 23 '13 at 14:16




            Well, that was easy! :) Thanks Andrew.
            – Werner
            Apr 23 '13 at 14:16












            This seems to overwrite other listingsoptions set by lstset like showlines=true– is there a way to keep those as well?
            – k.stm
            Jan 12 '14 at 0:05




            This seems to overwrite other listingsoptions set by lstset like showlines=true– is there a way to keep those as well?
            – k.stm
            Jan 12 '14 at 0:05












            @K.Stm. It is not overwriting these options, but there is interaction with them that one may not wish. The problem is that different options reference different counters.
            – Andrew Swann
            Jan 12 '14 at 14:15




            @K.Stm. It is not overwriting these options, but there is interaction with them that one may not wish. The problem is that different options reference different counters.
            – Andrew Swann
            Jan 12 '14 at 14:15












            @AndrewSwann True. I’m having problems with showlines that are unrelated. However, the solution here doesn’t go nicely with the lstlinebgrd package. If one wants to keep background colors, one has to imitate its implementation and add lst@linebgrd between the last two closing curly braces in the definitions of the key cases left, leftliteral, right (and rightliteral if one has defined it), e.g. …kernlst@numbersep}lst@linebgrd}\%.
            – k.stm
            Jan 12 '14 at 14:19






            @AndrewSwann True. I’m having problems with showlines that are unrelated. However, the solution here doesn’t go nicely with the lstlinebgrd package. If one wants to keep background colors, one has to imitate its implementation and add lst@linebgrd between the last two closing curly braces in the definitions of the key cases left, leftliteral, right (and rightliteral if one has defined it), e.g. …kernlst@numbersep}lst@linebgrd}\%.
            – k.stm
            Jan 12 '14 at 14:19














            @K.Stm. Not surprising; that package is also changing the same command. Your code suggestion seems to be the right sort approach. If you have open problems, you should ask a separate question on this site.
            – Andrew Swann
            Jan 12 '14 at 15:06




            @K.Stm. Not surprising; that package is also changing the same command. Your code suggestion seems to be the right sort approach. If you have open problems, you should ask a separate question on this site.
            – Andrew Swann
            Jan 12 '14 at 15:06










            up vote
            2
            down vote













            Based upon Andrew's answer I found this to be a better match to the problem:



            makeatletter
            deflst@MSkipToFirst{%
            globaladvancelst@lineno@ne
            ifnum lst@lineno=lst@firstline
            deflst@next{lst@LeaveMode globallst@newlinesz@
            lst@OnceAtEOL globalletlst@OnceAtEOL@empty
            lst@InitLstNumber % Added to work with modified lsthk@PreInit.
            lsthk@InitVarsBOL
            c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
            lst@BOLGobble}%
            expandafterlst@next
            fi}
            makeatother


            The advantage over his answer is that it supports multi-range constructs correctly without the need to introduce a key. This is done by overriding the method which actually takes care of range-skipping and already containing a hook for the first line of a new range.



            Tested against listings 1.6 (2015/06/04). As always when overriding a function pay attention to changes of the package's source (shouldn't be to hard as only one line was added).



            Bonus:



            2mm whitespace between line-blocks:



            makeatletter
            deflst@MSkipToFirst{%
            globaladvancelst@lineno@ne
            ifnum lst@lineno=lst@firstline
            deflst@next{lst@LeaveMode globallst@newlinesz@
            lst@OnceAtEOL globalletlst@OnceAtEOL@empty
            ifnum c@lstnumber>0
            vspace{2 mm}
            fi
            lst@InitLstNumber % Added to work with modified lsthk@PreInit.
            lsthk@InitVarsBOL
            c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
            lst@BOLGobble}%
            expandafterlst@next
            fi}
            makeatother





            share|improve this answer



























              up vote
              2
              down vote













              Based upon Andrew's answer I found this to be a better match to the problem:



              makeatletter
              deflst@MSkipToFirst{%
              globaladvancelst@lineno@ne
              ifnum lst@lineno=lst@firstline
              deflst@next{lst@LeaveMode globallst@newlinesz@
              lst@OnceAtEOL globalletlst@OnceAtEOL@empty
              lst@InitLstNumber % Added to work with modified lsthk@PreInit.
              lsthk@InitVarsBOL
              c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
              lst@BOLGobble}%
              expandafterlst@next
              fi}
              makeatother


              The advantage over his answer is that it supports multi-range constructs correctly without the need to introduce a key. This is done by overriding the method which actually takes care of range-skipping and already containing a hook for the first line of a new range.



              Tested against listings 1.6 (2015/06/04). As always when overriding a function pay attention to changes of the package's source (shouldn't be to hard as only one line was added).



              Bonus:



              2mm whitespace between line-blocks:



              makeatletter
              deflst@MSkipToFirst{%
              globaladvancelst@lineno@ne
              ifnum lst@lineno=lst@firstline
              deflst@next{lst@LeaveMode globallst@newlinesz@
              lst@OnceAtEOL globalletlst@OnceAtEOL@empty
              ifnum c@lstnumber>0
              vspace{2 mm}
              fi
              lst@InitLstNumber % Added to work with modified lsthk@PreInit.
              lsthk@InitVarsBOL
              c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
              lst@BOLGobble}%
              expandafterlst@next
              fi}
              makeatother





              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                Based upon Andrew's answer I found this to be a better match to the problem:



                makeatletter
                deflst@MSkipToFirst{%
                globaladvancelst@lineno@ne
                ifnum lst@lineno=lst@firstline
                deflst@next{lst@LeaveMode globallst@newlinesz@
                lst@OnceAtEOL globalletlst@OnceAtEOL@empty
                lst@InitLstNumber % Added to work with modified lsthk@PreInit.
                lsthk@InitVarsBOL
                c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
                lst@BOLGobble}%
                expandafterlst@next
                fi}
                makeatother


                The advantage over his answer is that it supports multi-range constructs correctly without the need to introduce a key. This is done by overriding the method which actually takes care of range-skipping and already containing a hook for the first line of a new range.



                Tested against listings 1.6 (2015/06/04). As always when overriding a function pay attention to changes of the package's source (shouldn't be to hard as only one line was added).



                Bonus:



                2mm whitespace between line-blocks:



                makeatletter
                deflst@MSkipToFirst{%
                globaladvancelst@lineno@ne
                ifnum lst@lineno=lst@firstline
                deflst@next{lst@LeaveMode globallst@newlinesz@
                lst@OnceAtEOL globalletlst@OnceAtEOL@empty
                ifnum c@lstnumber>0
                vspace{2 mm}
                fi
                lst@InitLstNumber % Added to work with modified lsthk@PreInit.
                lsthk@InitVarsBOL
                c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
                lst@BOLGobble}%
                expandafterlst@next
                fi}
                makeatother





                share|improve this answer














                Based upon Andrew's answer I found this to be a better match to the problem:



                makeatletter
                deflst@MSkipToFirst{%
                globaladvancelst@lineno@ne
                ifnum lst@lineno=lst@firstline
                deflst@next{lst@LeaveMode globallst@newlinesz@
                lst@OnceAtEOL globalletlst@OnceAtEOL@empty
                lst@InitLstNumber % Added to work with modified lsthk@PreInit.
                lsthk@InitVarsBOL
                c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
                lst@BOLGobble}%
                expandafterlst@next
                fi}
                makeatother


                The advantage over his answer is that it supports multi-range constructs correctly without the need to introduce a key. This is done by overriding the method which actually takes care of range-skipping and already containing a hook for the first line of a new range.



                Tested against listings 1.6 (2015/06/04). As always when overriding a function pay attention to changes of the package's source (shouldn't be to hard as only one line was added).



                Bonus:



                2mm whitespace between line-blocks:



                makeatletter
                deflst@MSkipToFirst{%
                globaladvancelst@lineno@ne
                ifnum lst@lineno=lst@firstline
                deflst@next{lst@LeaveMode globallst@newlinesz@
                lst@OnceAtEOL globalletlst@OnceAtEOL@empty
                ifnum c@lstnumber>0
                vspace{2 mm}
                fi
                lst@InitLstNumber % Added to work with modified lsthk@PreInit.
                lsthk@InitVarsBOL
                c@lstnumber=numexpr-1+lst@lineno % this enforces the displayed line numbers to always be the input line numbers
                lst@BOLGobble}%
                expandafterlst@next
                fi}
                makeatother






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 13 '17 at 12:35









                Community

                1




                1










                answered Mar 4 '16 at 19:20









                TheConstructor

                1213




                1213






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f110187%2flistings-line-numbers-that-match-the-linerange-specification%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

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?