Skip to content

Commit 0956bac

Browse files
committed
some fixes for filepath
1 parent 56d4a8d commit 0956bac

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

src/tikzplotlib/_files.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import pathlib
1+
from pathlib import Path
22

33

44
def _gen_filepath(data, nb_key, ext):
5-
name = data["base name"] + f"-{data[nb_key]:03d}{ext}"
6-
return pathlib.Path(data["output dir"]) / name
5+
rel_filepath = Path(f"{data['base name']}-{data[nb_key]:03d}{ext}")
6+
7+
if data["rel data path"]:
8+
rel_filepath = Path(data["rel data path"]) / rel_filepath
9+
10+
return Path(data["output dir"]) / rel_filepath, rel_filepath
711

812

913
def new_filepath(data, file_kind, ext):
@@ -25,19 +29,12 @@ def new_filepath(data, file_kind, ext):
2529
if nb_key not in data.keys():
2630
data[nb_key] = -1
2731

28-
data[nb_key] = data[nb_key] + 1
29-
filepath = _gen_filepath(data, nb_key, ext)
32+
data[nb_key] += 1
33+
filepath, rel_filepath = _gen_filepath(data, nb_key, ext)
3034
if not data["override externals"]:
3135
# Make sure not to overwrite anything.
32-
file_exists = filepath.is_file()
33-
while file_exists:
34-
data[nb_key] = data[nb_key] + 1
35-
filepath = _gen_filepath(data, nb_key, ext)
36-
file_exists = filepath.is_file()
37-
38-
if data["rel data path"]:
39-
rel_filepath = pathlib.Path(data["rel data path"]) / filepath
40-
else:
41-
rel_filepath = filepath.name
36+
while filepath.is_file():
37+
data[nb_key] += 1
38+
filepath, rel_filepath = _gen_filepath(data, nb_key, ext)
4239

4340
return filepath, rel_filepath

src/tikzplotlib/_image.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ def draw_image(data, obj):
5252
# Explicitly use \pgfimage as includegrapics command, as the default
5353
# \includegraphics fails unexpectedly in some cases
5454
ff = data["float format"]
55+
# Always use slash in file paths, see
56+
# <https://tex.stackexchange.com/a/18923/13262>
57+
# <https://github.com/nschloe/tikzplotlib/issues/509>
58+
posix_filepath = rel_filepath.as_posix()
5559
content.append(
5660
"\\addplot graphics [includegraphics cmd=\\pgfimage,"
5761
f"xmin={extent[0]:{ff}}, xmax={extent[1]:{ff}}, "
58-
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{rel_filepath}}};\n"
62+
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{posix_filepath}}};\n"
5963
)
6064
return data, content

src/tikzplotlib/_quadmesh.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ def draw_quadmesh(data, obj):
5353
# Explicitly use \pgfimage as includegrapics command, as the default
5454
# \includegraphics fails unexpectedly in some cases
5555
ff = data["float format"]
56+
posix_filepath = rel_filepath.as_posix()
5657
content.append(
5758
"\\addplot graphics [includegraphics cmd=\\pgfimage,"
5859
f"xmin={extent[0]:{ff}}, xmax={extent[1]:{ff}}, "
59-
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{rel_filepath}}};\n"
60+
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{posix_filepath}}};\n"
6061
)
6162

6263
return data, content

tests/test_viridis.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ def plot():
1616
def test():
1717
from .helpers import assert_equality
1818

19-
assert_equality(plot, __file__[:-3] + "_reference.tex")
19+
# test relative data path
20+
assert_equality(
21+
plot,
22+
__file__[:-3] + "_reference.tex",
23+
# tex_relative_path_to_data="data/files"
24+
)

0 commit comments

Comments
 (0)