Skip to content

Commit

Permalink
Repair the latex table generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Yann21 committed Sep 16, 2024
1 parent d9fdb72 commit f4df56f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#
yann_utils/out/*
yann_utils/latex_table.png

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
Empty file added yann_utils/out/.gitkeep
Empty file.
33 changes: 17 additions & 16 deletions yann_utils/phd.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
#%%
import pandas as pd
import subprocess
import os


def csv_to_latex_png(df, png_filename, caption):
# 1. Convert the df to LaTeX table format
latex_code = r"""
\documentclass{standalone}
\usepackage{booktabs}
\begin{document}
"""
latex_code += df.to_latex(index=True, escape=False, caption=caption)
latex_code = r"""\documentclass{standalone}
\usepackage{booktabs}
\usepackage{preview}
\PreviewEnvironment{tabular}
\begin{document}
"""
latex_code += df.to_latex(index=True, escape=True, caption=caption)
latex_code += r"\end{document}"

with open("temp_table.tex", "w") as latex_file:
with open("out/temp_table.tex", "w") as latex_file:
latex_file.write(latex_code)

# 2. Compile the LaTeX to produce a PDF
subprocess.call(["pdflatex", "-interaction=nonstopmode", "temp_table.tex"])
subprocess.call(["pdflatex", "-interaction=nonstopmode", "-output-directory=out", "temp_table.tex"])
subprocess.call(["pdftoppm", "-png", "out/temp_table.pdf", "temp_table"])
os.rename("temp_table-1.png", "latex_table.png")

# 3. Convert the PDF to PNG
subprocess.call(["pdftoppm", "-png", "temp_table.pdf", "temp_table"])

# Rename to the desired png filename
os.rename("temp_table-1.png", png_filename)
#%%
df = pd.read_clipboard()
csv_to_latex_png(df, "latex_table.png", "Reset algorithm")

# Clean up intermediate files
for ext in [".tex", ".aux", ".log", ".pdf"]:
os.remove("temp_table" + ext)
#%%
df

0 comments on commit f4df56f

Please sign in to comment.