[Tex/LaTex] Conditionals in LaTeX: try/catch and “not \equal”

class-optionsconditionalsdocument-classes

I have a problem with some variable checking: I offer the user an option in the interface of a document class. If the option is not provided my code fails. Hence I would like to check if the option has been provided or not. In other programming environments I would do this with TRY/CATCH of "!=" not equal. How do I do this in LaTeX?

A minimal example looks like this:

1) document class 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) working minimalExample.tex:

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

3) crashing minimalExample.tex:

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

I would like to check if variable \theVariable has a value if it is not provided and bypass the crashing code if not. Any ideas?

Best Answer

Just remove the \providecommand{\theVariable} line: you're later checking whether it exists with \ifdefined, aren't you?

However, if you later need to check whether it equals \@empty, then do like this:

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

Note \newcommand* instead of \providecommand (and no argument); note also that I define \theVariable to be empty by default, so a check

\ifx\theVariable\@empty

would succeed. It wouldn't if you do \newcommand*{\theVariable}{\@empty}, because in this case the replacement text of \theVariable is not empty: a box containing an empty box is not empty, is it?