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?
Document Classes ⇒ conditional \RequirePackage
conditional \RequirePackage
Maybe these commands could help you:
Code: Select all
\IfFileExists{file}{then-code}{else-code}\InputIfFileExists{file}{then-code}{else-code}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
conditional \RequirePackage
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:
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.
Thanks.
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:
Code: Select all
(if (featurep 'somepackage)(do-this-if-true)(do-this-if-false))
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:
Code: Select all
\IfPackageProvided{somepackage}{true-code-maybe-do-nothing}{\RequirePackage{somepackage}}
Thanks.
Lynn
Re: conditional \RequirePackage
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/.