如何根据 LaTeX 中具有状态的变量定义输出?

发布于 2024-07-11 22:08:55 字数 445 浏览 7 评论 0原文

我有一些标准文本,但其中某些部分是不同的。 但在这些不同的部分中,只有少数存在。

例如,我想要:

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

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

发布评论

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

评论(3

请远离我 2024-07-18 22:08:55

ifthen 包(包含在标准 LaTeX 安装中)定义了一个命令 \ifthenelse,其使用方式如下:

\usepackage{ifthen}
\ifthenelse{test}{then-code}{else-code}

因此您可以执行类似以下操作:

\newcommand\mytext[1]{%
    \ifthenelse{\equal{#1}{a}}{very precious}{%
    \ifthenelse{\equal{#1}{b}}{cheap}{unknown}}}

对于 LaTeX 编程,我建议您获取一份The LaTeX Companion。 这对这个东西来说确实是一个很好的参考。

The ifthen package (which is included in a standard LaTeX installation) defines a command \ifthenelse, which is used like this:

\usepackage{ifthen}
\ifthenelse{test}{then-code}{else-code}

so you could do something similar to:

\newcommand\mytext[1]{%
    \ifthenelse{\equal{#1}{a}}{very precious}{%
    \ifthenelse{\equal{#1}{b}}{cheap}{unknown}}}

For LaTeX programming, I'd recommend getting a copy of The LaTeX Companion. It's a really good reference for this stuff.

意中人 2024-07-18 22:08:55

您可以使用 \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.

第七度阳光i 2024-07-18 22:08:55

另一种选择是使用 etoolbox (与 XeLaTeX 一起使用),下面是一个 MWE

\documentclass{article}

\usepackage{etoolbox}

\begin{document}

\newcommand{\mytext}[1]
{
\ifstrequal{#1}{a} %% make the first comparison
{ %% print text in the first scenario
\section{Item: #1}\label{item.#1}
This is a standard item. Items of type a are very precious.
}
{} %% do nothing if false
\ifstrequal{#1}{b} %% make the second comparison
{ %% print text in the second scenario
\section{Item: #1}\label{item.#1}
This is a standard item. Items of type b are cheap.
}
{} %% do nothing if false
}

\mytext{a}
\mytext{b}
\\

\noindent
We can refer to sections \ref{item.a} and \ref{item.b}

\end{document}

,它会生成

my_text

Another option is to use etoolbox (which works with XeLaTeX), below is an MWE

\documentclass{article}

\usepackage{etoolbox}

\begin{document}

\newcommand{\mytext}[1]
{
\ifstrequal{#1}{a} %% make the first comparison
{ %% print text in the first scenario
\section{Item: #1}\label{item.#1}
This is a standard item. Items of type a are very precious.
}
{} %% do nothing if false
\ifstrequal{#1}{b} %% make the second comparison
{ %% print text in the second scenario
\section{Item: #1}\label{item.#1}
This is a standard item. Items of type b are cheap.
}
{} %% do nothing if false
}

\mytext{a}
\mytext{b}
\\

\noindent
We can refer to sections \ref{item.a} and \ref{item.b}

\end{document}

and it produces

my_text

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