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?

Caramdir
  • 89,023
  • 26
  • 255
  • 291

5 Answers5

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.

Andrey Vihrov
  • 22,325
  • 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 '20 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.

Ulrike Fischer
  • 327,261
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

wipet
  • 74,238
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.

user202729
  • 7,143