如何用他们的定义替换所有出现的用户定义的乳胶宏?
例如,给定这个文件
旧的.tex
\newcommand{\blah}[2]{#1 \to #2}
...
foo \blah{egg}{spam} bar
...
如何以自动方式生成以下文件
新的.tex
...
foo egg \to spam bar
...
我可以使用 Latex 或 tex 引擎本身来执行此操作,而不是用 perl 重新实现乳胶宏逻辑吗?
如何用他们的定义替换所有出现的用户定义的乳胶宏?
例如,给定这个文件
旧的.tex
\newcommand{\blah}[2]{#1 \to #2}
...
foo \blah{egg}{spam} bar
...
如何以自动方式生成以下文件
新的.tex
...
foo egg \to spam bar
...
我可以使用 Latex 或 tex 引擎本身来执行此操作,而不是用 perl 重新实现乳胶宏逻辑吗?
瞧 http://www.ctan.org/tex-archive/support/de-macro
这是一个 Python,它:
[...] 将扩展在 (re)newcommand 或 (re)newenvironment 命令、文档内或文档的“私有”包文件中定义的宏。
考虑使用带有 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,这根本不是一个无法克服的问题。
我希望这会有所帮助。
我在 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