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


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 :)


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

  \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.


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

  • @Ulrike -- thanks for your advice. I read the manual of ifluatex, is it included in texlive standard distributions? We cannot rely on packages which must be installed separately...
    – Matten
    Dec 7 '10 at 14:29
  • I don't have TeXLive but ifluatex is part of the oberdiek bundle and e.g. hyperref and fontspec both load it so I'm pretty sure that is present in every current TeXLive. Dec 7 '10 at 15:12
  • In TeXlive 2009 it is included, but I have to check with the maker of TikZ if it's okay to rely on other packages as he wants to keep the dependencies as minimal as possible! Thanks for your advice
    – Matten
    Dec 7 '10 at 19:02
  • @Matten If you are assuming that people will be using new versions of PGF, I'd go so far as to say that you can be sure they'll have ifluatex.sty installed. Dec 8 '10 at 2:16
  • 3
    @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 '10 at 9:40

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

It is also included in TeXLive.


What about

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


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

  • 5
    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 '10 at 23:42
  • For completeness, you can simply add \ifx\directlua\relax No give up. \else Yes!\fi to the true branch of your text. Oct 22 '11 at 1:57

Here is a more direct answer that uses iftex (see section 3.3):

% ⟨material for LuaTEX⟩
% ⟨material not for LuaTEX⟩

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.