将命令行参数传递给LaTeX文档 [英] Passing command-line arguments to LaTeX document

查看:79
本文介绍了将命令行参数传递给LaTeX文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我定义新的命令,如下所示.

Sometimes, I define new commands such as the following.

\newcommand{\comment}[1]{\textbf{#1}}
%\necommand{\comment}[1]{\emph{#1}} 

以上命令使我可以立即更改部分代码的样式.如果我想生成两种可能的样式,则每次修改源代码以启用所需样式时,都必须两次编译LaTeX文档.

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?

推荐答案

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

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

是的.三种选择:

在您的源文件中,编写

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

然后将LaTeX文档("myfile.tex")编译为

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

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

三个

甚至

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

使用

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

这可能是最短的,尽管它有些脆弱,因为您不知道何时包装会在您的背后定义\myflag.

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

这篇关于将命令行参数传递给LaTeX文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆