# Inserting an image into a figure using psfrag

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.

-
aaaaaaggggggghhhhhhhhhh the basic psfrag mechanism might just actually work for this but you basically have includegraphics being called within its own scope and that almost certainly trashes all the internal macros storing the file name and image size and stuff. The replacement text is unnecesarily complicated (that tabular is doing nothing, but perhaps your real example has more than one cell). I would try declaring a savebox and declaring all the replacement text in that box then just using psfrag replacement just \usebox\mybox that way you are separating the calls to \includegraphics –  David Carlisle Jun 22 '12 at 23:08
It might be a little off but I would suggest using a solution along the lines of drawing-on-an-image-with-tikz. Here instead of drawing you can directly put the images (possibly using \includegraphics{}). Also I would recommend porting your data to pgfplots and draw them in LaTeX directly to avoid such problems. –  percusse Jun 23 '12 at 1:05

## 1 Answer

\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}

-
Hmm, using saveboxes does not seem to work for pstools (to use psfrag with pdflatex). Any idea about that? –  quazgar Sep 9 '13 at 20:28
@quazgar No but whatever pstools is doing you can do by hand, just use a small document as above that just does the psfrag stuff use \pagestyle{empty} and perhaps standalone package if it doesn't conflict. Process that with latex/dvips/pstopdf to get a pdf file of the psfrag processed image, then include that into your main document. (pstools presumably tries to automate that workflow but probably just makes a temporary document with the \usebox it would not have the information of where that box is defined. –  David Carlisle Sep 9 '13 at 20:38