17

I want to check if my tex document is compiled with LuaTeX (or any other Lua TeX compiler).

The simple approach from the TeXbook is to check the \directlua macro against \@empty, like

\ifx\directlua\@empty{\errmessage{...}}\fi

If I'm running luatex, everything is fine. If I run tex, the compilation fails with my \errmessage -- fine. But if I run latex, my error does not occur, but the compilation fails because of the undefined \directlua environment. Which \ifx statement works for all TeX dialects? And why not the one which works fine with plain TeX, the one from above? Thanks in advance :)

UPDATE

An solution for this is very easy, but I don't know if it's causing any problems in the future.

\ifx\directlua\pgf@gd@undefined@donotuse{%
  \errmessage{an error}\fi

works well with TeX and LaTeX. Is there a way which is more elegant and doesn't need any additional packages? Otherwise I will post this as answer.

1
  • // with expl3, \sys_if_engine_luatex
    – user202729
    Aug 8, 2022 at 13:14

4 Answers 4

14

\@empty is defined in LaTeX, so your test fails. Why don't you use ifluatex.sty? It will work with plainTeX too,

1
  • 4
    @Matten I wouldn't use \undefined with LaTeX. \undefined is safe at the start of a format generation (context is using it there) but in a package it can fail as the probability that a user has defined it is quite high. Use a \veryunlikelythatsomeonewilldefineitcommand from the namespace of your package. Dec 10, 2010 at 9:40
5

Here is a more direct answer that uses iftex (see Section 3):

\usepackage{iftex}
\ifLuaTeX
% ⟨material for LuaTEX⟩
\else
% ⟨material not for LuaTEX⟩
\fi
4

How about iftex? it works fine in plain, and gives you some other useful macros.

It is also included in TeXLive.

1

What about

\ifcsname directlua\endcsname
  Yes, I am running LuaTeX
 \else
 Oh no, I give up
\fi

?

I guess that everybody is running etex with \ifcsname these days... There must be a drawback. But which?

1
  • 6
    This wouldn't work if somebody had tested for the \directlua primitive using \@ifundefined because that defines the command while checking. The ifluatex package also does a few sanity checks to avoid issues with nonstandard catcodes, old LuaTeX versions, etc.
    – Philipp
    Dec 7, 2010 at 23:42

You must log in to answer this question.

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