TransWikia.com

Insert pgf with referenced png file from a different folder

TeX - LaTeX Asked on December 23, 2021

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:

documentclass{article}
usepackage{pgf}
usepackage{import}

begin{document}


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

end{document}

EDIT:

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.

One Answer

Edit: The matplotlib backend has been updated to emit includegraphics instead of pgfimage. The best solution is to update matplotlib if possible.


Original Answer

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
fig.savefig(output)
_, 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.

Answered by Keith Prussing on December 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP