TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
Then use \ifx\pdfpagewidth\undefined \else ... \fi. This doesn't rely on e-TeX extensions. It will work also with tex executable. – jfbu Feb 10 at 7:42
1  
@vacan1ty: Please unaccept my answer and choose another one – Christian Hupfer Feb 10 at 15:39
up vote 11 down vote accepted

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

share|improve this answer
1  
Thanks that is perfect. – vancan1ty Feb 9 at 15:51
    
@vancan1ty: You're welcome. Happy TeXing – Christian Hupfer Feb 9 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. – jfbu Feb 9 at 22:59
    
@jfbu: I updated after a suggestion by Joseph Wright, but did not test. I reverted to the old version. Thanks. – Christian Hupfer Feb 10 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). – jfbu Feb 10 at 7:02

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.

share|improve this answer
    
Oh my, I could have bet that there's a package ;-) – Christian Hupfer Feb 9 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 at 19:05

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
share|improve this answer
    
excuse me, what does mean Knuth TeX I think it's pdftex and etex now? – touhami Feb 9 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 at 18:38
    
you're right for miktex I was ignore that – touhami Feb 9 at 18:47
    
\ifx\numexpr\undefined. – jfbu Feb 9 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 at 23:35

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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