I have something like the following MWE code (see below), a \if condition is checked only if the corresponding if command is defined. Otherwise the \if condition is skipped by the \else of the \ifdefined. But I get an error when the 'if command', i.e. \ifiskomascriptloaded, is undefined.

! Extra \else.

l.13 \else

Is there any problem in nesting these two kinds of if's?


% Defining an 'if command'
\newif\ifiskomascriptloaded \iskomascriptloadedfalse

%Checking if the 'if command' is defined
    %In case defined we use the 'if command'
        \newcommand{\testtext}{Package Not Loaded}
        %Note: Never to use with a komma script document
        \usepackage{scrextend} %with the nice functionality addmargin. 
        \newcommand{\testtext}{Package Loaded}
    %In case not defined we do something else
    \newcommand{\testtext}{Not defined}

   The result is: \testtext    

look what happens if it is not defined

\ifdefined\ifiskomascriptloaded % false so skip to matching \else 
   \ifiskomascriptloaded % skip over this (this is not an if remember)
   \newcommand{\testtext}{Package Not Loaded}% skip this
\else % _this_ else matches the \ifdefined
   % so do these
   \newcommand{\testtext}{Package Loaded}
\fi % this closes the \ifdefined
\else %oops this is an error an \else with no if
\newcommand{\testtext}{Not defined}% this is an error as the command was defined just above 
\fi % and so is this an error as no matching if.

If you used the standard latex syntax

  {undefined case}
  {defined case}

then the problem would not arise. There is a reason why that is the latex syntax.....

|improve this answer|||||

I found a good post here with an answer that I find somehow 'cleaner' (using ifthen package ;)), no need to use @ (makeatletter and co.)

On the other hand the if condition is a little longer because \isundefined does not work for if-commands, e.g. \ifiskomascriptloaded, so I need to check the two macros -true and -false associated to the if.

I am sure the expert LaTeX people can define a nice macro \isifundefined that makes the whole expression more compact. :)

\ifthenelse{\isundefined{\iskomascriptloadedtrue} \AND \isundefined{\iskomascriptloadedfalse}}
    {\newcommand{\testtext}{Not defined}} %then code
    { \ifiskomascriptloaded 
            \newcommand{\testtext}{Package Not Loaded}
            \usepackage{scrextend} %with the nice functionality addmargin. 
            \newcommand{\testtext}{Package Loaded}
         \fi} %end ifthenelse
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.