3

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

例如我想要:

\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。有没有人解决这个问题?

4

3 回答 3

4

ifthen包(包含在标准 LaTeX 安装中)定义了一个 command \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的副本。这对这些东西来说是一个很好的参考。

于 2009-01-07T21:10:00.657 回答
2

您可以使用 \newif\ifoo 来声明一个新条件。然后\footrue 或\foofalse 将其设置为true 或false,你可以通过\iffoo ... \else ... \fi 来使用它。

还有更多条件,请参阅 TeXbook 中的第 209 页。

于 2009-01-07T21:00:00.707 回答
0

另一种选择是使用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}

它产生

我的文本

于 2021-08-15T17:43:01.943 回答