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

귀하의 질문에 대한 의견을 보면 선택적 인수는 쉼표로 구분된 항목 쌍이 될 것으로 예상됩니다. 구분 기호 수가 예상과 다른 경우 오류 보고와 함께 주어진 구분 기호에서 인수를 분할할 수 있는 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}

오르콘드

관련 정보