13

The MWE below attemps to define a \newif within a \ifdefined. This works fine, except when the file is loaded the second time.

As it is below generates the following error message:

Incomplete \ifdefined; all text was ignored after line 1.

However if you comment out the \newif works as desired.

Question:

How do I declare the \newif within the \ifdefined?

MWE:

\documentclass{article}

\usepackage{filecontents} \begin{filecontents}{MyHeader.tex} \ifdefined\MyHeaderAlreadyIncluded\else \def\MyHeaderAlreadyIncluded{}% \newcommand{\SomeCommand}{some command}% \newif\ifDTLnewdbonload% \fi \end{filecontents}

\begin{document} Lorem ipsum \input{MyHeader.tex} \SomeCommand.

\input{MyHeader.tex} \SomeCommand. \end{document}

Peter Grill
  • 223,288

1 Answers1

11

In order not to load a file twice, another method is preferable:

%%% MyHeader.tex
\ifdefined\MyHeaderAlreadyIncluded
  \expandafter\endinput
\fi

\gdef\MyHeaderAlreadyIncluded{}
\newcommand{\SomeCommand}{some command}
\newif\ifDTLnewdbonload

If the file has already been loaded, the control sequence would be defined. So the conditional expands \endinput that stops reading the file. The \expandafter is just to finish the conditional. A different format would be

%%% MyHeader.tex
\ifdefined\MyHeaderAlreadyIncluded\endinput\fi

\gdef\MyHeaderAlreadyIncluded{}
\newcommand{\SomeCommand}{some command}
\newif\ifDTLnewdbonload

because TeX always ends the line before \endinput does its work.


To answer the original question,

\expandafter\newif\csname ifDTLnewdbonload\endcsname

would work, because if TeX skips the \else branch, it will not "see" the already defined conditional.

egreg
  • 1,121,712