如何用他们的定义替换乳胶宏(使用乳胶)

时间:2009-10-02 14:17:39

标签: latex substitution

如何将所有用户定义的乳胶宏替换为其定义?

例如,给定此文件

old.tex

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

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

new.tex

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

不是用perl重新实现latex宏逻辑,我可以使用latex或tex引擎来实现吗?

4 个答案:

答案 0 :(得分:8)

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

这是一个Python:

  

[...]将展开(重新)新命令或(重新)新环境命令,文档或文档的“私有”包文件中定义的宏。

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

考虑使用模板引擎,例如Jinja2和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还支持macros。例如,您的上述代码应按预期工作:

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

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

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

我希望有所帮助。

答案 3 :(得分:0)

我早在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
相关问题