将命令行参数传递给 LaTeX 文档
有时,我会定义如下新命令。
\newcommand{\comment}[1]{\textbf{#1}}
%\necommand{\comment}[1]{\emph{#1}}
上述命令使我能够一次性更改部分代码的样式。如果我想生成两种可能的样式,我必须编译 LaTeX 文档两次,每次修改源代码以启用所需的样式。
有没有办法避免这种情况下修改源代码?也就是说,我可以向 Latex 传递一些命令行参数,以便我可以根据该参数选择要使用的样式吗?
Sometimes, I define new commands such as the following.
\newcommand{\comment}[1]{\textbf{#1}}
%\necommand{\comment}[1]{\emph{#1}}
The above commands enable me to change the style of parts of my code all at once. If I want to generate both of the possible styles, I have to compile my LaTeX document two times each time modifying the source code to enable the desired style.
Is there a way to avoid the source code modification in such cases? That is, can I pass latex some command-line arguments so that I can choose which style to use based on that argument?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。三个选项:
1
在源文件中,编写
并编译 LaTeX 文档(“myfile.tex”) 2
或者
,
然后在源文件中
3
甚至
使用
哪个可能是最短的,尽管有点脆弱,因为你永远不知道当一个包可能在你背后定义
\myflag
时。Yes. Three options:
One
In your source file, write
and then compile the LaTeX document ("myfile.tex") as
Two
Alternatively,
and then have in the source
Three
Or even
with
which is probably the shortest, albeit slightly fragile because you never know when a package might define
\myflag
behind your back.当您需要相当灵活的一次性选项(例如更改简历上的职位线)时,您应该使用威尔的方法。如果 otoh 您正在生成与 & 相同的选项选择如果结束了,那么您应该考虑避免命令行参数,或者将它们放入构建脚本或 makefile 中。
我将提供两种避免命令行参数的技术:
技巧 1:如果您正在生成必须保持可访问性的固定文档数组,就像您的两种样式示例一样,那么我建议简单地实现Will 的乳胶代码位于另一个 tex 文件中,即 thesis.tex 包含
\providecommand\comment[1]{\emph{#1}}
, thesis-ugly.tex 包含\newcommand\ comment[1]{\textbf{#1}} \输入thesis.tex
。当然,使用此技术时,您必须重新运行 bibtex 等工具,除非您对中间文件进行符号链接,ala
ln -s thesis.aux thesis-ugly.aux
和ln -s thesis.bbl thesis -ugly.bbl。
技巧 2: 我发现技巧 1 对于更改文档纸张大小很不方便,因此我编写了以下 Perl 脚本,简称为 papersize。命令
papersize A4eaching.tex
修改教学.tex,并将教学.pdf符号链接到教学-A4.pdf,以便运行pdflatex教学
创建教学-A4。 pdf,但不会干扰预先存在的教学-letter.pdf,并且不需要重新运行bibtex教学
。对于具有内部引用的文档,显然需要重新运行 pdflatex 两次。您必须将特殊注释
% paper size :: ...
添加到更改纸张尺寸时应更改的每个文件行。显然,您也可以将 papersize 纳入构建脚本或 makefile 中,或者修改 .dvi 文件的上述脚本......或者将脚本推广到其他修改。
You should use Will's approaches when you need fairly flexible one-off options, like say changing the position line on your resume. If otoh you are producing the same selection of options over & over, then you should consider avoiding command line arguments, or working them into a build script or makefile.
I'll give two techniques for avoiding command line arguments :
Trick 1: If you're producing a fixed array of documents that must remain accessible, like your two styles example, then I'd recommend simply implementing Will's latex code inside another tex file, i.e. thesis.tex contains a
\providecommand\comment[1]{\emph{#1}}
and thesis-ugly.tex consists of\newcommand\comment[1]{\textbf{#1}} \input thesis.tex
.You must of course rerun tools like bibtex when using this technique, unless you symlink the intermediary files, ala
ln -s thesis.aux thesis-ugly.aux
andln -s thesis.bbl thesis-ugly.bbl
.Trick 2: I found trick 1 awkward for changing document papersizes, so I wrote the following perl script, called simply papersize. The command
papersize A4 teaching.tex
modifies teaching.tex in place, and symlinks teaching.pdf to teaching-A4.pdf, so that runningpdflatex teaching
creates teaching-A4.pdf, but does not disturb the pre-existing teaching-letter.pdf and does not require rerunningbibtex teaching
. It does obviously require rerunning pdflatex twice for documents with internal references.You must add the special comments
% paper size :: ...
to every file line that should be changed when you change the paper size.You could obviously work papersize into a build script or makefile too or modify the above script for .dvi files.. or generalize the script to other modifications.
这可能不是所讨论情况的最佳解决方案(也是一个非常旧的帖子),但我想我会提出一个对我有用的替代解决方案。只需在两个不同的文件中定义两个命令,将第三个文件链接到所需的选择,然后输入该第三个文件。
例如,定义:
。
。
然后使用 option1 进行编译,只需运行:
我更喜欢这个,因为我对 bash 比晦涩的 Latex 更熟悉,对文件的激增没有问题(这些文件可以轻松地组织在子目录中),并且无论如何都有脚本编译我的文档,所以这额外的步骤花费很少。
This may not be the optimal solution for the situation in question (also a very old post), but I thought I'd present an alternative solution which was useful for me. It is simply to define the two commands in two different files, link a third file to the desired choice, and \input that third file.
For example, define:
.
.
Then to compile with option1, simply run:
I prefer this because I am more comfortable with bash than obscure latex, have no problem with a proliferation of files (these can easily be organized in subdirectories), and have scripts compile my document anyway so this additional step comes at little cost.
为了以所需的、丑陋的、浪费树木的格式和紧凑的漂亮版本提供我的论文,我使用了
ifthen
以及make
和sed
的组合。 code> 重写了一些标头。我认为威尔的方法全部< /em> 更好。
To provide my dissertation in both the required, ugly, tree wasting format, and a compact prettier version, I used
ifthen
an a kludge ofmake
andsed
that rewrote a bit of the header.I think Will's approaches are all nicer.