diff --git a/scripts/csv2latex b/scripts/csv2latex deleted file mode 100755 index 73cf5e4..0000000 --- a/scripts/csv2latex +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/env python3 -import argparse -import csv -from pathlib import Path -import sys - - -def get_args(): - parser = argparse.ArgumentParser() - parser.add_argument( - "input", - help="input csv file", - ) - parser.add_argument("output", help="output tex file") - parser.add_argument("--label", help="table label", required=True) - parser.add_argument("-c", "--caption", help="table caption", default="") - parser.add_argument("--split", help="column number to split using splitseq") - parser.add_argument("--fill", help="expand to fill page", action="store_true") - parser.add_argument("--fmt", help="column format to use with tabular") - return parser.parse_args() - - -def hyphen(v): - return r"\-".join([v[i : i + 3] for i in range(0, len(v), 3)]) - - -def splitseq(v): - if " " in v: - return v - if "**" in v: - start, n, end = v.split("**") - return rf"{hyphen(start)}\-\textbf{{{hyphen(n)}}}\-{hyphen(end)}" - else: - return hyphen(v) - - -def main(): - args = get_args() - path_to_csv = Path.cwd() / args.input - path_to_tex = Path.cwd() / args.output - caption = args.caption - - rows = [] - - # get header and row information from csv - with path_to_csv.open(mode="r") as f: - reader = csv.DictReader(f) - if not args.fmt: - column_fmt = " ".join(["c"] * len(reader.fieldnames)) - else: - column_fmt = args.fmt - width = r"width=1.1\textwidth,center=.5\textwidth" if args.fill else "" - headers = [rf"\textbf{{{header}}}" for header in reader.fieldnames] - header_str = " & ".join(headers) + r" \\" - - for row in reader: - # split sequences so they will wrap nicer in the table - if args.split: - row_str = ( - " & ".join( - [ - splitseq(v) if i + 1 == int(args.split) else v - for i, v in enumerate(row.values()) - ] - ) - + r" \\" - + "\n" - ) - else: - try: - row_str = " & ".join(row.values()) + r" \\" + "\n" - except TypeError: - print(f"Error:{row.values()}") - print( - "there was an issue with this row, does it have a value for each column?" - ) - sys.exit(1) - - rows.append(row_str) - - rows = "".join(rows) - label = args.label - table_tex_str = rf""" -%... -% generated using csv2latex - -% DO NOT edit manually -\setlength{{\tabcolsep}}{{0.5em}} % for the horizontal padding -{{\renewcommand{{\arraystretch}}{{1.2}} % for the vertical padding -\begin{{table}}[!hbt] - \begin{{center}} - \begin{{adjustbox}}{{{width}}} - \label{{tab:{label}}} - \begin{{tabular}}{{{column_fmt}}} - \hline - {header_str} - \hline - {rows} - \hline - \end{{tabular}} - \end{{adjustbox}} - \caption{{{caption}}} - \end{{center}} -\end{{table}} -}} -%... -""" - # build latex file - path_to_tex.parent.mkdir(exist_ok=True) - - with path_to_tex.open(mode="w") as f: - f.write(table_tex_str) - - -if __name__ == "__main__": - main()