6

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

12
  • You can use \iffoo in macros defined in your preamble that are executed later but clearly if you put them at the top level so they are evaluated when the file is input, they must be defined at that point. That is not a restriction of plain tex or etoolbox, just a fact of life. Commented Apr 17, 2013 at 10:20
  • do you mean, you want to have \foofalse in your config file, and then \input{config} followed by \newif\iffoo in your document? this is such an odd idea, i must surely be misunderstanding... Commented Apr 17, 2013 at 10:27
  • Welcome to TeX.sx! Commented Apr 17, 2013 at 10:31
  • @DavidCarlisle Is there maybe a way of overriding default values? Similar to package options set after their definition?
    – Michael
    Commented Apr 17, 2013 at 10:32
  • It mostly depends on what your "conditional definitions" look like. Can you add a couple of examples?
    – egreg
    Commented Apr 17, 2013 at 10:35

1 Answer 1

8

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}
1
  • This exactly what I was looking for. Thanks!!
    – Michael
    Commented Apr 17, 2013 at 11:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .