[Tex/LaTex] How to includegraphics inside pspicture

graphicspstricks

I try to put a graphics file inside a pspicture, like this:

\documentclass{article} 

\usepackage[crop=off]{pstricks}
\usepackage{auto-pst-pdf}
\usepackage{graphicx}
\psset{unit=1mm}

\begin{document}

\begin{pspicture}(100,100)
    \psframe[fillcolor=green,fillstyle=solid](0,0)(70,70)
    \rput(10,10){
        \includegraphics[width=50mm,height=50mm]{graphics.jpg}
    }
\end{pspicture}

\end{document}

The PDF file shows only a green square, and I get an error message:

Error: /undefined in Operand stack: Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1977 1 3 %oparray_pop 1976 1 3 %oparray_pop 1960 1 3 %oparray_pop 1852 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- Dictionary stack: --dict:1199/1684(ro)(G)-- --dict:0/20(G)-- --dict:120/200(L)-- --dict:96/300(L)-- --dict:46/200(L)-- Current allocation mode is local Last OS error: No such file or directory Current file position is 88848 GPL Ghostscript 9.18: Unrecoverable error, exit code 1

system returned with code 256

When I put the "includegraphics" line outside the "pspicture", I do not get the error message, but the graphics is shown separately from the green square.

How can I make includegraphics work inside pspicture?

Best Answer

The well known method is as follows. Compile it with xetex.

\documentclass[pstricks,preview,margin=10mm]{standalone} 
\usepackage{graphicx}
\newbox\temp
\savebox\temp{\includegraphics[scale=.5]{bobo.jpg}}

\psset
{
    xunit=\dimexpr\wd\temp/10\relax,
    yunit=\dimexpr\ht\temp/10\relax
}

\begin{document}
\begin{pspicture}[showgrid=top](-5,-5)(5,5)
    \rput(0,0){\usebox\temp}
    \psframe[linecolor=red](-5,-5)(5,5)
\end{pspicture}
\end{document}

enter image description here

Related Question