将多个参数传递给 \ifthenelse 例程

将多个参数传递给 \ifthenelse 例程

我最近发现了 LaTeX,并尝试用它来编写一份复杂的工业文档(实验室测试说明)。典型的说明集包含一长串标准测试(每个测试都由一个编号系统标识)。由于某些测试在测试序列中重复多次,因此我创建了一个系统,如所附示例中所示,其中单个指令(例如)调用\TestDetails{107}测试 107 所需的指令。但是,描述测试的所有例程都没有参数。我现在面临的问题是在某些测试中引入选项(即测试 107 将在序列的某个点在条件 A 下执行,在另一个点在条件 B 下执行,这需要更改说明。)使用 ifthenelse 选项可以实现吗?如果不行,建议采用什么方法?

\documentclass[a4paper, 11pt]{report} % Mode production
\usepackage[utf8]{inputenc} % UTF-8 encoding for code editing
\usepackage[T1]{fontenc} % Usual fonts
\usepackage{ifthen} % Use of logic tests
%----------------------------------------------
\newcommand{\SandAndDust}[0]{%
Some text for sand and dust test.
} %

\newcommand{\VoltageProof}[0]{%
Some text for voltage proof text.
} %

\newcommand{\TestDetails}[1]{%
\ifthenelse{\equal{#1}{100}}{\SandAndDust}{}%
\ifthenelse{\equal{#1}{107}}{\VoltageProof}{}%
}%

\begin{document}

\TestDetails{100}

\TestDetails{107}

\end{document}

答案1

您可以使用 LaTeX3。在那里,您可以使用\prg_case_int:nnn还允许计算的函数。

\documentclass[a4paper, 11pt]{report} % Mode production
\usepackage[utf8]{inputenc} % UTF-8 encoding for code editing
\usepackage[T1]{fontenc} % Usual fonts
\newcommand{\SandAndDust}[0]{%
Some text for sand and dust test.
} %

\newcommand{\VoltageProof}[0]{%
Some text for voltage proof text.
} %


\usepackage{xparse,expl3}
\ExplSyntaxOn
\NewDocumentCommand { \TestDetails } { m }
 {
  \prg_case_int:nnn {#1}
   {
    {100} { \SandAndDust }
    {107} { \VoltageProof }
   }
   {
    not~in~list
   }
 }
\begin{document}
\TestDetails{100}\par
\TestDetails{107}\par
\TestDetails{100+7}\par
\TestDetails{103}
\end{document}

答案2

\documentclass{report}
\newcommand\SandAndDust{Some text for sand and dust test.} 
\newcommand\VoltageProof{Some text for voltage proof text.} 
\newcommand\TestDetails[1]{%
  \ifnum100=#1 \SandAndDust\else\ifnum107=#1 \VoltageProof
  \else Something else\fi\fi}%

\begin{document}
\TestDetails{100}

\TestDetails{107}

\TestDetails{1}
\end{document}

答案3

如果我正确理解了你的要求,我认为你只需要对你已有的内容进行轻微的改变,如下所示

Some text for sand and dust test.

Some text for voltage proof text.

Some text for voltage proof text in Condition A.

Some text for voltage proof text  in Condition B.

修改后的示例:

\documentclass[a4paper, 11pt]{report} % Mode production
\usepackage[utf8]{inputenc} % UTF-8 encoding for code editing
\usepackage[T1]{fontenc} % Usual fonts
\usepackage{ifthen} % Use of logic tests
%----------------------------------------------
\newcommand{\SandAndDust}[0]{%
Some text for sand and dust test.
}

\newcommand{\VoltageProof}[1][]{%
Some text for voltage proof text%
    \ifthenelse{\equal{#1}{}}{}{ in Condition #1}.}


\newcommand{\TestDetails}[2][]{%
\ifthenelse{\equal{#2}{100}}{\SandAndDust}{}%
\ifthenelse{\equal{#2}{107}}{\VoltageProof[#1]}{}%
}

\begin{document}

\TestDetails{100}

\TestDetails{107}

\TestDetails[A]{107}

\TestDetails[B]{107}

\end{document}

相关内容