# \ifpdf returns true when using LuaLaTeX

The ifpdf package is supposed to detect if pdfTeX is running:

This package looks for pdfTeX in pdf mode and implements and sets the switch \ifpdf.

(from the ifpdf documentation)

However, when I compile the following MWE with LuaLaTeX, my document reads

This is pdfTeX.

\documentclass{article}

\usepackage{ifpdf}

\begin{document}
\ifpdf
This is pdfTeX.
\else
This is not pdfTeX.
\fi
\end{document}


Is this expected and intended behavior? How do I set up a conditional that only detects pdfTeX? (The MWE works as expected for XeTeX.)

-
Looks OK to me: LuaLaTeX makes PDF files, not DVI. – Joseph Wright Oct 6 '12 at 18:33
@JosephWright Reading the name of the switch literally, yes. But then again, XeTeX produces pdf as well, and the switch returns false for that. Also, the abstract talks about pdfTeX, not pdf output. – doncherry Oct 6 '12 at 18:35
XeTeX always works via the .xdv (extended DVI) format, and the PDF is actually made by xdvipdfmx which is normally as part of the XeTeX run. \ifpdf is about the driver, and in the pdfTeX and LuaTeX cases the 'driver' is direct PDF output. – Joseph Wright Oct 6 '12 at 18:36
use the package ifluatex instead with \ifluatex ... \else ... \fi – Herbert Oct 6 '12 at 18:37

LuaTeX comes from pdfTeX, it also knows the same modes for PDF and DVI and provides nearly all PDF features of pdfTeX. It knows even \pdftexversion and \pdftexrevision. Thus \ifpdf has the same semantics here.

The test \ifpdf is not for testing for an engine. Even if \ifpdf is false, then you can have pdfTeX, but pdfTeX in DVI mode. Usually TeX Live and MiKTeX are also using pdfTeX for the command latex, but generating DVI with pdfTeX in DVI mode.

If you want to know, if pdfTeX and not LuaTeX is running, then you can exclude luatex by \ifluatex of package ifluatex and then testing for \pdftexversion, for example:

\usepackage{ifluatex}
\usepackage{ifpdf}
\ifluatex
\typeout{This is LuaTeX}%
\else
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname pdftexversion\endcsname\relax
\typeout{This is neither LuaTeX nor pdfTeX}%
\else
\typeout{This is pdfTeX in \ifpdf PDF \else DVI \fi mode}%
\fi
\fi


## Testing for package driver options and …

Some TeX engines and modes can be detected at TeX macro level and the packages (or its configuration files) are automatically loading the right drivers, e.g.:

• pdfTeX and LuaTeX in PDF mode
• XeTeX

DVI drivers are different. The DVI driver program is running after the TeX run, thus there is no way of knowing the future. The default is often dvips.

Therefore in many cases you won't need complicate switch structures to differentiate between the different TeX compilers, just loading the package is enough:

\usepackage{color}
\usepackage{graphicx}
\usepackage{hyperref}


In case of package fontspec you need to test for the Unicode aware TeX compilers, for example:

\usepackage{ifluatex}
\usepackage{ifxetex}
\ifluatex
\usepackage{fontspec}
% other LuaTeX setup
\else
\ifxetex
\usepackage{fontspec}
% other XeTeX setup
\else
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{lmodern}
% other font setup
\fi
\fi

-
using package ifxetex is also possible – Herbert Oct 6 '12 at 18:52
Great, thanks! Eventually, I'm looking for a way to have one template that I can use with pdfTeX, luaTeX, or XeTeX, and that loads the packages necessary for each one automatically. My approach after this question is \ifxetex XeTeX \else\ifluatex LuaTeX \else\ifpdf pdfTeX \else something else \fi\fi\fi. Not sure if I should've asked that directly in a question? – doncherry Oct 6 '12 at 18:56
I have added some more cases in the answer, but feel free to ask a new question. – Heiko Oberdiek Oct 6 '12 at 20:05

use the package ifluatex instead with \ifluatex ... \else ... \fi

-