Skip to content

Instantly share code, notes, and snippets.

@jakelevi1996
Last active March 7, 2024 14:53
Show Gist options
  • Save jakelevi1996/2898c58edfe056d442ad5ea6823411e4 to your computer and use it in GitHub Desktop.
Save jakelevi1996/2898c58edfe056d442ad5ea6823411e4 to your computer and use it in GitHub Desktop.
Make Latex table of polynomial coefficients
from jutility import util
def main():
p = util.Printer("poly_table", file_ext="tex", print_to_console=False)
b_deg = 5
a_deg = 7
p("\\documentclass{article}")
p("\\usepackage{subcaption}")
p("\\pagestyle{empty}")
p("\\begin{document}")
p("\\begin{table}")
p("\\centering")
p("\\begin{tabular}{c|%s}" % " ".join(["c"] * (b_deg + 1)))
headers = [""] + ["$b_%i t^%i$" % (i, i) for i in range(b_deg + 1)]
p(format_table_row(headers), "\\\\")
p("\\hline")
for j in range(a_deg + 1):
row_prefix = "$a_%i t^%i$" % (j, j)
row = ["$a_%i b_%i t^{%i}$" % (j, i, i+j) for i in range(b_deg + 1)]
p(format_table_row([row_prefix] + row), "\\\\")
p("\\end{tabular}")
p("\\label{table:animals}")
p("\\end{table}")
p("\\end{document}")
def format_table_row(entries, width=20, sep=" & "):
row_str = sep.join(str(i).ljust(width) for i in entries)
return row_str
if __name__ == "__main__":
main()
\documentclass{article}
\usepackage{subcaption}
\pagestyle{empty}
\begin{document}
\begin{table}
\centering
\begin{tabular}{c|c c c c c c}
& $b_0 t^0$ & $b_1 t^1$ & $b_2 t^2$ & $b_3 t^3$ & $b_4 t^4$ & $b_5 t^5$ \\
\hline
$a_0 t^0$ & $a_0 b_0 t^{0}$ & $a_0 b_1 t^{1}$ & $a_0 b_2 t^{2}$ & $a_0 b_3 t^{3}$ & $a_0 b_4 t^{4}$ & $a_0 b_5 t^{5}$ \\
$a_1 t^1$ & $a_1 b_0 t^{1}$ & $a_1 b_1 t^{2}$ & $a_1 b_2 t^{3}$ & $a_1 b_3 t^{4}$ & $a_1 b_4 t^{5}$ & $a_1 b_5 t^{6}$ \\
$a_2 t^2$ & $a_2 b_0 t^{2}$ & $a_2 b_1 t^{3}$ & $a_2 b_2 t^{4}$ & $a_2 b_3 t^{5}$ & $a_2 b_4 t^{6}$ & $a_2 b_5 t^{7}$ \\
$a_3 t^3$ & $a_3 b_0 t^{3}$ & $a_3 b_1 t^{4}$ & $a_3 b_2 t^{5}$ & $a_3 b_3 t^{6}$ & $a_3 b_4 t^{7}$ & $a_3 b_5 t^{8}$ \\
$a_4 t^4$ & $a_4 b_0 t^{4}$ & $a_4 b_1 t^{5}$ & $a_4 b_2 t^{6}$ & $a_4 b_3 t^{7}$ & $a_4 b_4 t^{8}$ & $a_4 b_5 t^{9}$ \\
$a_5 t^5$ & $a_5 b_0 t^{5}$ & $a_5 b_1 t^{6}$ & $a_5 b_2 t^{7}$ & $a_5 b_3 t^{8}$ & $a_5 b_4 t^{9}$ & $a_5 b_5 t^{10}$ \\
$a_6 t^6$ & $a_6 b_0 t^{6}$ & $a_6 b_1 t^{7}$ & $a_6 b_2 t^{8}$ & $a_6 b_3 t^{9}$ & $a_6 b_4 t^{10}$ & $a_6 b_5 t^{11}$ \\
$a_7 t^7$ & $a_7 b_0 t^{7}$ & $a_7 b_1 t^{8}$ & $a_7 b_2 t^{9}$ & $a_7 b_3 t^{10}$ & $a_7 b_4 t^{11}$ & $a_7 b_5 t^{12}$ \\
\end{tabular}
\label{table:animals}
\end{table}
\end{document}
python make_poly_table.py
cd Results
pdflatex poly_table.tex
pdfcrop --margins 10 poly_table.pdf poly_table_crop.pdf
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r2000 -sOutputFile="../poly_table.png" poly_table_crop.pdf
mv poly_table.tex ../poly_table.tex
cd ..
rm -rf ./Results *.aux *.fdb_latexmk *.fls *.log *.pdf *.synctex.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment