MilesCranmer commited on
Commit
f257e58
1 Parent(s): cf19d41

Create helper file for LaTeX export

Browse files
Files changed (1) hide show
  1. pysr/export_latex.py +34 -0
pysr/export_latex.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Functions to help export PySR equations to LaTeX."""
2
+ import re
3
+
4
+
5
+ def set_precision_of_constants_in_string(s, precision=3):
6
+ """Set precision of constants in string."""
7
+ constants = re.findall(r"\b[-+]?\d*\.\d+|\b[-+]?\d+\.?\d*", s)
8
+ for c in constants:
9
+ reduced_c = "{:.{precision}g}".format(float(c), precision=precision)
10
+ s = s.replace(c, reduced_c)
11
+ return s
12
+
13
+
14
+ def generate_top_of_latex_table():
15
+ latex_table_pieces = [
16
+ r"\begin{table}[h]",
17
+ r"\centering",
18
+ r"\caption{}",
19
+ r"\label{}",
20
+ r"\begin{tabular}{@{}lcc@{}}",
21
+ r"\toprule",
22
+ r"Equation & Complexity & Loss \\",
23
+ r"\midrule",
24
+ ]
25
+ return "\n".join(latex_table_pieces)
26
+
27
+
28
+ def generate_bottom_of_latex_table():
29
+ latex_table_pieces = [
30
+ r"\bottomrule",
31
+ r"\end{tabular}",
32
+ r"\end{table}",
33
+ ]
34
+ return "\n".join(latex_table_pieces)