[Tex/LaTex] Detect whether running on pdfTeX or Knuth’s TeX for conditional include

macrospdftexplain-tex

Does anyone know a way to detect whether one is running in pdfTeX or Knuth's TeX? I want to be able to conditionally include extra features when a document is being built using pdfTex, but also support original TeX as a fallback.

Something like

\ifdefined\pdfpagewidth
\pdfpagewidth 8.5 true in
\pdfpageheight 11 true in
\fi

would get the job done — however the above relies on the e-TeX extensions and doesn't build on Knuth TeX.

Thanks

Best Answer

Something like this?

Since \csname foo\endcsname expands to \relax if \foo is not defined, it's possible use \ifx.... to compare the command sequence to be equal to \relax. This does not need e-TeX at all.

\newif\ifknuthtex
\expandafter\ifx\csname pdfpagewidth\endcsname\relax
\knuthtextrue
\else
\pdfpagewidth 8.5 true in
\pdfpageheight 11 true in
\fi

This is \ifknuthtex Knuth's \TeX\else pdfTeX\fi
\bye

Compiling with tex gives the image below.

enter image description here

Related Question