18

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

  • Then use \ifx\pdfpagewidth\undefined \else ... \fi. This doesn't rely on e-TeX extensions. It will work also with tex executable. – user4686 Feb 10 '16 at 7:42
  • 1
    @vacan1ty: Please unaccept my answer and choose another one – user31729 Feb 10 '16 at 15:39
11

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

  • @vancan1ty: You're welcome. Happy TeXing – user31729 Feb 9 '16 at 15:51
  • 1
    This doesn't work. There is a missing \expandafter. Try with tex and you will get an error. I guess the image was generated before the update. Besides \ifx\pdfpagewidth\undefined works equally well. – user4686 Feb 9 '16 at 22:59
  • @jfbu: I updated after a suggestion by Joseph Wright, but did not test. I reverted to the old version. Thanks. – user31729 Feb 10 '16 at 1:10
  • @ChristianHupfer the problem with the reverted-to old version is that it leaves then control sequence \pdfpagewidth defined with meaning \relax if you run by Knuth's tex. This could fool later tests or force them to act like LaTeX @ifundefined (which does like your old code). The idea with the \begingroup...\endgroup and suitable \expandafter's is to avoid this after-effect. But \ifx\pdfpagewidth\undefined has no such after-effect and works. However it would be falsified if code like your reverted-to old version (now current) was executed earlier (or LaTeX \@ifundefined). – user4686 Feb 10 '16 at 7:02
  • @jfbu: I'll investigate this. Apparently I am missing some point here I don't see at the moment. I asked the O.P. to unaccept and choose a better answer instead – user31729 Feb 10 '16 at 19:58
13

You can use the iftex "package". According to the documentation

This very simple package, for both Plain TeX and LaTeX, defines the \ifPDFTeX, \ifXeTeX, and \ifLuaTeX boolean for testing whether PDFTeX, or XeTeX, or LuaTeX is being used for typesetting.

  • Oh my, I could have bet that there's a package ;-) – user31729 Feb 9 '16 at 16:15
  • @ChristianHupfer there are also ifluatex and ifxetex packages, although, I am not sure why you would use them over iftex. – StrongBad Feb 9 '16 at 19:05
6

If you only plan to distinguish between Knuth TeX and pdftex (or LuaTeX), this does it:

\begingroup\escapechar=-1
\edef\undefined{\string\undefined}%
\edef\test{\meaning\pdftexversion}%
\expandafter\endgroup\expandafter\let\expandafter\ifknuthtex
\csname if\ifx\undefined\test true\else false\fi\endcsname

\ifknuthtex
  \message{Knuth TeX}
\else
  \message{Not Knuth TeX}
\fi

One assumption is made: that no previous macro file or loaded format defines \pdftexversion. If you want that \ifknuthtex returns the correct truth value also with XeTeX, you can load ifxetex:

\input ifxetex.sty
\begingroup\escapechar=-1
\edef\undefined{\string\undefined}%
\edef\test{\meaning\pdftexversion}%
\expandafter\endgroup\expandafter\let\expandafter\ifknuthtex
\csname if%
  \ifnum 0=%
    \ifx\undefined\test 0\else 1\fi
    \ifxetex 1\fi
    true\else false\fi
\endcsname

\ifknuthtex
  \message{Knuth TeX}
\else
  \message{Not Knuth TeX}
\fi
  • excuse me, what does mean Knuth TeX I think it's pdftex and etex now? – touhami Feb 9 '16 at 18:37
  • 1
    In TeX Live (and also in MiKTeX, I guess), calling tex launches the original program with no extension and we use to call it “Knuth TeX” for better clarity. – egreg Feb 9 '16 at 18:38
  • you're right for miktex I was ignore that – touhami Feb 9 '16 at 18:47
  • \ifx\numexpr\undefined. – user4686 Feb 9 '16 at 23:05
  • 1
    @jfbu Not really: you can make a format for pdftex that doesn't load e-TeX extensions, but it's not the same as Knuth TeX nonetheless. Try pdftex -ini '\show\numexpr' – egreg Feb 9 '16 at 23:35
6

For your problem, I strongly suspect that:

\ifx\pdfpagewidth\undefined
\else
  \pdfpagewidth 8.5 true in
  \pdfpageheight 11 true in
\fi

is, for all intents and purposes, quite fine enough. No need for complications.

Solutions based on a \csname..\endcsname approach have the drawback that they actually create a control sequence with meaning \relax, if the control sequence did not exist beforehand. Hence people do tricks with a bunch of \expandafter's to have this meaning exist only briefly and then disappear after the \ifx test. However the TeX memory will have a new entry, even for such a briefly existing control sequence, as also does the simple fact of writing \pdfpagewidth in your source code (if not commented out).

Unfortunately as soon as some code does the \csname..\endcsname approach without the alluded-to precautions, the simple \ifx\pdfpagewidth\undefined test above will become dysfunctional. And this is the main reason, I guess, that people are reluctant to recommend it. One should be aware that LaTeX's \@ifundefined creates precisely this problem. But on modern installations, latex executable is using pdftex binary (with in particular e-TeX extensions enabled), hence this is not Knuth TeX. On more ancient installations naturally you can have Knuth TeX + LaTeX format.

Note also that sometimes one may think (depending possibly on how your IDE presents things) that one is executing the Knuth TeX tex, whereas in fact it is pdftex in dvi output mode.

The \ifx\pdfpagewidth\undefined could also be compromised by some earlier code which has given a meaning to \undefined, but that would be very condemnable thing.

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.