I'm trying to insert a pgf file that I made with matplotlib. This file references to a png file (since I inserted a png figure in the plot).

When I put the png in the same folder than my tex file, everything works, but if the png file is in the folder with the pgf file, i got an error

Package pgf Warning: File "plot2.png" not found when def
ining image "pgflastimage".

I was trying to use import but I don't know how it works for a pgf. If I change pgf by tex, it still doesn't find the png:



% \subimport{../pgf/}{plot.pgf} --> Not working



Changing \pgfimage{...} by \includegraphics{...} in the pgf file, seems to work with subimport* . However, matplotlib produces the pgf with pgfimage, so it would be necessary to change the output plots by hand. Maybe someone knows a better solution.

  • Where? In the pgf file? =o – PerroNoob Jul 29 '14 at 22:22
  • Same problem here. I didn't find a proper answer yet.. – user62681 Sep 17 '14 at 16:21

From what I found, there is no better way. This answer notes that pgfimage uses includegraphics, but does not propagate the relative paths. Looking through the matplotlib pgf backend source, changing pgfimage to includegraphics does not appear to be a configurable option on the matplotlib side of the equation.

A work around I came up with was to intercept the output and update the appropriate lines.

# lots of plotting commands
_, ext = os.path.splitext(output)
if ext == ".pgf":
    with open(output, "r") as fid:
        lines = fid.readlines()

    with open(output, "w") as fid:
        for line in lines:
            fid.write(re.sub(r"\\pgfimage", r"\\includegraphics", line))

This is really only a “solution” if you can edit the plotting routines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.