带有列表的行编号并排除范围标记

带有列表的行编号并排除范围标记

大家好,

我的问题非常类似https://stackoverflow.com/questions/2178301/linenumbering-with-listings-package-in-latex但我尝试对行范围标记执行相同的操作。问题如下:使用时,行号变为 2、6、11、16 等includerangemarker=false

这是一个最小工作示例:

\documentclass{scrreprt}
\usepackage{listings}

\lstset{
    rangebeginprefix=//\ begin:\ ,
    rangeendprefix=//\ end:\ ,
    includerangemarker=false, % this causes the problem
    numbers=left,
    stepnumber=5,    
    firstnumber=1,
    numberfirstline=true
}


\begin{document}                                                        
    \lstinputlisting[linerange=main-main]{./HelloWorld.java}
\end{document}

和 HelloWorld.java:

public class HelloWorld {
    // begin: main
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");      
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
    }
    // end: main
}

我怎样才能排除范围标记并仍然获得 1,5,10,15 等行的编号?

答案1

这似乎有效,但不能保证它不会破坏其他东西……

\lst@DefRangeB@listings.sty是where中极少数\lst@ifincluderangemarker ... \else ... \fi使用的地方之一。下面的代码添加\global\advance\c@lstnumber\m@ne错误的分支,即,如果为假,则将行号减少一includerangemarker

\documentclass{article}
\usepackage{listings}

\usepackage{etoolbox}
\makeatletter
\patchcmd\lst@DefRangeB@
  {\lst@InitLstNumber\fi}% search
  {\lst@InitLstNumber\global\advance\c@lstnumber\m@ne\fi}% replace
  {}% success
  {\err}% failure
\makeatother

\lstset{
  rangebeginprefix = //\ begin:\ ,
  rangeendprefix   = //\ end:\ ,
  includerangemarker = false , % this causes the problem
  numbers     = left ,
  stepnumber  = 5 , 
  firstnumber = 1 ,
  numberfirstline = true
}

% just to keep the example in one file:
\usepackage{filecontents}
\begin{filecontents*}{HelloWorld.java}
public class HelloWorld {
    // begin: main
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");      
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
    }
    // end: main
}
\end{filecontents*}

\begin{document}                                                        

\lstinputlisting[basicstyle=\ttfamily,linerange=main-main]{HelloWorld.java}

\end{document}

enter image description here

相关内容