【问题标题】:how to replace latex macros with their definitions (using latex)如何用定义替换乳胶宏(使用乳胶)
【发布时间】:2009-10-02 14:17:38
【问题描述】:

如何将所有出现的用户定义的 Latex 宏替换为其定义?

例如,给定这个文件

旧的.tex

\newcommand{\blah}[2]{#1 \to #2}
...
foo \blah{egg}{spam} bar
...

如何自动生成下面的文件

新的.tex

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

我可以使用latex 或tex 引擎本身来执行此操作,而不是用perl 重新实现latex 宏逻辑吗?

【问题讨论】:

  • 有趣的问题。我认为这真的很难,如果不是不可能的话。一个合适的 TeX 脚本必须解析每一行中的每个标记并检查它是否是用户定义的命令,我认为这很复杂。文档中的 catcode 更改之类的事情使情况更加复杂。我建议您尝试找到一个完全不同的解决方案。 TeX 非常适合从输入文件排版 DVI 或 PDF 输出文件,但其他任何事情都非常复杂。
  • 使用 perl 或您选择的语言来解析 .tex 文件并替换宏可能会更好。
  • This stackoverflow 问题有一个答案:tme

标签: latex substitution


【解决方案1】:

VOILÀ http://www.ctan.org/tex-archive/support/de-macro

这是一个 Python:

[...] 将扩展在 (re)newcommand 或 (re)newenvironment 命令、文档内或文档的“私有”包文件中定义的宏。

【讨论】:

  • 不错。 (这是一个 Python 脚本,但仍然可能对阅读此问题的人有所帮助。)
  • 如果我能完成这项工作,我会永远爱你,4 年前的陌生人。
  • 2020 并且仍在工作:) 尽管它需要 python2
【解决方案2】:

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

  1. 如果您想内联展开所有这些宏的原因是为了调试,那么在您的文档中设置\tracingmacros=1 将展开您的所有宏,但输出到一个日志文件。

  2. CTAN 存档提供了一个package,您可以使用它来内联定义中的扩展(但不是新命令),但我不知道您是否可以看看它可能会有多痛苦修改以执行 \newcommand 而不是 \def 的内联扩展。

【讨论】:

    【解决方案3】:

    考虑在 Python 中使用模板引擎,例如 Jinja2

    您可能希望更改默认的 {%、{{ 等语法,以使其与 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 还支持macros。例如,您的上述代码应按预期工作:

    [% macro blah(egg, spam) -%]
    foo [=egg] \to [=spam] bar
    [%- endmacro %]
    
    [= blah("chicken","pork") ]
    % substitutes with "foo chicken \to pork"
    

    我不确定你的目标是什么,这需要一些工作,但如果你熟悉 Python,这根本不是一个无法克服的问题。

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      我在 2007 年编写了一个 C 程序来扩展 \newcommand:http://www.gtoal.com/src/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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-29
        • 1970-01-01
        • 2011-02-08
        • 1970-01-01
        • 1970-01-01
        • 2015-01-20
        • 2010-10-20
        相关资源
        最近更新 更多