[Tex/LaTex] Inserting an image into a figure using psfrag

MATLABpsfrag

I'm trying to import figures from Matlab into a LaTeX document using PSFrag. For some of the figures I'd like to be able to add little images in particular places. I thought I'd be able to just add dummy tags in Matlab and replace them, as in

\psfrag{s14}[lt][lt]{%
    \color[rgb]{0,0,0}
    \setlength{\tabcolsep}{0pt}
    \begin{tabular}{l}
        \includegraphics[width = 0.8cm]{Simple2}
    \end{tabular}}%

but for some reason it replaces the tag with the base figure (as in, the big plot I'm trying to add little images to) instead of the little image.

Any solutions greatly appreciated.

Best Answer

enter image description here

\includegraphics uses a lot of internal macros and registers to record the state of the image being included and it was never designed to be recursively called during its own processing. Hence as you observe it loses track of the file being processed (and probably lots of other details). Using a box register you can avoid the need for the nested macro expansion and (amazingly enough:-) it all works out. The above shows A in the original EPS being replaced by the rendering of the file b.ps.

\begin{filecontents*}{a.ps}
%!
%%BoundingBox:100 100 172 172
100 100 moveto
72 72 rlineto
72 neg 0 rlineto
72 72 neg rlineto
stroke
100 100 moveto
/Times-Roman findfont
72 scalefont
setfont
(A) show
showpage
\end{filecontents*}
\begin{filecontents*}{b.ps}
%!
%%BoundingBox:100 100 120 120
100 100 moveto
20 20 rlineto
20 neg 0 rlineto
20 20 neg rlineto
stroke
100 100 moveto
/Times-Roman findfont
20 scalefont
setfont
(B) show
showpage
\end{filecontents*}
\documentclass{article}
\usepackage{psfrag}
\newsavebox\mybox

\begin{document}

\savebox\mybox{\includegraphics{b}}
\psfrag{A}{\usebox\mybox}

\includegraphics{a}

\end{document}
Related Question