28

Is there a way to enforce that a certain version, or a later version, of a package is loaded when building the document? I've found that compiling a document on another platform, where the package versions differ, can cause problems that may or may not be hard to pinpoint. Thus, I would like to be able to say, in the document, that "you need at least this version of this package", and produce an explicit error about it when it doesn't hold true.

Is there any way of doing this?

4

1 Answer 1

30

You can request a minimal version/date using the trailing optional argument of \usepackage[<options>]{<name>}[<min.date>] (or \RequirePackage). However, it will only print a warning text and not cause an error if the installed version is older than the requested version.

This feature is realized in the LaTeX core by the macro \@ifpackagelater{<name>}{<date>}{<yes>}{<no>}, which you can also use yourself (\makeatletter .. \makeatother is required if not used inside a package or class file).

The <date> must be in the format year/month/day, e.g. 2022/08/10 for August 10th, 2022.

You can use the following code to cause an error if the package is older than the given date. However, this only works after the package was loaded.

\makeatletter
\usepackage{<name>}%
\@ifpackagelater{<name>}{<date>}{%
    % Package is new enough
}{%
    \PackageError{<your package or document name>}{Package <name> is to old <...>}%
}
\makeatother

Note that despite the name \@ifpackagelater, AFAIK, the macro checks actually if the package date is later or equal. A more accurate name would be therefore ifpackagenotolder. If you want to check if a package is actually newer than a last version known to you, than simply add one day to the date.


Here an example based on the mwe package which also checks if the package is installed at all:

\IfFileExists{mwe.sty}{%
    \RequirePackage{mwe}%
    \@ifpackagelater{mwe}{2018/03/30}{%
        % Package version new enough
    }{%
        % Package version too old
    }
}{%
   % Package not installed
}
5
  • 1
    After trying it, this answer is not entirely accurate as you must first load the package (i.e. do \usepackage) and then check whether it is too old (i.e. do \@ifpackagelater). The reason being that \@ifpackagelater checks a variable var@<package> which is not available (and will thus produce an error) until you've loaded the package.
    – gablin
    Aug 31, 2012 at 12:52
  • @gablin: I corrected my answer now. Thanks for the feedback. Nov 7, 2012 at 10:31
  • 2
    Also note that the <date> must be supplied as <year/month/day>. Sep 27, 2016 at 20:38
  • can you provide a full example of how this would be used? Aug 8 at 11:31
  • 1
    @FrederickNord: Done Aug 10 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.