How to tell which package is defining \ifpdf?

I'm experiencing a problem similar to this one: Package ifpdf Error

After upgrading to a more recent version of TeX Live, my pre-existing documents stopped compiling, with complaints that `\ifpdf` was already defined. My project is large and pulls in a lot of packages, so I don't know which one is defining `\ifpdf`. Using `\let\ifpdf\relax` after the `\documentclass`, as suggested in the answers to the other question, fixes the problem on the newer TeX Live. However, this breaks the document on older versions of TeX Live, where I still need it to compile for various reasons. (I think I'm getting malformed PDFs from pdfTeX 3.1415926-2.4-1.40.13, but that's another topic.)

Is there any simple, straightforward way of finding out which package is defining `\ifpdf` when it shouldn't be? I could presumably figure it out by trial and error, by bisection, but this is a large, complicated project, and that would be a lot of work. If that worked, would knowing this allow me to make my file compile on both versions of TeX Live?

Is there some other way of making a more robust fix? Can I somehow test somewhere to see whether `\ifpdf` has been defined, and then define it conditionally at that point?

[EDIT] I figured out the problem. It wasn't another package, it was some code in my own .cls file, which I'd cut and pasted from somewhere probably 15 years ago:

``````\newif\ifpdf
\ifx\pdfoutput\undefined
\pdffalse % we are not running PDFLaTeX
\else
\pdfoutput=1 % we are running PDFLaTeX
\pdftrue
\fi

\ifpdf
\usepackage[pdftex]{graphicx}
\else
\usepackage{graphicx}
\fi
\AtBeginDocument{
\ifpdf
\DeclareGraphicsExtensions{.pdf,.jpg,.png}
\else
\DeclareGraphicsExtensions{.eps,.jpg,.png}
\fi
}
``````

I was able to make it run on both (modern) versions of texlive by shortening this to the following:

``````\usepackage{graphicx}
\AtBeginDocument{
\DeclareGraphicsExtensions{.pdf,.jpg,.png}
}
``````

This probably breaks compatibility with dvi-flavored tex, but I don't care about that.

-
You could try using a condition: `\ifx\ifpdf\undefined\else\let\ifpdf\relax\fi` – Werner Jan 30 at 4:06
@Werner: Thanks for the suggestion, but that caused an error on the old texlive, where `\ifpdf` wasn't being defined and needed to be. I edited the question to show the solution I found that actually worked. – Ben Crowell Jan 30 at 6:37