20

I have some documents using a same custom package. That "header" package loads by default the package fullpage. But I would like to write a new LaTeX document, using the same header, but without loading fullpage, and I don't want to change old files.

Is there a way to add an "if false" condition about options? I mean. I have currently:

\DeclareOption{fullpage}{
  \usepackage{fullpage}
}

But that implies to change all of my old documents to add the fullpage option to my package. I want however to make something like:

if-not-nofullpage {%
  \usepackage{fullpage}
}

And in my new document:

\usepackage[nofullpage]{my-header}

Or perhaps a way to "unload" a previously loaded package:

\usepackage{fullpage} % inconditionally

\DeclareOption{nofullpage} {%
    unload-or-revert-the-fullpage-inclusion
}

How can I do that? (if plain TeX macros are used in your answers, please, with little explanations).

2 Answers 2

26

You have to load fullpage after \ProcessOptions, first of all.

Here's the scheme:

\ProvidesPackage{mypackage}
\newif\ifmypackage@fullpage
\DeclareOption{fullpage}{\mypackage@fullpagetrue}
\DeclareOption{nofullpage}{\mypackage@fullpagefalse}

\ExecuteOptions{fullpage} % default is fullpage

\ProcessOptions\relax

\ifmypackage@fullpage
  \RequirePackage{fullpage}
\fi

Then a document calling either

\usepackage{myclass}

or

\usepackage[fullpage]{myclass}

will load fullpage; a document calling

\usepackage[nofullpage]{myclass}

won't.

Thus you won't have to change your old document, but new ones will be able to avoid loading of fullpage.

4
  • Thanks egreg. What is the difference between \RequirePackage and \usepackage? I've read they are synonyms, but, really synonyms? Why two commands with same meaning?
    – ABu
    Commented Sep 26, 2013 at 18:08
  • 1
    @Peregring-lk In packages or classes \RequirePackage should be used. It's mostly a tradition, but having two separate command names can be used by the LaTeX developers for adding functionality to one of the commands without changing the meaning of the other.
    – egreg
    Commented Sep 26, 2013 at 18:19
  • Your solution works perfect. Elegant, direct and short. Thank you very much.
    – ABu
    Commented Sep 26, 2013 at 18:27
  • 1
    +1 - I took the liberty of taking your example and modifying it to use kvoptions Commented Nov 20, 2015 at 7:52
7

You can also use kvoptions:

\ProvidesPackage{mypackage}

\RequirePackage{kvoptions}
\DeclareBoolOption[true]{fullpage}       % automatically creates \ifmypackage@fullpage
\DeclareComplementaryOption{nofullpage}{fullpage}  % automatically inverses the former
\ProcessKeyvalOptions*{}

\ifmypackage@fullpage
  \RequirePackage{fullpage}
\fi

(based on egreg's example

See also the question on Mutually exclusive options in packages

1
  • Thanks for linking to "Mutually exclusive..." question. It worked like a charm there, the answer provided by "Martin Scharrer"! Commented Aug 9, 2022 at 17:28

You must log in to answer this question.

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