tikzplotlib
tikzplotlib copied to clipboard
Relative path between TeX and image outputs
trafficstars
I have the following structure:
├── images
│ ├── plot-000.png
│ └── plot.tex
├── main.tex
├── Makefile
└── plot.py
main.tex:
\documentclass{article}
%\usepackage[luatex]{graphicx}
%\graphicspath{{images/}}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.16}
\begin{document}
\input{images/plot.tex}
\end{document}
Makefile:
all:
python plot.py
lualatex main.tex
plot.py:
import numpy as np
import matplotlib.pyplot as plt
import os
import tikzplotlib
x = np.linspace(0.0, 1.0, 500)
y = np.linspace(0.0, 1.0, 500)
x_2d, y_2d = np.meshgrid(x, y)
step = np.where((0.25 <= x_2d) & (x_2d <= 0.75) & (0.25 <= y_2d) & (y_2d <= 0.75), 1.0, 0.0)
plt.imshow(step)
tikzplotlib.save(
"images/plot.tex",
# tex_relative_path_to_data="images/",
# tex_relative_path_to_data=os.path.abspath("images/"),
)
generated plot.tex:
% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}
\definecolor{darkgray176}{RGB}{176,176,176}
\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xmin=-0.5, xmax=499.5,
xtick style={color=black},
y dir=reverse,
y grid style={darkgray176},
ymin=-0.5, ymax=499.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=499.5, ymin=499.5, ymax=-0.5] {plot-000.png};
\end{axis}
\end{tikzpicture}
I would like generated *.tex files to be stored in images/ together with their *.png companions.
However, if I do so (with the images/ prefix in tikzplotlib.save), when compiling main.tex I get:
Package pgf Warning: File "plot-000.png" not found when defining image "pgflast
image". Tried all extensions in ".pdf:.jpg:.jpeg:.png:" on input line 18.
because in plot.tex the *.png is included without path.
- Adding
tex_relative_path_to_data="images/"totikzplotlib.savedoes not work, I get the error
FileNotFoundError: [Errno 2] No such file or directory: 'images/images/plot-000.png'
- Adding
\graphicspath{{images/}}tomain.texdoes not work, because for some reasontikzplotlibforces theincludegraphics cmdto\pgfimage(which does not seem to respect\graphicspath) rather than\includegraphics. - There's an option
table/search pathin PGF for\addplot table, but there doesn't seem to be an equivalent for\addplot graphics. - I can make it work with
tex_relative_path_to_data=os.path.abspath("images/")intikzplotlib.save, but that's ugly and non-portable.
Is there a good way to make this work?
Note: I could generate plot.tex to the top-level directory, but that's seems less logical conceptually, plus it's less convenient e.g. for VCS integration.
A couple independent ideas in case tikzplotlib has to be modified for this:
- Make the choice of
\pgfimagevs\includegraphicsaccessible by a parameter - Always use
tex_relative_path_to_datain the generated*.tex(but that's not robust to moving generated files around) - Use LaTeX's
\CurrentFilePathand friends - Request a
graphics/search pathfeature by PGF - Request that
\pgfimagerespects\graphicspathby PGF or graphicx - Add yet another target folder parameter to
tikzplotlib.save