\def に「or」条件を追加することは可能ですか?

\def に「or」条件を追加することは可能ですか?

こんなことは可能でしょうか

\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}

質問に対するコメントを見ると、オプションの引数はコンマで区切られた 2 つの項目であることが想定されているようです。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}

または

関連情報