Skip to content

tex_relative_path_to_data makes slashes in Windows style, which is not accepted by LaTeX. #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
IsabellLehmann opened this issue Sep 9, 2021 · 6 comments · Fixed by #530

Comments

@IsabellLehmann
Copy link

Hi,

When I give an argument to tex_relative_path_to_data, the slashes in the .tex-file are in Windows style but LaTeX does not accept them. Would be good if they are these '/' also for Windows PCs.

Furthermore, I have no idea why there is images/images in the path. I think it should be there only once.

Here you find:

  • the Python code (make sure a folder images exists in the folder where you run the code),
import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib

plt.imshow(np.random.randn(20,20))
tikzplotlib.save('images/test.tex', tex_relative_path_to_data='images', override_externals=True)
  • the output TeX (Pgfplots), and
% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=19.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=19.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=19.5, ymin=19.5, ymax=-0.5] {images\images\test-000.png};
\end{axis}

\end{tikzpicture}
  • the expected TeX (Pgfplots).
% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=19.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=19.5,
ytick style={color=black}
]
% the next line has the difference
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=19.5, ymin=19.5, ymax=-0.5] {images/test-000.png};
\end{axis}

\end{tikzpicture}

Thanks for any suggestions besides manually adjusting the path in each image!
Isabell

@Blubbaa
Copy link

Blubbaa commented Dec 3, 2021

I am also not entirely sure of how this is supposed to behave. It seems like the option tex_relative_path_to_data will be just prepended to all paths used in the tikz file. So my workaround on Windows is to use tex_relative_path_to_data for the ./plots/ directory within my tex directory and another directory ./plots/data/ within, containing all additional data, images, tables etc.

import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib

plt.imshow(np.random.randn(20,20))
tikzplotlib.save('./data/test.tex', tex_relative_path_to_data='plots', override_externals=True)

Doing so, will save all files to a sub directory called ./data/ and you will have to manually move the files to the corresponding tex directories (./plots/ for tex file and ./plots/data/ for all others). This is still cumbersome but I'd rather move some files manually / via scripts than edit all auto-generated tikz files.

Regarding the issue with slashes, I cannot replicate the same behaviour, for me the above code yields correctly:

% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=19.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=19.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=19.5, ymin=19.5, ymax=-0.5] {plots/data/test-000.png};
\end{axis}

\end{tikzpicture}

@IsabellLehmann
Copy link
Author

IsabellLehmann commented Dec 3, 2021

Thanks @Blubbaa ! Will try it out next time I need to generate tikz images :)

Strange that this error does not occur with you ... Your code generates for me:

% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=19.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.0196078431373!black},
ymin=-0.5, ymax=19.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=19.5, ymin=19.5, ymax=-0.5] {plots\data\test-000.png};
\end{axis}

\end{tikzpicture}

Maybe I have configured my Python wrongly. Both running the code in a Jupyter notebook and in the PyCharm console results in the above...

@Blubbaa
Copy link

Blubbaa commented Dec 3, 2021

Ok, I did some investigations, it seems I encountered this issue before and just couldn't remember 😄

There was an open PR (#473) some time ago, which proposed to fix the problem here:

if data["rel data path"]:
rel_filepath = pathlib.Path(data["rel data path"]) / filepath
else:
rel_filepath = filepath.name
return filepath, rel_filepath

by adding some string replacement for Windows paths. Back then I have manually edited the local source from tikzplotlib and added this line in L42:

    # FIX: for windows relative paths (see https://github.com/nschloe/tikzplotlib/pull/473)
    rel_filepath = str(rel_filepath).replace('\\', '/')

It seems like that this should have been fixed already, but apparently is not.

@nschloe
Copy link
Owner

nschloe commented Dec 3, 2021

I'll look at it later.

@Blubbaa
Copy link

Blubbaa commented Dec 23, 2021

I believe the support for .dat files was forgotten in the fix. Instead of rel_filepath here

if len(opts) > 0:
opts_str = ",".join(opts)
content.append(f"table [{opts_str}] {{{rel_filepath}}};\n")
else:
content.append(f"table {{{rel_filepath}}};\n")

posix_filepath = rel_filepath.as_posix() should also be used instead. I am not sure if there are any other similar issues with different styles.

Don't know if I should open a new issue or just reuse the old one.

@nschloe
Copy link
Owner

nschloe commented Dec 23, 2021

@Blubbaa Thanks for the notification. Fixed in #533.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants