Created
October 31, 2023 06:54
-
-
Save nmalkin/55e7aaf7311cc3d067fd1aa3a35f00b4 to your computer and use it in GitHub Desktop.
Replace longtable with table in LaTeX generated by Quarto and/or Pandoc
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
#!/usr/bin/env python | |
""" | |
This script patches the .tex generated by Quarto to replace usage of \longtable with vanilla tables, and then compiles that. | |
This is necessary because longtable doesn't support two-column layouts, and even after applying the hacky solution, the PDF output is mangled in such a way that entire pages are skipped. | |
""" | |
import os | |
import shutil | |
import subprocess | |
from pathlib import Path | |
def is_big_table(h): | |
# I only had one big table, but otherwise you'll want some more sophisticated logic. | |
return h == "\\hypertarget{tbl-big}{}\n" | |
def patch_table(in_filename, out_filename): | |
with open(in_filename, "r") as f: | |
lines = f.readlines() | |
out_lines = [] | |
inside_table = False | |
inside_caption = False | |
inside_duplicate_header = False | |
caption_count_opens = 0 | |
table_definition = "" | |
in_big_table = False | |
for line in lines: | |
if inside_table: | |
if inside_duplicate_header: | |
out_lines.append(f"% {line}") | |
if line == "\endhead\n": | |
pass | |
elif line == "\endlastfoot\n": | |
inside_duplicate_header = False | |
elif inside_caption: | |
out_lines.append(line) | |
caption_count_opens += line.count("{") | |
caption_count_opens -= line.count("}") | |
if caption_count_opens <= 0: | |
inside_caption = False | |
out_lines.append(table_definition) | |
else: | |
if line.startswith("\\caption{"): | |
inside_caption = True | |
out_lines.append(line) | |
caption_count_opens = line.count("{") | |
caption_count_opens -= line.count("}") | |
elif line == "\endfirsthead\n": | |
out_lines.append(f"% {line}") | |
inside_duplicate_header = True | |
elif line.endswith("\\noalign{}\n"): | |
out_lines.append(line.replace("\\noalign{}", "%\\noalign")) | |
elif line.startswith("\\end{longtable}"): | |
inside_table = False | |
out_lines.append(line.replace("{longtable}", "{tabular}")) | |
if in_big_table: | |
out_lines.append("\\end{table*}\n") | |
else: | |
out_lines.append("\\end{table}\n") | |
else: | |
out_lines.append(line) | |
elif line.startswith("\\begin{longtable}"): | |
inside_table = True | |
if in_big_table: | |
out_lines.append("\\begin{table*}\n") | |
else: | |
out_lines.append("\\begin{table}\n") | |
table_definition = line.replace("{longtable}", "{tabular}") | |
elif line.startswith("\\hypertarget{"): | |
out_lines.append(line) | |
in_big_table = is_big_table(line) | |
else: | |
out_lines.append(line) | |
with open(out_filename, "w") as f: | |
f.writelines(out_lines) | |
def run_latex(base_name): | |
os.chdir("paper") | |
subprocess.check_call(["pdflatex", base_name]) | |
subprocess.check_call(["bibtex", base_name]) | |
subprocess.check_call(["pdflatex", base_name]) | |
subprocess.check_call(["pdflatex", base_name]) | |
os.chdir("..") | |
def replace_current_output(): | |
current_pdf = Path("_output/paper/paper.pdf") | |
backup_pdf = current_pdf.with_name("paper.bak.pdf") | |
shutil.move(current_pdf, backup_pdf) | |
replacement_pdf = Path("paper/main2.pdf") | |
shutil.move(replacement_pdf, current_pdf) | |
def run(): | |
shutil.copytree( | |
"_output/paper/main_files/figure-latex", | |
"paper/main_files/figure-latex", | |
dirs_exist_ok=True, | |
) | |
patch_table("paper/main.tex", "paper/main2.tex") | |
run_latex("main2") | |
replace_current_output() | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment