views:

209

answers:

4

Is there a way, in a LaTeX style/class file, to detect which output device is being used (or at least which capabilities it has)? The reason is, I'm writing a class file in which I want to use some Postscript-specific packages (e.g. pstricks) if Postscript is available, but if I just write

\RequirePackage{pstricks}

then of course bad things happen if I'm compiling the document with pdflatex. So ideally I'm looking for something I can use like

\if@postscriptokay\RequirePackage{pstricks}\fi

It seems like this must be possible because I know packages like pgf can change their behavior to use appropriate graphics commands based on the output device, but I've done some Google searches and checked in my LaTeX book and haven't found a way.

+5  A: 
\usepackage{ifpdf}

\ifpdf
  % nothing
\else
  \RequirePackage{pstricks}
\fi
Ben Alpert
This is the best you can do. Also see ifxetex, ifluatex.
Will Robertson
+5  A: 

You can detect pdfTeX like this (this is what ifpdf.sty does):

\makeatletter
\ifx\pdfoutput\@undefined
  no pdfTeX
\else\ifnum\pdfoutput<1
  pdfTeX is outputting a .dvi file
\else
  pdfTeX is outputting a .pdf file
\fi\fi

graphicx.sty, hyperref.sty and pgf.sty have their own autodetection mechanisms built in. They load a different driver file (like pdftex.def and hpdftex.def) based on the autodetection and the package option. If you load any of these in your .tex file, try to get the information which driver they loaded. The relevant driver files are:

/usr/share/texmf/tex/generic/pgf/systemlayer/pgfsys-*.def
/usr/share/texmf-texlive/tex/latex/hyperref/hpdftex.def
/usr/share/texmf-texlive/tex/latex/graphics/*.def
  /usr/share/texmf-texlive/tex/latex/pdftex-def/pdftex.def

The name of the driver for pgf.sty and graphicx.sty is stored in the macro \Gin@driver. You can inspect this macro after loading any of these packages.

pts
+1 nice explanation, although just using ifpdf is easier in practice (that's why I accepted the other answer).
David Zaslavsky
A: 

If you are using the KOMA-Script package (which I can really recommend, unless you need to use a different style of course) you already have a macro for this: \ifpdfoutput{pdf output}{dvi output} is defined in that package. If not, use the ifpdf package as has already been mentioned.

bluebrother
+2  A: 

Have a look at packages pstool and auto-pst-pdf. They are here to help use pstricks and friends with pdfLaTeX.

Damien Pollet
Actually I've never tried using pstool with pstricks; probably needs a bit more glue to make it user friendly. Let me know if otherwise!
Will Robertson