Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
\expandafter\newif\csname ifDTLnewboload\endcsname; but there are better ways. –  egreg Feb 3 '13 at 22:31

1 Answer 1

up vote 9 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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