
是否有可能有類似的東西
\def\fiveorsix{5,6 OR 6,5}
以便進行以下工作:
\documentclass{standalone}
\def\fiveorsix{5,6 OR 6,5}
\makeatletter
\newcommand{\test}[1][]{
\def\test@a{#1}
\ifx\test@a\fiveorsix
the optional argument is 5,6 or 6,5
\else
the optional argument is not 5,6 or 6,5
\fi}
\makeatother
\begin{document}
\test[5,6]
\end{document}
(顯然這是行不通的)
或者有更好的方法來實現我的目標嗎?
編輯:我必須更新 MnWE,因為第一個版本實際上太小了。
答案1
\documentclass{article}
\usepackage{expl3}[2013/07/24]
\ExplSyntaxOn
\newcommand{\test}[1][]{
\str_case:nnTF {#1}
{
{ 5,6 } { } % within those braces you could put code specific to the 5,6 case
{ 6,5 } { } % within those braces you could put code specific to the 6,5 case
}
{ the~argument~is~5,6~or~6,5 }
{ the~argument~is~neither~5,6~nor~6,5 }
}
\ExplSyntaxOff
\begin{document}
\test[123]
\test[5,6]
\test[6,5]
\end{document}
鑑於對您問題的評論,可選參數似乎應該是一對用逗號分隔的項目。您可能需要考慮使用xparse
s \SplitArgument
,它可以在給定的分隔符號處分割參數,如果分隔符號的數量不符合預期,則會報告錯誤。它還刪除了每個項目周圍的空格。
答案2
你可以使用etoolbox
(需要e-TeX
):
\documentclass{article}
\usepackage{etoolbox}
\newcommand{\test}[1][]{%
\ifboolexpr{
test{\ifstrequal{#1}{5,6}} or
test{\ifstrequal{#1}{6,5}
}
}
{the optional argument is 5,6 or 6,5}
{the optional argument is not 5,6 or 6,5}}
\begin{document}
\test[3,2]
\test[5,6]
\end{document}
結果是:
可選參數不是 5,6 或 6,5
可選參數是 5,6 或 6,5
您可以使用其他測試,例如\ifnumcomp
.詳細資訊請查看 etoolbox手動的。
答案3
不完全符合要求,但想法應該很清楚:
\documentclass{article}
\begin{document}
\def\fivesix#1{\ifcase#1 the argument is not 5 or 6
\or the argument is not 5 or 6
\or the argument is not 5 or 6
\or the argument is not 5 or 6
\or the argument is not 5 or 6
\or the argument IS 5 or 6
\or the argument IS 5 or 6
\else the argument is not 5 or 6\fi}
\fivesix{1}
\fivesix{5}
\end{document}
答案4
考慮到您詢問的是定義而不是比較,您問題的確切答案應該是不可能(在 TeX 中)。宏展開必須是唯一的。若要實現此目的,您必須在定義中指定在什麼情況下\fiveorsix
巨集應擴展為「5,6」或「6,5」。
至於您詢問測試巨集參數是否擴展到“5,6”或“6,5”的替代方法 - 並且已經得到了一堆答案 - 我添加了這個通用解決方案以確保完整性:
\documentclass{article}
\def\fivsix{5,6}
\def\sixfiv{6,5}
\makeatletter
\newcommand{\test}[1][]{%
\def\test@a{#1}
\ifnum
\ifx\test@a\fivsix 1\else\ifx\test@a\sixfiv 1\else 0\fi\fi
=1
the optional argument is 5,6 or 6,5
\else
the optional argument is not 5,6 and not 6,5
\fi}
\makeatother
\begin{document}
\test[4,5]\par
\test[5,6]\par
\test[6,5]
\end{document}