40

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?

5 Answers 5

41

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.

1
  • 5
    At the time of writing this comment, the iftex package seems to have been revamped to work as a superset of sorts to the other ones mentioned (ifxetex and ifluatex). iftex also provides their checks (\ifxetex, \ifluatex) among others (\ifluahbtex, ...). My use case was to enforce LuaTeX usage and inform users, for which iftex already ships with a solution: \RequireLuaTeX (etc.).
    – Alex Povel
    Oct 29, 2020 at 11:52
14

Packages ifxetex, ifluatex, ifpdf. You can also look e.g. in hyperref to check how it detect which driver it should use.

1
  • 3
    ifpdf just detects pdf or dvi modes, you can not detect engine with it.
    – IRAN
    Mar 12, 2011 at 2:00
12

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.

4

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
2
  • 1
    You’re forgetting pTeX and upTeX
    – egreg
    May 12, 2023 at 6:17
  • 1
    To 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, 2023 at 6:30
3

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:

interface3.pdf snippet

There's no equivalent of \ifTUTeX for checking if the engine supports Unicode tokens as far as I can see, however.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .