LaTeX forum ⇒ Document Classesconditional \RequirePackage

Information and discussion about specific document classes and how to create your own document classes.
lnewton
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:

\RequirePackage{fancyhdr}

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

\documentclass{newlfm}
\uspackage{ourstylefile}

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
possible?
Lynn

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

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

Maybe these commands could help you:
  1. \IfFileExists{file}{then-code}{else-code}
  2. \InputIfFileExists{file}{then-code}{else-code}
1,1,2,3,5,8,13,21,34,55,89,144,233,...

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

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:
  1. (if (featurep 'somepackage)
  2. (do-this-if-true)
  3. (do-this-if-false))

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:

  1. \IfPackageProvided{somepackage}{true-code-maybe-do-nothing}{\RequirePackage{somepackage}}

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.

Thanks.
Lynn

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

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 4 guests