LaTeX forum ⇒ Document Classesconditional \RequirePackage

Information and discussion about specific document classes and how to create your own document classes.
Posts: 17
Joined: Mon Mar 31, 2008 3:01 pm

conditional \RequirePackage

Postby lnewton » Tue Apr 29, 2008 8:31 pm

I asked the following question on the LaTeX User's Group in
February but never got a response.

Is there a conditional expression I can put in a style file
by means of which I can execute \RequirePackage only if know
that a package has not already been Provided?

We have a standard package we use with most of our
documentation with the command:


Meanwhile, I was trying to do create a template using the
newlfm package that includes its own instance of
\RequirePackage{fancyhdr}, so that when I do


it stops and tells me that commands from the fancyhdr
package are already defined.

This is fine, and I can alter our style package to retrieve
fancyhdr only if it's not already defined ... but I don't
know how to specify a test for that. I assume this must be

Recommended reading 2021: •
LaTeX Beginner's Guide LaTeX Cookbook
User avatar
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

conditional \RequirePackage

Postby gmedina » Tue Apr 29, 2008 9:25 pm

Maybe these commands could help you:

Posts: 17
Joined: Mon Mar 31, 2008 3:01 pm

conditional \RequirePackage

Postby lnewton » Wed Apr 30, 2008 2:20 pm

Thank you, but no, that's not quite what I'm after. The programmer in me, also Goosens et al, say that \IfFileExists is a test for the physical existence of a file in the file system, i.e., if it's there, then do something and if not do something else. (In the case of LaTeX, probably load it, though I'm sure there are other uses. And \IfFileExists appears to make use of TeX search path, so a full path specifier is not needed.

My problem is one of wanting to \RequirePackage in a style file, which I gather is functionally equivalent to \usepackage in a LaTeX document file, only if that package has not already been loaded.

To provide a comparison from Lisp (Emacs Lisp in this case): packages often have a line at the end that says (provide 'packagename), which I understand to be pretty much the same as \ProvidesPackage in a style file. It's just a label that is presumably added to some list which indicates that the package has been successfully loaded. Therefore, in Lisp, you would do:
(if (featurep 'somepackage)

Also, Lisp has a (require 'somepackage) that works such that it will load conditionally, i.e., it tests (featurep 'somepackage) internally and if it's already be been provided, it skips loading it, which speeds up startup. In Lisp you can use (load "package") or (load-library "package") to load unconditionally.

I don't mean to confuse the issue here; I'm just trying to understand it by comparing what I know.

Since LaTeX includes a \ProvidesPackage command for style files, my assumption is that it would have no purpose if there was no way to test it, e.g., by a corresponding command that might be named \IfPackageProvided. Therefore, one might be able to do something like:


Meanwhile, since I first encountered the original problem I was trying to solve (last February), I switched from using the now-obsolete teTeX distribution to TeXLive, and I can no longer reproduce it. I sincerely doubt that switching distributions in itself could have influenced this, but c'est la vie. It's fixed, but I'd still like to understand this matter for the sake of information and later development.


User avatar
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Re: conditional \RequirePackage

Postby Juanjo » Fri May 02, 2008 12:52 am

You are searching for \@ifpackageloaded, a test that can only be used in the preamble or in a class/package file. For more details look at p. 365 of source2e.pdf, which is a file included in any TeX Live distribution. In my Mac, it is at /usr/local/texlive/2007/texmf-dist/doc/latex/base/.

Return to “Document Classes”

Who is online

Users browsing this forum: No registered users and 1 guest