ltxcmds provides \ltx@ifpackagelater, \ltx@ifclasslater, \ltx@iffilelater
and there is \@ifpackagelater - but is there any "if TeX later" or some other way to (automatically) get the used TeX version (besides looking into the log file etc., of course)?
After the comment of Joseph Wright I now know that I wanted to know the LaTeX format (not the TeX) version. (And he already gave the answer to this question).
Cool: At tex.stackexchanges answers are provided to people who do not even know their question!
- 259,911
- 34
- 706
- 1,036
- 14,890
4 Answers
Recent versions of pdfTeX, and I think all versions of XeTeX and LuaTeX, provide 'version' data for the engine. Something like
\documentclass{article}
\usepackage{ifluatex,ifxetex}
\makeatletter
\def\engineversion@#1#2.{%
\ifnum#2>9\relax
#1.#2.%
\else
0.#1#2.%
\fi
}
\def\engineversion@@#1#2#3{#1.#2#3}
\edef\engineversion{%
\ifxetex
XeTeX:\the\XeTeXversion\XeTeXrevision
\else
\ifluatex
LuaTeX:\expandafter\engineversion@\the\luatexversion.\luatexrevision
\else
pdfTeX:\expandafter\engineversion@@\the\pdftexversion.\pdftexrevision
\fi
\fi
}
\makeatother
\show\engineversion
will show it in a 'nice' way. What this warns you about is that telling what engine version is in use also means worrying about which engine is in use. Without more context, it's hard to be sure quite what effect is required. One possible approach is something like
\def\@ifpdftexlater#1{%
\ifxetex
\expandafter\@secondoftwo
\else
\ifluatex
\expandafter\expandafter\expandafter\@secondoftwo
\else
\ifnum#1>\pdftexversion
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\@secondoftwo
\else
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\@firstoftwo
\fi
\fi
\fi
}
% Use cases
\@ifpdftexlater{150}{\TRUE}{\FALSE} % TRUE on my system
\@ifpdftexlater{150}{\TRUE}{\FALSE} % FALSE on my system
which tests the major pdfTeX version, and is TRUE for any major version greater or equal to the given argument, and FALSE for older versions or for the wrong engine begin used. (This needs the ifluatex and ifxetex packages.) Tests for minor revision is a bit more complex, but the same approach applies.
If you actually want the TeX version, then the banner or similar is the only way to go. Knuth did not provide a \texversion macro, and while it's possible to tell TeX2 from TeX3, that is not much use (as TeX3 was released in 1982!).
- 259,911
- 34
- 706
- 1,036
(Second answer, as it seems the question was not really about TeX version.)
The LaTeX format version is stored in the macro \fmtversion. LaTeX does not provide a direct interface to do a date test on this, but it not too hard to put one together:
\newcommand*\@iflatexlater{\@ifl@t@r\fmtversion}
This is used
\@iflatexlater{2001/09/01}{\TRUE}{\FALSE} % TRUE, I hope!
\@iflatexlater{2011/09/01}{\TRUE}{\FALSE} % FALSE
- 259,911
- 34
- 706
- 1,036
-
1Additional info: After compiling a TeX file with pdflatex, in the log file it is written "
LaTeX2e <2015/01/01> patch level 2", where it is LaTeX2e =\fmtname, 2015/01/01 =\fmtversion, and 2 =\patch@level. – Stephen Jul 22 '15 at 10:08
Since version 2020-10-01 the LaTeX kernel provides the command \IfFormatAtLeastTF{<date>}{<true code>}{<false code>} which executes <true code> if the LaTeX version is <date> or newer and <false code> otherwise. Example:
\documentclass{article}
\IfFormatAtLeastTF{2021/06/01}{%
\newcommand\VersionInfo{This document was compiled using \LaTeX\ version 2021-06-01 or newer.}%
}{%
\newcommand\VersionInfo{This document was compiled using a \LaTeX\ version older than 2021-06-01.}%
}
\begin{document}
\VersionInfo
\end{document}
Furthermore, similar commands \IfClassAtLeastTF{<class name>}{<date>}{<true code>}{<false code>} and \IfPackageAtLeastTF{<package name>}{<date>}{<true code>}{<false code>} are provided for class and package tests.
Note that these three commands do not exist in LaTeX versions older than 2020-10-01. You have to keep this in mind when writing code that may be compiled with older LaTeX versions. In this case, you can e. g. add
\providecommand\IfFormatAtLeastTF{\@ifl@t@r\fmtversion}
to define \IfFormatAtLeastTF if necessary, or test the existence of \IfFormatAtLeastTF (e. g. via \ifcsname or \@ifundefined) and then execute code depending on whether \IfFormatAtLeastTF exists or not.
- 432
If you use pdftex in either PDF or DVI mode (i.e. pdflatex or latex with a modern LaTeX) you could adapt Herbert's answer to my question
Is there a way to detect from inside a package that MiKTeX is used? to extract the TeX version instead.
It uses the primitive \pdftexbanner which prints something like:
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011) kpathsea version 6.0.1
The 3.1415926 is the TeX version and can be extracted as shown in the above link. However, this wouldn't work, in general, with a very old TeX where pdftex isn't used in DVI mode.
- 262,582
\fmtversion. I suspect the breakage you refer to is to do with option lists: there was a bug report a couple of days ago about an internal macro,\@in, that is relevant here. – Joseph Wright Aug 20 '11 at 19:08