# \if nested in \ifdefined

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

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

\begin{document}
The result is: \testtext
\end{document}

• Maybe another question for @DavidCarlisle ... ;) – loved.by.Jesus Nov 8 '16 at 14:35
• must be a duplicate:-) – David Carlisle Nov 8 '16 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 – David Carlisle Nov 8 '16 at 14:54
• see this one in the related list on the right tex.stackexchange.com/questions/315749/… – David Carlisle Nov 8 '16 at 15:24

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)
\else % _this_ else matches the \ifdefined
% so do these
\usepackage{scrextend}
\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.....

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