[Tex/LaTex] Boolean switch for conditional statement after its definition

conditionals

I'd like to use conditional statements in my preamble file and then define the value of the Boolean variable afterwards. The reason is that I import the preamble.tex first thing in my main file where I then want to specify switches as appropriate. I've tried this with the plain tex \newif and with the etoolbox packages, yet both seem to require the value of the Boolean variable before the definition of the conditional. This appears to mean that I would have to change my preamble.tex, defeating the purpose of making it a separate file.

Is there an easy way of achieving this, short of splitting the preamble file in two parts?

Thanks,
Michael

Best Answer

The approach you're following is not correct. The best thing is to change preamble.tex into preamble.sty and use options.

File preamble.sty

\ProvidesPackage{preamble}
\RequirePackage{kvoptions}

% The following defines \ifpreamble@foo (\if<packagename>@<optionname>)
\DeclareBoolOption[true]{foo} % specifying `foo` is the same as `foo=true`

\ExecuteOptions{foo=false} % foo is false by default

\ProcessKeyvalOptions{preamble}

\RequirePackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

\ifpreamble@foo
\fancyhead[R]{Michael's report}
\fi

\endinput

File test.tex (foo enabled)

\documentclass{article}
\usepackage[foo]{preamble} % or \usepackage[foo=true]{preamble}
\usepackage{kantlipsum}
\begin{document}
\section{A}
\kant
\end{document}

File test.tex (foo disabled)

\documentclass{article}
\usepackage{preamble} % or \usepackage[foo=false]{preamble}
\usepackage{kantlipsum}
\begin{document}
\section{A}
\kant
\end{document}
Related Question