Prevent table headline to be at end of page for latex file generated from python source

Viewed 25

I have some sourcecode to dynamically generate tables from python with some headline and write it into some latex file. It looks like this:

import pandas as pd
import numpy as np

def generate_tables():
    with open('tables.tex', 'w') as f:
        # header

        f.write("% !TeX TS-program = lualatex\n") 
        f.write("\\documentclass{article}\n")
        f.write("\\usepackage{booktabs}\n")
        f.write("\\usepackage{unicode-math}\n")
        f.write("\\begin{document}\n")

        fooder = "\\end{document}\n"

        for i in range(10):
            df = pd.DataFrame(np.random.random((5, 5)))
            latex_table = df.to_latex(index=False,header=False,escape=False)
            f.write(f"Table {i}:")
            f.write("\n\\\\\n")
            f.write(latex_table)
            f.write("\\\\[2\\baselineskip]\n")

        f.write(fooder)

generate_tables()

It generates the following latex code:

% !TeX TS-program = lualatex
\documentclass{article}
\usepackage{booktabs}
\usepackage{unicode-math}
\begin{document}
Table 0:
\\
\begin{tabular}{rrrrr}
\toprule
0.371952 & 0.669503 & 0.049893 & 0.468560 & 0.362626 \\
0.528347 & 0.483403 & 0.087952 & 0.476994 & 0.421168 \\
0.507104 & 0.985636 & 0.299446 & 0.055767 & 0.146663 \\
0.299839 & 0.850865 & 0.292166 & 0.455254 & 0.390094 \\
0.716182 & 0.497894 & 0.032962 & 0.206535 & 0.701566 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 1:
\\
\begin{tabular}{rrrrr}
\toprule
0.944850 & 0.210858 & 0.093959 & 0.197392 & 0.839515 \\
0.232418 & 0.115631 & 0.058326 & 0.019664 & 0.280028 \\
0.326205 & 0.013849 & 0.706969 & 0.402050 & 0.951654 \\
0.179592 & 0.025729 & 0.986432 & 0.768442 & 0.700137 \\
0.032620 & 0.030183 & 0.097770 & 0.213503 & 0.737244 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 2:
\\
\begin{tabular}{rrrrr}
\toprule
0.171137 & 0.202541 & 0.717975 & 0.853340 & 0.567567 \\
0.866236 & 0.287948 & 0.369145 & 0.321263 & 0.713473 \\
0.559648 & 0.232801 & 0.530387 & 0.629864 & 0.236152 \\
0.422992 & 0.170105 & 0.461402 & 0.677928 & 0.994581 \\
0.991853 & 0.170034 & 0.434196 & 0.775089 & 0.237765 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 3:
\\
\begin{tabular}{rrrrr}
\toprule
0.972044 & 0.765008 & 0.026849 & 0.063924 & 0.312687 \\
0.770727 & 0.332079 & 0.076711 & 0.111243 & 0.051879 \\
0.888069 & 0.460427 & 0.482428 & 0.662136 & 0.108501 \\
0.942437 & 0.614880 & 0.891087 & 0.237526 & 0.649645 \\
0.899199 & 0.397127 & 0.263776 & 0.213914 & 0.670456 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 4:
\\
\begin{tabular}{rrrrr}
\toprule
0.653364 & 0.614661 & 0.274680 & 0.684983 & 0.312441 \\
0.762303 & 0.639873 & 0.638030 & 0.563154 & 0.210836 \\
0.737251 & 0.289572 & 0.184137 & 0.288722 & 0.164200 \\
0.201049 & 0.709251 & 0.023084 & 0.726323 & 0.757200 \\
0.925808 & 0.947689 & 0.702203 & 0.310276 & 0.341111 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 5:
\\
\begin{tabular}{rrrrr}
\toprule
0.884983 & 0.183106 & 0.669247 & 0.924775 & 0.266794 \\
0.611894 & 0.858615 & 0.908220 & 0.805593 & 0.469397 \\
0.492322 & 0.890270 & 0.376719 & 0.033564 & 0.389576 \\
0.143099 & 0.108604 & 0.808267 & 0.105064 & 0.989036 \\
0.536014 & 0.033140 & 0.001459 & 0.197306 & 0.614286 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 6:
\\
\begin{tabular}{rrrrr}
\toprule
0.795137 & 0.863915 & 0.056398 & 0.395086 & 0.322729 \\
0.337455 & 0.913096 & 0.745055 & 0.683221 & 0.936046 \\
0.581282 & 0.061453 & 0.173496 & 0.276883 & 0.011773 \\
0.338902 & 0.027020 & 0.590554 & 0.473165 & 0.681370 \\
0.053432 & 0.567317 & 0.599100 & 0.443148 & 0.986242 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 7:
\\
\begin{tabular}{rrrrr}
\toprule
0.035678 & 0.726866 & 0.096819 & 0.720812 & 0.170551 \\
0.282408 & 0.679047 & 0.432019 & 0.943898 & 0.622419 \\
0.287570 & 0.459775 & 0.257608 & 0.912952 & 0.347501 \\
0.940003 & 0.881516 & 0.629745 & 0.946774 & 0.077157 \\
0.832748 & 0.357248 & 0.777124 & 0.547402 & 0.627144 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 8:
\\
\begin{tabular}{rrrrr}
\toprule
0.791259 & 0.459723 & 0.960981 & 0.637817 & 0.528181 \\
0.817673 & 0.342002 & 0.073439 & 0.931211 & 0.702702 \\
0.894257 & 0.801851 & 0.286740 & 0.741268 & 0.641442 \\
0.926924 & 0.350539 & 0.017811 & 0.084452 & 0.841406 \\
0.262145 & 0.720061 & 0.860032 & 0.829259 & 0.835973 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
Table 9:
\\
\begin{tabular}{rrrrr}
\toprule
0.810070 & 0.381169 & 0.068985 & 0.397946 & 0.262417 \\
0.673725 & 0.036426 & 0.453721 & 0.967120 & 0.076771 \\
0.539643 & 0.978185 & 0.233677 & 0.970223 & 0.340148 \\
0.180692 & 0.733842 & 0.725471 & 0.276546 & 0.053212 \\
0.763539 & 0.017316 & 0.030469 & 0.871404 & 0.724823 \\
\bottomrule
\end{tabular}
\\[2\baselineskip]
\end{document}

Now I have a question:

If I now generate the .pdf from the .tex file it happens, that the header f.write(f"Table {i}:") is at end of the .pdf page and the table f.write(latex_table) beginns at the new page (see screenshot below). Can this be prevented?

enter image description here

0 Answers
Related