12

如何用他们的定义替换所有出现的用户定义的乳胶宏?

例如,给定这个文件

旧的.tex

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

如何以自动方式生成以下文件

新的.tex

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

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

4

4 回答 4

13

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

这是一个 Python,它:

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

于 2010-04-27T14:20:38.560 回答
1

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

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

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

于 2009-10-04T02:24:49.483 回答
1

考虑使用带有 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 还支持。例如,您的上述代码应按预期工作:

[% macro blah(egg, spam) -%]
foo [=egg] \to [=spam] bar
[%- endmacro %]

[= blah("chicken","pork") ]
% substitutes with "foo chicken \to pork"

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

我希望这会有所帮助。

于 2009-10-07T19:17:50.270 回答
1

我在 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
于 2018-12-12T02:11:42.397 回答