I'm working on a package that has to work on both TeXlive 2009 (Ubuntu, so updating TeXlive is a nonstarter) and later versions, and I'd like to be able to use fontspec with LuaLaTeX if we're on TeXlive 2010 or later, but \RequirePackage{fontspec}[2008/08/10] (Ubuntu's TeXlive 2009 ships with a fontspec.sty from 2008/08/09) still tries to load fontspec.sty and the date argument only serves to make LaTeX issue a warning that it's too old - after barfing out errors because fontspec.sty has \RequireXeTeX in it.
Is there a way to abort loading a package if it's too old, or do I have to do something borderline insane involving (temporarily) redefining \ProvidesPackage. I have the following example cooked up, but I'd love to replace it with something saner:
\listfiles
\documentclass{article}
%\usepackage{trace}
\def\XID#1{\begingroup\ifdefined #1\aftergroup#1\fi\endgroup}
\XID\traceon
\let\OPP\ProvidesPackage
\def\ProvidesPackage#1[#2/#3]{%
\let\ProvidesPackage\OPP
\let\OPP\undefined
\begingroup
\ifnum #2 < 2009 % if fontspec is older than 2009, abort
\aftergroup\endinput
\XID\traceoff
\fi
\endgroup
\XID\traceoff
\ProvidesPackage#1[#2/#3]}
\usepackage{fontspec}
\ifdefined\setmainfont
\message{^^J*** \string\setmainfont\space defined! ***^^J}
\else
\message{^^J*** \string\setmainfont\space not defined! ***^^J}
\fi
\begin{document}
\end{document}
EDIT: After some fairly intense hacking, I came up with this shorter and saner replacement:
\documentclass{minimal}
\makeatletter
\def\IfNewer#1#2#3#4{% we chain into #5 if true and #6 if false using \@{first,second}oftwo.
\begingroup %that we might forget the craziness that is to follow...
\def\NeedsTeXFormat##1{\@ifnextchar[\relax\relax}
% This is rather brittle and should probably use \@ifnextchar[
\def\ProvidesPackage##1[##2/##3/##4 ##5]{%
\newif\if@goahead % defaults to false, obviously.
% Ugly test, but it works
\ifnum ##2 < #2 % Year too old
\else\ifnum ##2 = #2\ifnum ##3 < #3 % Year OK, month too old
\else\ifnum ##2 = #2\ifnum ##3 = #3\ifnum ##4 < #4 % Year & month OK, day too old
\else
\@goaheadtrue
\fi\fi\fi\fi\fi\fi
\endinput}
\input #1.sty % use TeX's \input to avoid #1.sty being in \listfiles
\if@goahead
\endgroup % \if@goahead and our redifinitions disappear in a puff of logic.
\expandafter\@firstoftwo
\else
\endgroup
\expandafter\@secondoftwo
\fi}
\makeatother
\IfNewer{fontspec}{2010}{01}{01}
{\message{^^J*** New enough! ***^^J}}
{\message{^^J*** Too old! ***^^J}}
\begin{document}
\end{document}
\let\setmainfont\relax, then obviously your test fails. – IRAN Nov 06 '11 at 06:28\ProvidesPackageseems to be the only way to do it. My first idea was to hack the warning into a\endinput, but it is only executed after the file was full loaded as well. – Martin Scharrer Nov 06 '11 at 07:58