如何将所有用户定义的乳胶宏替换为其定义?
例如,给定此文件
old.tex
\newcommand{\blah}[2]{#1 \to #2}
...
foo \blah{egg}{spam} bar
...
如何以自动方式生成以下文件
new.tex
...
foo egg \to spam bar
...
不是用perl重新实现latex宏逻辑,我可以使用latex或tex引擎来实现吗?
答案 0 :(得分:8)
VOILÀ http://www.ctan.org/tex-archive/support/de-macro
这是一个Python:
[...]将展开(重新)新命令或(重新)新环境命令,文档或文档的“私有”包文件中定义的宏。
答案 1 :(得分:1)
从未见过这样做,但有两个半生不熟的想法:
答案 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