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

        \includegraphics[width = 0.8cm]{Simple2}

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.

  • 1
    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 Commented Jun 22, 2012 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
    Commented Jun 23, 2012 at 1:05

1 Answer 1


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.

%%BoundingBox:100 100 172 172
100 100 moveto
72 72 rlineto
72 neg 0 rlineto
72 72 neg rlineto
100 100 moveto
/Times-Roman findfont
72 scalefont
(A) show
%%BoundingBox:100 100 120 120
100 100 moveto
20 20 rlineto
20 neg 0 rlineto
20 20 neg rlineto
100 100 moveto
/Times-Roman findfont
20 scalefont
(B) show




  • Hmm, using saveboxes does not seem to work for pstools (to use psfrag with pdflatex). Any idea about that?
    – quazgar
    Commented Sep 9, 2013 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. Commented Sep 9, 2013 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .