Is it possible to abort loading a package if it's too old?
After looking through the related LaTeX core code I don't see any other method than redefining \ProvidesPackage
. It is the earliest place where the version is known. However, I would reuse the existing macros as much as possible to keep it compatible.
The following code reads the two arguments of, feeds them to the original and then checks the package version as normal, aborting the loading if required. Note that the package version number still gets defined (globally!) and can be used afterwards as well. You should set it to \relax
as shown to mark the package as not loaded.
\documentclass{article}
\makeatletter
\let\origProvidesPackage\ProvidesPackage
\def\ProvidesPackage#1{%
\@testopt{\@ProvidesPackage{#1}}{}%
}
\def\@ProvidesPackage#1[#2]{%
\let\ProvidesPackage\origProvidesPackage
\ifx\\#2\\
\ProvidesPackage{#1}%
\else
\ProvidesPackage{#1}[#2]%
\fi
\@ifpackagelater{fontspec}{2008/08/10}{%
}{%
\endinput
}%
}
\usepackage{fontspec}
\@ifpackagelater{fontspec}{2008/08/10}{%
\expandafter\let\csname [email protected]\endcsname\relax% Mark package as not loaded; this must be after `\usepackage` because of internal LaTeX core code.
}{}%
\makeatother
\begin{document}
\end{document}
Sorry, I did not understand the question - Werner explained it me.
Ok, after you loaded package, you can check its version, for example
\@ifpackagelater{amsmath}{1999/12/20}{\typeout{OK}}{\typeout{BAD}}
See this example:
\documentclass{article}
\RequirePackage{amsmath}
\makeatletter
\@ifpackagelater{amsmath}{1999/12/20}{\typeout{OK}}{\typeout{BAD}}
\@ifpackagelater{amsmath}{2013/12/20}{\typeout{OK}}{\typeout{BAD}}
\makeatother
\begin{document}
Text
\end{document}
The problem here is that you need first to load the package, and only then its version becomes known. Which is ok if you want to kill the compilation for older distributions, but might pose problems otherwise.
In the comments I suggested to write whether the package is ok to a special file, and on the next run to check it before loading the package. Probably first one needs to redefine \RequreXeTeX
to make the first run end.