-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
21 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |