如何用定义替换latex宏(使用latex)
如何用用户定义的latex宏的定义替换所有出现的用户定义的latex宏 例如,给定此文件 old.tex如何用定义替换latex宏(使用latex),latex,substitution,Latex,Substitution,如何用用户定义的latex宏的定义替换所有出现的用户定义的latex宏 例如,给定此文件 old.tex \newcommand{\blah}[2]{#1 \to #2} ... foo \blah{egg}{spam} bar ... 如何自动生成下面的文件 new.tex ... foo egg \to spam bar ... 我可以使用latex或tex引擎本身来完成这项工作,而不是用perl重新实现latex宏逻辑吗?从未见过这样做,但有两个半生不熟的想法: 如果希望以内联方式展开
\newcommand{\blah}[2]{#1 \to #2}
...
foo \blah{egg}{spam} bar
...
如何自动生成下面的文件
new.tex
...
foo egg \to spam bar
...
我可以使用latex或tex引擎本身来完成这项工作,而不是用perl重新实现latex宏逻辑吗?从未见过这样做,但有两个半生不熟的想法:
\tracingmacros=1
将展开所有宏,但输出将转到日志文件考虑使用模板引擎,例如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还支持。例如,您的上述代码应按预期工作,如下所示:
[% macro blah(egg, spam) -%]
foo [=egg] \to [=spam] bar
[%- endmacro %]
[= blah("chicken","pork") ]
% substitutes with "foo chicken \to pork"
我不确定您的目标是什么,这需要一些工作,但如果您熟悉Python,这并不是一个无法克服的问题
我希望这会有帮助。瞧
这是一条Python:
[…]将在(重新)新命令或(重新)新命令、文件内或文件的“Private”包文件中定义扩展宏
我在2007年写了一个C程序来扩展\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
有趣的问题。我认为这真的很难,如果不是不可能的话。一个合适的TeX脚本必须解析每行中的每一个标记,并检查它是否是一个用户定义的命令,我认为这相当复杂。文档中的catcode更改之类的事情会使它更加复杂。我建议您尝试找到一个完全不同的方法solution.TeX可以从输入文件中排版DVI或PDF输出文件,但其他任何内容都非常复杂。您最好使用perl或您选择的语言来解析.TeX文件并替换宏。stackoverflow问题的答案是:很好。(这是一个Python脚本,但仍然可能对阅读此问题的人有所帮助。)地址似乎已经改变:如果我能让它工作,我将永远爱你,4年前的陌生人。2020年,仍然工作:)尽管它需要Python 2