mirkobunse / critdd

Critical difference diagrams with Python and Tikz
https://mirkobunse.github.io/critdd
Other
22 stars 3 forks source link

Unexpected output with to_file #9

Closed Suchun-sv closed 2 months ago

Suchun-sv commented 3 months ago

Hi mirkobunse,

Thanks for your gorgeous work, I meet a little problem that when I using the linux (Ubuntu 22.04) with pdflatex 3.141592653-2.6-1.40.24, the diagram.to_file will output all the intermediate files (.aux, .log) and .pdf in the current directory rather than the subdirectories.

For example, if I pass the "./output/a.py", actually, there only the .tex file will show in ./output, the other files will produced only in the current directory.

After check the code, I find it is the tikz.py:to_file problem. The original code only use "pdflatex", "-interaction=nonstopmode", "-halt-on-error", f"-output-directory={output_dir}", path, I add the `f"-output-directory={output_dir}", which solves the problem in my side.

def to_file(path, tikz_code):
    """Export the tikz_code to a file."""
    root, ext = os.path.splitext(path)
    output_dir = os.path.dirname(path) # get the output dir
    if ext not in [ ".tex", ".tikz", ".pdf", ".svg", ".png" ]:
        raise ValueError("Unknown file path extension")
    if ext in [ ".pdf", ".svg", ".png" ]:
        path = root + ".tex"
    with open(path, "w") as f: # store the Tikz code in a .tex or .tikz file
        f.write(tikz_code)
    if ext in [ ".tex", ".tikz" ]:
        return None # we are done here
    pdflatex = subprocess.Popen(
        ["pdflatex", "-interaction=nonstopmode", "-halt-on-error", f"-output-directory={output_dir}", path],
        stdout = subprocess.PIPE
    )
    (out, err) = pdflatex.communicate() # convert to PDF
    if pdflatex.returncode:
        print(out.decode())
        raise ExportException(root + ".pdf")
    if ext == ".svg":
        pdf_to_svg = subprocess.Popen(
            ["pdf2svg", root + ".pdf", root + ".svg"],
            stdout = subprocess.PIPE
        )
        (out, err) = pdf_to_svg.communicate() # convert to SVG
        if pdf_to_svg.returncode:
            print(out.decode())
            raise ExportException(root + ".svg")
    elif ext == ".png":
        raise NotImplementedError(f"{ext} export is not yet implemented")

best, suchunsv

mirkobunse commented 2 months ago

Hi suchunsv, thanks for pointing this out! Also, I'm glad to hear that this package is useful to you.

I'll make this change ASAP.

mirkobunse commented 2 months ago

Solved with PR #10

You can now update your existing installation of critdd as follows:

pip install --force-reinstall --no-deps 'critdd @ git+https://github.com/mirkobunse/critdd@main'