How can I detect inside a TeX file which engine (pdftex/xetex/luatex) is used to compile the file? Is there a LaTeX package that provides this functionality?
- 89,023
- 26
- 255
- 291
5 Answers
Yes, there is — iftex. There are also ifxetex and ifluatex packages.
Internally these packages rely on checking if engine-specific primitives exist, such as \XeTeXversion.
- 22,325
Packages ifxetex, ifluatex, ifpdf. You can also look e.g. in hyperref to check how it detect which driver it should use.
- 327,261
I am using the following code:
%% ==================================================================
%%
%% Macros specific for latex / pdflatex / xetex compilers
%%
\usepackage{ifpdf,ifxetex,ifluatex}
\ifpdf
\typeout{^^J *** PDF mode *** }
\else
\typeout{^^J *** DVI mode ***}
\fi
\ifluatex
\typeout{ *** LuaLaTeX *** ^^J}
% patch \typein
% see http://tex.stackexchange.com/questions/12435/incompatibilities-between-lualatex-and-typein
\makeatletter
\def\@xtypein[#1]#2{%
\typeout{#2}%
\@tempcnta\endlinechar
\endlinechar\m@ne
\read\@inputcheck to#1%
\endlinechar\@tempcnta
\@typein}
\makeatother
% LuaLaTeX specific code
\else\ifxetex
\typeout{ *** XeLaTeX *** ^^J}
% XeLaTeX specific code
\else
\typeout{ *** LaTeX *** ^^J}
\fi\fi
\typein{} % pause to watch result
%% =====================================================================
LuaLaTeX can run both in DVI and PDF modes.
- 13,332
We can ask directly to primitives used in engines. We needn't any LaTeX package.
\ifx\numexpr\undefined\else eTeX is activated\fi
\ifx\mubyte\undefined\else encTeX is activated\fi
\ifx\pdftexversion\undefined\else pdfTeX is working\fi
\ifx\XeTeXrevision\undefined\else XeTeX is working\fi
\ifx\directlua\undefined\else LuaTeX is working\fi
\newif\ifpdftex \newif\ifxetex \newif\ifluatex
\ifx\pdftexversion\undefined\else \pdftextrue \fi
\ifx\XeTeXrevision\undefined\else \xetextrue \fi
\ifx\directlua\udefined\else \luatextrue \fi
\ifluatex \else \chardef\outputmode=0 \fi
\ifpdftex \let\outputmode=\pdfoutput \fi
\ifxetex \chardef\outputmode=1 \fi
\ifnum\outputmode>0 PDF is created\else DVI is created\fi
- 74,238
-
1
-
1To be fair this is roughly how the packages are implemented anyway, so you don't really simplify anything doing this apart for academical purpose. (plus a few odd stuff.) – user202729 May 12 '23 at 06:30
In a sufficiently new version, expl3 is built into the LaTeX format, so commands such as \sys_if_engine_pdftex:TF etc. are available by default:
There's no equivalent of \ifTUTeX for checking if the engine supports Unicode tokens as far as I can see, however.
- 7,143

iftexpackage seems to have been revamped to work as a superset of sorts to the other ones mentioned (ifxetexandifluatex).iftexalso provides their checks (\ifxetex,\ifluatex) among others (\ifluahbtex, ...). My use case was to enforce LuaTeX usage and inform users, for whichiftexalready ships with a solution:\RequireLuaTeX(etc.). – Alex Povel Oct 29 '20 at 11:52