如何根据 LaTeX 中具有状态的变量定义输出?
我有一些标准文本,但其中某些部分是不同的。 但在这些不同的部分中,只有少数存在。
例如,我想要:
\mytext{...}{a}
\mytext{...}{b}
产生:
\section{Item: ...}\label{item...}
This is a standard item. Items of type a are very precious.
\section{Item: ...}\label{item...}
This is a standard item. Items of type b are cheap.
一个简单的解决方案是定义命令 mytexta 和 mytextb,但由于我有更多选项,我想要更多类似编程语言中的 if 或 switch 之类的东西。 有没有人解决这个问题?
I have some standard-texts, but some portion of it is different. But of these different parts only a few exists.
For instance I want:
\mytext{...}{a}
\mytext{...}{b}
That produces:
\section{Item: ...}\label{item...}
This is a standard item. Items of type a are very precious.
\section{Item: ...}\label{item...}
This is a standard item. Items of type b are cheap.
A simple solution to this would be to define commands mytexta and mytextb, but as I have more options I want more something like an if or switch in programming languages. Has anyone a solution for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ifthen
包(包含在标准 LaTeX 安装中)定义了一个命令\ifthenelse
,其使用方式如下:因此您可以执行类似以下操作:
对于 LaTeX 编程,我建议您获取一份The LaTeX Companion。 这对这个东西来说确实是一个很好的参考。
The
ifthen
package (which is included in a standard LaTeX installation) defines a command\ifthenelse
, which is used like this:so you could do something similar to:
For LaTeX programming, I'd recommend getting a copy of The LaTeX Companion. It's a really good reference for this stuff.
您可以使用 \newif\iffoo 来声明新条件。
然后 \footrue 或 \foofalse 将其设置为 true 或 false,并且可以通过 \iffoo ... \else ... \fi 来使用它。
还有更多条件,请参阅 TeXbook 中的第 209 页。
You can use \newif\iffoo to declare a new condition.
Then \footrue or \foofalse sets it to true or false, and you can use it by \iffoo ... \else ... \fi.
There are more conditionals, see pages 209ff in the TeXbook.
另一种选择是使用
etoolbox
(与 XeLaTeX 一起使用),下面是一个 MWE,它会生成
Another option is to use
etoolbox
(which works with XeLaTeX), below is an MWEand it produces