Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用定义替换latex宏(使用latex)_Latex_Substitution - Fatal编程技术网

如何用定义替换latex宏(使用latex)

如何用定义替换latex宏(使用latex),latex,substitution,Latex,Substitution,如何用用户定义的latex宏的定义替换所有出现的用户定义的latex宏 例如,给定此文件 old.tex \newcommand{\blah}[2]{#1 \to #2} ... foo \blah{egg}{spam} bar ... 如何自动生成下面的文件 new.tex ... foo egg \to spam bar ... 我可以使用latex或tex引擎本身来完成这项工作,而不是用perl重新实现latex宏逻辑吗?从未见过这样做,但有两个半生不熟的想法: 如果希望以内联方式展开

如何用用户定义的latex宏的定义替换所有出现的用户定义的latex宏

例如,给定此文件

old.tex

\newcommand{\blah}[2]{#1 \to #2}
...
foo \blah{egg}{spam} bar
...
如何自动生成下面的文件

new.tex

...
foo egg \to spam bar
...

我可以使用latex或tex引擎本身来完成这项工作,而不是用perl重新实现latex宏逻辑吗?

从未见过这样做,但有两个半生不熟的想法:

  • 如果希望以内联方式展开所有这些宏的原因是为了调试,则在文档中设置
    \tracingmacros=1
    将展开所有宏,但输出将转到日志文件

  • 归档文件提供了一个可用于在定义(但不是newcommand)中进行内联扩展的工具,但我不知道您是否可以看一看,看看修改以执行\newcommand而不是\def的内联扩展会有多痛苦


  • 考虑使用模板引擎,例如Python

    您可能希望更改默认的{%、{等语法,以使其与LaTeX自己的语法更兼容。例如:

    env = jinja2.Environment(
          loader=jinja2.FileSystemLoader( JINJA_DIRS ),
          comment_start_string='["', # don't conflict with e.g. {#1
          comment_end_string = '"]',
          block_start_string = '[%',
          block_end_string = '%]',
          variable_start_string = '[=',
          variable_end_string = ']',
          autoescape=True,
          finalize=_jinja2_finalize_callback, # make a function that escapes TeX
          )
    
    template = env.get_template( self.template )
    
    tex = template.render( content ) 
    
    除了传递到模板环境的函数外,Jinja2还支持。例如,您的上述代码应按预期工作,如下所示:

    [% macro blah(egg, spam) -%]
    foo [=egg] \to [=spam] bar
    [%- endmacro %]
    
    [= blah("chicken","pork") ]
    % substitutes with "foo chicken \to pork"
    
    我不确定您的目标是什么,这需要一些工作,但如果您熟悉Python,这并不是一个无法克服的问题

    我希望这会有帮助。

    这是一条Python:

    […]将在(重新)新命令或(重新)新命令、文件内或文件的“Private”包文件中定义扩展宏


    我在2007年写了一个C程序来扩展\newcommand:-我想在发布这个问题时它还没有被编入索引。现在向仍然在寻找这样一个东西并找到这个页面的人提及它

    从代码中

    // FOR DOCUMENTATION, SEE MY BLOG POST:
    //    http://techennui.blogspot.com/2007/11/quick-hack-17-in-series-of-42-inlining.html
    
    // Expands LaTeX \newcommand macros to allow submission of documents
    // to print services which do not allow user-defined macros.
    
    // Valid input formats are:
    // \newcommand{\whatever}{Replacement text}
    // \newcommand{\whatever}[2]{Expand #1 and #2 but not \#1 or even $\#1$}
    // - anything else ought to be passed through verbatim; if an insurmountable
    // error is detected, the program exits with a non-0 return code.
    
    // The purpose of this utility is similar to:
    //    http://winedt.org/Macros/LaTeX/uncommand.php
    // which I wasn't aware of when I wrote it.  Though I would like to see how
    // well that program handles the test input file, to see if it does the
    // right thing with some of the more complex definitions :-)
    //
    // See also http://texcatalogue.sarovar.org/entries/de-macro.html
    // and http://www.mackichan.com/index.html?techtalk/685.htm~mainFrame
    

    有趣的问题。我认为这真的很难,如果不是不可能的话。一个合适的TeX脚本必须解析每行中的每一个标记,并检查它是否是一个用户定义的命令,我认为这相当复杂。文档中的catcode更改之类的事情会使它更加复杂。我建议您尝试找到一个完全不同的方法solution.TeX可以从输入文件中排版DVI或PDF输出文件,但其他任何内容都非常复杂。您最好使用perl或您选择的语言来解析.TeX文件并替换宏。stackoverflow问题的答案是:很好。(这是一个Python脚本,但仍然可能对阅读此问题的人有所帮助。)地址似乎已经改变:如果我能让它工作,我将永远爱你,4年前的陌生人。2020年,仍然工作:)尽管它需要Python 2