\makeatletter
  \def\PDFWriteLine{\immediate\write\PDFStream}
  \def\BeforeIncludedStream{%
    \stepcounter{psfig}%
    \xdef\PDFCutFile{\PDFTfigname.tex}%
    \message{Opening PDFStream=\PDFCutFile}%
    \immediate\openout\PDFStream=\PDFCutFile
    \begingroup
      \let\\\@backslashchar
      \let\%\@percentchar
      \edef\{{\string{}%
      \edef\}{\string}}%
      \def\ {\space\space}%
      \PDFWriteLine{\\documentclass{article}}%
      \PDFWriteLine{\\input{tmp.inputs}}%
      \PDFWriteLine{\\pagestyle{empty}}%
      \PDFWriteLine{\\usepackage{amssymb,amsbsy}}%
      \PDFWriteLine{\\setlength{\\topmargin}{-1in}}%
      \PDFWriteLine{\\setlength{\\headheight}{0pt}}%
      \PDFWriteLine{\\setlength{\\headsep}{0pt}}%
      \PDFWriteLine{\\setlength{\\oddsidemargin}{-1in}}%
      \PDFWriteLine{\\setlength{\\evensidemargin}{-1in}}%
      \PDFWriteLine{\\newsavebox{\\PDFTbox}}%
      \PDFWriteLine{\\newlength{\\PDFTheight}}%
      \PDFWriteLine{\\newenvironment{PDFTfig}\{\%}%
      \PDFWriteLine{\ \\begin{lrbox}{\\PDFTbox}\%}%
      \PDFWriteLine{\}\{\%}%
      \PDFWriteLine{\ \\end{lrbox}\%}%
      \PDFWriteLine{\ \\setlength{\\PDFTheight}{\\ht\\PDFTbox}\%}%
      \PDFWriteLine{\ \\addtolength{\\PDFTheight}{\\dp\\PDFTbox}\%}%
      \PDFWriteLine{\ \special{papersize=\\the\\wd\\PDFTbox,%
        \\the\\PDFTheight}\%}%
      \PDFWriteLine{\ \\noindent\\usebox{\\PDFTbox}\%}%
      \PDFWriteLine{\}}%
      \PDFWriteLine{\\begin{document}}%
      \PDFWriteLine{\\begin{PDFTfig}}%
    \endgroup
    \let\ThisStream\WriteStreamLine
  }
  \specialstream{pdfpic}{%
    \PDFWriteLine{\string\end{PDFTfig}}%
    \PDFWriteLine{\streaminfo}%
  }{%
   \psgraphicsinclude
   \global\noprocessfalse
  }
  \providecommand{\PDFTLatexCmd}{latex}
  \providecommand{\PDFTLatexOptions}{-interaction=batchmode}%
  \providecommand{\PDFTDvipsCmd}{dvips}
  \providecommand{\PDFTDvipsOptions}{-Ppdf -G0}
  \providecommand{\PDFTEpstopdfCmd}{ps2pdf}
  \providecommand{\PDFTEpstopdfOptions}{}
  \providecommand{\PDFTRmCmd}{rm}
  \providecommand{\PDFTRmOptions}{}
  \gdef\psgraphicsinclude{%
    \@ifundefined{Fig\thepsfig}{%
      \PDFTWarningNoLine{pdftricks}{%
        ************ PROCESSING FIG: \thepsfig\space **********%
      }%
    }{%
      \noprocess
    }%
    \ifPDFTshell
      \ifnoprocess
      \else
        \IfFileExists{\PDFTfigname.tex}{%
          \immediate\write18{%
            \PDFTLatexCmd\space\PDFTLatexOptions\space\PDFTfigname
          }%
          \PDFTWarning{pdftricks3}{%
            \PDFTfigname.tex converted to \PDFTfigname.dvi%
          }%
        }{}%
        \IfFileExists{\PDFTfigname.dvi}{%
          \immediate\write18{%
            \PDFTDvipsCmd\space\PDFTDvipsOptions\space
            -o \PDFTfigname.eps \PDFTfigname
          }%
          \PDFTWarning{pdftricks}{%
            EPS generated -- \PDFTfigname.eps%
          }%
        }{}%
        \IfFileExists{\PDFTfigname.eps}{%
          \immediate\write18{%
            \PDFTEpstopdfCmd\space\PDFTEpstopdfOptions\space
            \PDFTfigname.eps%
          }%
          \PDFTWarning{pdftricks3}{%
            \PDFTfigname.eps converted to \PDFTfigname.pdf%
          }%
        }{}%
        \immediate\write18{%
          \PDFTRmCmd\space\PDFTRmOptions\space
          \PDFTfigname.aux %
          \PDFTfigname.dvi %
          \PDFTfigname.log %
          \PDFTfigname.eps%
        }%
      \fi
    \fi
    \IfFileExists{\PDFTfigname.pdf}{%
      \begingroup
        \fboxsep\@PDFboxsep
        \fboxrule\@PDFboxrule
        \color{\@PDFgraphiccolor}%
        \fcolorbox{\@PDFgraphiclinecolor}{\@PDFgraphicbackground}{%
          \if@pdfGINwidth
            \includegraphics[%
              width=\@PDFgraphicwidth
            ]{\PDFTfigname}%
          \else
            \if@pdfGINheight
              \includegraphics[%
                height=\@PDFgraphicheight
              ]{\PDFTfigname}%
            \else
              \if@pdfGINscale
                \includegraphics[%
                  scale=\@PDFgraphicscale
                ]{\PDFTfigname}%
              \else
                \includegraphics{\PDFTfigname}%
              \fi
            \fi
          \fi
        }%
      \endgroup
      \global\@pdfGINwidthfalse
      \let\@PDFgraphicwidth\relax
      \global\@pdfGINheightfalse
      \let\@PDFgraphicheight\relax
      \global\@pdfGINscalefalse
      \let\@PDFgraphicscale\relax
    }{%
      \PDFTnopdftrue
    }%
    \gdef\@PDFgraphiclinecolor{white}%
    \gdef\@PDFgraphicbackground{white}%
    \gdef\@PDFboxsep{0pt}%
    \gdef\@PDFboxrule{0pt}%
  }
\makeatother
