代码之家  ›  专栏  ›  技术社区  ›  phimuemue

LaTeX:Lstlisting自动识别代码段

  •  5
  • phimuemue  · 技术社区  · 14 年前

    为了包含源代码并使其保持最新,我在文档中执行以下操作:

    \lstinputlisting[firstline=200, lastline=210]{../src/source.c)
    

    ../src/source.c 在我的文件里。

    但是,如果在第200行之前添加一些行,这意味着第200行“向下游移一些行”,因此我必须调整此项以获得原始功能。

    所以我的问题是:是否有人知道如何动态地 lstinputlisting

    我想象如下:我在C源代码中添加了一些特殊的注释,这些注释将被 lstinputlisting公司 ,例如。

    /// lstinputlisting "myfunc" BEGIN
    int myFunction(int x){
      return x+2;
    }
    /// lstinputlisting "myfunc" END
    

    lstlisting BEGIN 以及 END

    4 回复  |  直到 14 年前
        1
  •  14
  •   Calaf    11 年前

    我将在你的帖子发表几个月后回复你,但是我下面描述的lstlistings的特性已经在这个包中存在了好几年了。

    要查找的关键字是选项 linerange 为了方便起见, rangeprefix rangesuffix .

    这是一个完整的例子。

    \documentclass{article}
    \usepackage{fullpage,times,listings}
    \lstloadlanguages{C++}
    
    \lstset{language=C++,keywordstyle=\bfseries,commentstyle=\itshape,
      rangeprefix=//----------------,rangesuffix=----------------,
      includerangemarker=false,columns=spaceflexible}
    
    \begin{document}
    We first show the main function.
    \lstinputlisting[linerange=main0-main1]{code.cpp}
    Then we show the implementation.
    \lstinputlisting[linerange=fact0-fact1]{code.cpp}
    \end{document}
    

    然后将以下内容保存在code.cpp中:

    #include <cassert>
    
    //----------------fact0----------------
    // A recursive implementation of factorial
    int factorial(int i)
    {
        if(i)
            return i * factorial(i-1);
        else
            return 1;
    }
    //----------------fact1----------------
    
    //----------------main0----------------
    // Test the implementation.
    int main()
    {
        assert(factorial(5) == 120);
    }
    //----------------main1----------------
    

    这是一个很好的解决方案,因为人们不可避免地要编辑代码,然后在整个TeX文件中更新行号会变得很乏味。使用符号可以解决这个问题,但它也会在代码本身留下痕迹,即如果行数发生变化,或者宽度变化过大,则需要确认排版输出看起来仍然合理。

    最后,在编辑代码之后,只有在标记块中插入/删除时,才需要重新排版latex文件。

        2
  •  1
  •   kahen    14 年前

    唯一合理的方法是创建一个makefile,并让它负责生成正确的输出。

    假设sourcefile.c在./src中,而LaTeX文件在./tex中,那么./tex/Makefile可能如下所示:

    doc.tex: sourcefile.grep
            <command to compile doc.tex>
    sourcefile.grep: 
            <command to grep/whatever to dump from 
            ../src/sourcefile.c to ./tex/sourcefile.grep>
    

    然后doc.tex中的lstlistings将指向./src/sourcefile.grep

        3
  •  1
  •   Grzegorz Gierlik    14 年前

    使用起来并不容易 #include

    它不是完美的,但足够好,解决方案。

    下面是一个例子(无法编译,我上次在 C 5年前):

    主要 C类 程序:

        #include <stdio.h>
    
        //function included from separate file -- will be included in LaTeX too
        #include "fun_x.c"         
    
        int main() 
        {
          int d = 0;
          printf("%d", fun_x(d));
    
        //code included from separate file -- will be included in LaTeX too
        #include "calc.c"
    
          return 0;
        }
    

    fun_x.c

    int fun_x(int c) 
    {
      c++;
      return c;
    }
    

    calc.c

    d = 99;
    d = fun_x(d);
    

    \begin{document}
    ...
    
    \lstinputlisting{../src/fun_x.c)
    
    ...
    
    \lstinputlisting{../src/calc.c)
    
    ...
    
    \end{document}
    
        4
  •  1
  •   Community frankie liuzzi    7 年前

    像这样的事情 discussed on the TeX SE 不久前,一个答案用了一个包裹 catchfilebetweentags . 这并不能直接解决您的问题,但是也许您可以使用它来提供您想要的片段 \lstlistings