将命令行参数传递给 LaTeX 文档

发布于 2024-08-05 11:17:28 字数 271 浏览 4 评论 0原文

有时,我会定义如下新命令。

\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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单挑你×的.吻 2024-08-12 11:17:28

也就是说,我可以向 Latex 传递一些命令行参数,以便我可以根据该参数选择要使用的样式吗?

是的。三个选项:

1

在源文件中,编写

\providecommand{\comment}[1]{\emph{#1}}% fallback definition

并编译 LaTeX 文档(“myfile.tex”) 2

pdflatex (whatever options you need) "\newcommand\comment[1]{\textbf{#1}}\input{myfile}"

或者

pdflatex "\let\ifmyflag\iftrue\input{myfile}"

然后在源文件中

\ifcsname ifmyflag\endcsname\else
  \expandafter\let\csname ifmyflag\expandafter\endcsname
                  \csname iffalse\endcsname
\fi
...
\ifmyflag
  \newcommand\comment[1]{\emph{#1}}
\else
  \newcommand\comment[1]{\textbf{#1}}
\fi

3

甚至

pdflatex "\def\myflag{}\input{myfile}"

使用

\ifdefined\myflag
  \newcommand\comment[1]{\emph{#1}}
\else
  \newcommand\comment[1]{\textbf{#1}}
\fi

哪个可能是最短的,尽管有点脆弱,因为你永远不知道当一个包可能在你背后定义 \myflag 时。

That is, can I pass latex some command-line arguments so that I can choose which style to use based on that argument?

Yes. Three options:

One

In your source file, write

\providecommand{\comment}[1]{\emph{#1}}% fallback definition

and then compile the LaTeX document ("myfile.tex") as

pdflatex (whatever options you need) "\newcommand\comment[1]{\textbf{#1}}\input{myfile}"

Two

Alternatively,

pdflatex "\let\ifmyflag\iftrue\input{myfile}"

and then have in the source

\ifcsname ifmyflag\endcsname\else
  \expandafter\let\csname ifmyflag\expandafter\endcsname
                  \csname iffalse\endcsname
\fi
...
\ifmyflag
  \newcommand\comment[1]{\emph{#1}}
\else
  \newcommand\comment[1]{\textbf{#1}}
\fi

Three

Or even

pdflatex "\def\myflag{}\input{myfile}"

with

\ifdefined\myflag
  \newcommand\comment[1]{\emph{#1}}
\else
  \newcommand\comment[1]{\textbf{#1}}
\fi

which is probably the shortest, albeit slightly fragile because you never know when a package might define \myflag behind your back.

舟遥客 2024-08-12 11:17:28

当您需要相当灵活的一次性选项(例如更改简历上的职位线)时,您应该使用威尔的方法。如果 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.auxln -s thesis.bbl thesis -ugly.bbl。

技巧 2: 我发现技巧 1 对于更改文档纸张大小很不方便,因此我编写了以下 Perl 脚本,简称为 papersize。命令papersize A4eaching.tex修改教学.tex,并将教学.pdf符号链接到教学-A4.pdf,以便运行pdflatex教学创建教学-A4。 pdf,但不会干扰预先存在的教学-letter.pdf,并且不需要重新运行bibtex教学。对于具有内部引用的文档,显然需要重新运行 pdflatex 两次。

#!/usr/bin/perl -i~ -n

BEGIN {
die "Usage: papersize letter/A4/etc. [filename]\n" if ($#ARGV < 0);
$SIZE = shift @ARGV;  @files=@ARGV;
$FLAG = "% paper size :: ";
}

if (/$FLAG(\w+)/) {
    if ($1 eq $SIZE) {
        s/^\% //;
    } else {
        s/^([^\%])/\% \1/;
    }
}
print $_;

END {
foreach (@files) {
    if (s/\.tex//) {
    $l = "$_-$SIZE.pdf";  $_ .= ".pdf";
    unlink($_) if (-l $_);
    symlink($l,$_) if (! -e $_);
} }
}

您必须将特殊注释 % paper size :: ... 添加到更改纸张尺寸时应更改的每个文件行。

\documentclass[letterpaper,11pt]{article}  % paper size :: letter
% \documentclass[a4paper,11pt]{article}  % paper size :: A4
\usepackage[text={6.5in,8.8in}]{geometry}  % paper size :: letter
% \usepackage[text={16.4cm,24.5cm}]{geometry}  % paper size :: A4

显然,您也可以将 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 and ln -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 running pdflatex teaching creates teaching-A4.pdf, but does not disturb the pre-existing teaching-letter.pdf and does not require rerunning bibtex teaching. It does obviously require rerunning pdflatex twice for documents with internal references.

#!/usr/bin/perl -i~ -n

BEGIN {
die "Usage: papersize letter/A4/etc. [filename]\n" if ($#ARGV < 0);
$SIZE = shift @ARGV;  @files=@ARGV;
$FLAG = "% paper size :: ";
}

if (/$FLAG(\w+)/) {
    if ($1 eq $SIZE) {
        s/^\% //;
    } else {
        s/^([^\%])/\% \1/;
    }
}
print $_;

END {
foreach (@files) {
    if (s/\.tex//) {
    $l = "$_-$SIZE.pdf";  $_ .= ".pdf";
    unlink($_) if (-l $_);
    symlink($l,$_) if (! -e $_);
} }
}

You must add the special comments % paper size :: ... to every file line that should be changed when you change the paper size.

\documentclass[letterpaper,11pt]{article}  % paper size :: letter
% \documentclass[a4paper,11pt]{article}  % paper size :: A4
\usepackage[text={6.5in,8.8in}]{geometry}  % paper size :: letter
% \usepackage[text={16.4cm,24.5cm}]{geometry}  % paper size :: A4

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.

温折酒 2024-08-12 11:17:28

这可能不是所讨论情况的最佳解决方案(也是一个非常旧的帖子),但我想我会提出一个对我有用的替代解决方案。只需在两个不同的文件中定义两个命令,将第三个文件链接到所需的选择,然后输入该第三个文件。
例如,定义:

% in main.tex
\input{options}
% ...

% in option1.tex
\newcommand{\comment}[1]{\textbf{#1}}

% in option2.tex
\newcommand{\comment}[1]{\emph{#1}} 

然后使用 option1 进行编译,只需运行:

> ln -s option1.tex options.tex && pdflatex main.tex

我更喜欢这个,因为我对 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:

% in main.tex
\input{options}
% ...

.

% in option1.tex
\newcommand{\comment}[1]{\textbf{#1}}

.

% in option2.tex
\newcommand{\comment}[1]{\emph{#1}} 

Then to compile with option1, simply run:

> ln -s option1.tex options.tex && pdflatex main.tex

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.

北城孤痞 2024-08-12 11:17:28

为了以所需的、丑陋的、浪费树木的格式和紧凑的漂亮版本提供我的论文,我使用了 ifthen 以及 makesed 的组合。 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 of make and sed that rewrote a bit of the header.

I think Will's approaches are all nicer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文