4

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?

\documentclass[]{article}

% Defining an 'if command'
% !!! The ERROR pops up IF THIS LINE IS COMMENTED OUT
\newif\ifiskomascriptloaded \iskomascriptloadedfalse

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

\begin{document}
   The result is: \testtext    
\end{document}
4
  • Maybe another question for @DavidCarlisle ... ;) Nov 8, 2016 at 14:35
  • must be a duplicate:-) Nov 8, 2016 at 14:49
  • there is no problem with nesting \if commands _if they are defined to be if commands) but not commands that are undefined but just happen to have names starting with \if Nov 8, 2016 at 14:54
  • see this one in the related list on the right tex.stackexchange.com/questions/315749/… Nov 8, 2016 at 15:24

2 Answers 2

7

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
   \usepackage{scrextend} 
   \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

\@ifundefined{ifiskomascriptloaded}
  {undefined case}
  {defined case}

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

0
1

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}
         \else
            \usepackage{scrextend} %with the nice functionality addmargin. 
            \newcommand{\testtext}{Package Loaded}
         \fi} %end ifthenelse

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .