LaTeX의 조건문: try/catch 및 "not \equal"

LaTeX의 조건문: try/catch 및 "not \equal"

일부 변수 검사에 문제가 있습니다. 문서 클래스의 인터페이스에서 사용자에게 옵션을 제공합니다. 옵션이 제공되지 않으면 내 코드가 실패합니다. 따라서 옵션이 제공되었는지 확인하고 싶습니다. 다른 프로그래밍 환경에서는 "!="가 같지 않은 TRY/CATCH를 사용하여 이 작업을 수행합니다. LaTeX에서는 어떻게 하나요?

최소한의 예는 다음과 같습니다.

1) 문서 클래스 minimalExample.cls:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{FAIRControlledDocument}[2017/07/03 minimalExample]

\LoadClass[a4paper,11pt]{report}

\RequirePackage{ifthen}
\RequirePackage{xkeyval}

\providecommand{\theVariable}[1]{\@empty}
\DeclareOptionX{docoption}{%
    \def\theVariable{#1}%
}

\ProcessOptionsX

% pre-defined document types
\ifdefined\theVariable
\ifthenelse{\equal{\theVariable}{a} \OR \equal{\theVariable}{b}}{%
    \providecommand\fcd@type@xx{some text}%
}{}
\fi

2) 근무 중 minimalExample.tex:

\documentclass[docoption=a]{minimalExample}
\begin{document}
test
\end{document}

3) 충돌 minimalExample.tex:

\documentclass[]{minimalExample}
\begin{document}
test
\end{document}

\theVariable변수가 제공되지 않은 경우 변수에 값이 있는지 확인하고 그렇지 않은 경우 충돌 코드를 우회하고 싶습니다 . 어떤 아이디어가 있나요?

답변1

그 줄을 제거하세요 \providecommand{\theVariable}. 나중에 그 줄이 에 존재하는지 확인하게 될 것입니다 \ifdefined. 그렇죠?

그러나 나중에 같은지 확인해야 하는 경우 \@empty다음과 같이 하십시오.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{FAIRControlledDocument}[2017/07/03 minimalExample]

\LoadClass[a4paper,11pt]{report}

\RequirePackage{ifthen}
\RequirePackage{xkeyval}

\newcommand*{\theVariable}{}
\DeclareOptionX{docoption}{%
    \def\theVariable{#1}%
}

\ProcessOptionsX

% pre-defined document types
\ifthenelse{\equal{\theVariable}{a} \OR \equal{\theVariable}{b}}{%
    \providecommand\fcd@type@xx{some text}%
}{}

\newcommand*대신에 참고하십시오 \providecommand(인수 없음). 또한 \theVariable기본적으로 비어 있도록 정의하므로 확인 하세요.

\ifx\theVariable\@empty

성공할 것입니다. 그렇게 하면 그렇지 않을 것입니다 \newcommand*{\theVariable}{\@empty}. 왜냐하면 이 경우 대체 텍스트가 \theVariable비어 있지 않기 때문입니다. 빈 상자를 포함하는 상자는 비어 있지 않습니까?

답변2

이에 대한 대답은 다소 간단합니다. 의 기본 정의에는 \theVariable인수가 필요하지만 옵션이 사용되는 경우 정의한 정의에는 인수가 필요하지 않으므로 옵션 사용법에 따라 두 가지 다른 정의가 있습니다. 코드 \theVariable가 요청하는 동안 인수를 제공하지 않기 때문에 코드가 실패합니다 .

이 문제를 해결하려면 \providecommand*{\theVariable}{\@empty}. 어떤 식으로든 인수를 허용하지 않기 때문에 오래 걸릴 *필요가 없기 때문에 추가했습니다 .\theVariable

따라서 .cls는 다음과 같아야 합니다.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{FAIRControlledDocument}[2017/07/03 minimalExample]

\LoadClass[a4paper,11pt]{report}

\RequirePackage{ifthen}
\RequirePackage{xkeyval}

\providecommand*{\theVariable}{\@empty}
\DeclareOptionX{docoption}{%
    \def\theVariable{#1}%
}

\ProcessOptionsX\relax

% pre-defined document types
\ifdefined\theVariable
\ifthenelse{\equal{\theVariable}{a} \OR \equal{\theVariable}{b}}{%
    \providecommand\fcd@type@xx{some text}%
}{}
\fi

답변3

egreg와 Skillmon의 접근 방식을 시도했지만 코드를 실행하지 못했습니다. 마지막으로 다음 클래스 정의로 실제 문제를 해결했습니다.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{FAIRControlledDocument}[2017/07/03 minimalExample]

\LoadClass[a4paper,11pt]{report}

\RequirePackage{ifthen}
\RequirePackage{xkeyval}

%\providecommand{\theVariable}[1]{\@empty} <-- Do not defined this variable
\DeclareOptionX{docoption}{%
    \def\theVariable{#1}%
}

\ProcessOptionsX

% pre-defined document types
\ifdefined\theVariable
\ifthenelse{\equal{\theVariable}{a} \OR \equal{\theVariable}{b}}{%
    \providecommand\fcd@type@xx{some text}%
}{}
\else
   \def\documentLanguage{xx} <-- set some special value for later use
\fi

그래도 try/catch에 대한 더 큰 질문에 대한 답변은 없지만 "해결됨"으로 문제를 종료하겠습니다. 이제 코드가 작동합니다.

관련 정보