If you use the LaTeX tests they take care to lift the true and false branches out of the tex primitive conditional:
%%% in file A
\documentclass{article}
\newif\ifdraft\drafttrue
\def\OptionLoaded{}
%%% in file B
\makeatletter
\@ifundefined{OptionLoaded}{\newif\ifdraft\draftfalse}{}
\makeatother
\begin{document}
hello
\end{document}
Your version fails as when the conditional is defined the \ifx
test is false
so in
\ifx \OptionLoaded \undefined
\newif\ifdraft\draftfalse
\fi
Tex tries to skip past \newif\ifdraft\draftfalse
without evaluating anything so \newif
is just an inert token, however when skipping it does match \if
tokens so sees \ifdraft
and the \fi
but it carries on skipping expecting another \fi
to close the \ifx
.
\newif\ifdraft\drafttrue
this complies for me without any problems. – user30471 Sep 24 '20 at 7:16\ifdraft
, then file B do nothing; if file A doesn't define\ifdraft
, then file B should define it. – Tianren Liu Sep 24 '20 at 7:21