Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
fab6f87
1
Parent(s):
2a802ab
Center all equations and use `breqn` for breaks
Browse files- pysr/export_latex.py +17 -8
- test/test.py +3 -3
pysr/export_latex.py
CHANGED
@@ -3,6 +3,10 @@ import sympy
|
|
3 |
from sympy.printing.latex import LatexPrinter
|
4 |
import pandas as pd
|
5 |
from typing import List
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
class PreciseLatexPrinter(LatexPrinter):
|
@@ -26,7 +30,7 @@ def to_latex(expr, prec=3, full_prec=True, **settings):
|
|
26 |
|
27 |
|
28 |
def generate_table_environment(columns=["equation", "complexity", "loss"]):
|
29 |
-
margins = "
|
30 |
column_map = {
|
31 |
"complexity": "Complexity",
|
32 |
"loss": "Loss",
|
@@ -65,6 +69,8 @@ def generate_single_table(
|
|
65 |
"""Generate a booktabs-style LaTeX table for a single set of equations."""
|
66 |
assert isinstance(equations, pd.DataFrame)
|
67 |
|
|
|
|
|
68 |
latex_top, latex_bottom = generate_table_environment(columns)
|
69 |
latex_table_content = []
|
70 |
|
@@ -92,17 +98,20 @@ def generate_single_table(
|
|
92 |
if len(latex_equation) < max_equation_length:
|
93 |
row_pieces.append("$" + latex_equation + "$")
|
94 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
broken_latex_equation = " ".join(
|
96 |
[
|
97 |
-
r"\
|
98 |
r"\vspace{-1em}",
|
99 |
-
r"\begin{
|
100 |
-
r"$\displaystyle",
|
101 |
latex_equation,
|
102 |
-
"
|
103 |
-
r"\end{
|
104 |
-
r"\vspace{-1em}",
|
105 |
-
"}",
|
106 |
]
|
107 |
)
|
108 |
row_pieces.append(broken_latex_equation)
|
|
|
3 |
from sympy.printing.latex import LatexPrinter
|
4 |
import pandas as pd
|
5 |
from typing import List
|
6 |
+
import warnings
|
7 |
+
|
8 |
+
|
9 |
+
raised_long_equation_warning = False
|
10 |
|
11 |
|
12 |
class PreciseLatexPrinter(LatexPrinter):
|
|
|
30 |
|
31 |
|
32 |
def generate_table_environment(columns=["equation", "complexity", "loss"]):
|
33 |
+
margins = "c" * len(columns)
|
34 |
column_map = {
|
35 |
"complexity": "Complexity",
|
36 |
"loss": "Loss",
|
|
|
69 |
"""Generate a booktabs-style LaTeX table for a single set of equations."""
|
70 |
assert isinstance(equations, pd.DataFrame)
|
71 |
|
72 |
+
global raised_long_equation_warning
|
73 |
+
|
74 |
latex_top, latex_bottom = generate_table_environment(columns)
|
75 |
latex_table_content = []
|
76 |
|
|
|
98 |
if len(latex_equation) < max_equation_length:
|
99 |
row_pieces.append("$" + latex_equation + "$")
|
100 |
else:
|
101 |
+
if not raised_long_equation_warning:
|
102 |
+
warnings.warn(
|
103 |
+
"Please add \\usepackage{breqn} to your LaTeX preamble."
|
104 |
+
)
|
105 |
+
raised_long_equation_warning = True
|
106 |
+
|
107 |
broken_latex_equation = " ".join(
|
108 |
[
|
109 |
+
r"\begin{minipage}{0.8\linewidth}",
|
110 |
r"\vspace{-1em}",
|
111 |
+
r"\begin{dmath*}",
|
|
|
112 |
latex_equation,
|
113 |
+
r"\end{dmath*}",
|
114 |
+
r"\end{minipage}",
|
|
|
|
|
115 |
]
|
116 |
)
|
117 |
row_pieces.append(broken_latex_equation)
|
test/test.py
CHANGED
@@ -524,7 +524,7 @@ class TestLaTeXTable(unittest.TestCase):
|
|
524 |
true_latex_table_str = r"""
|
525 |
\begin{table}[h]
|
526 |
\begin{center}
|
527 |
-
\begin{tabular}{@{}
|
528 |
\toprule
|
529 |
Equation & Complexity & Loss & Score \\
|
530 |
\midrule"""
|
@@ -532,7 +532,7 @@ class TestLaTeXTable(unittest.TestCase):
|
|
532 |
true_latex_table_str = r"""
|
533 |
\begin{table}[h]
|
534 |
\begin{center}
|
535 |
-
\begin{tabular}{@{}
|
536 |
\toprule
|
537 |
Equation & Complexity & Loss \\
|
538 |
\midrule"""
|
@@ -669,7 +669,7 @@ class TestLaTeXTable(unittest.TestCase):
|
|
669 |
middle_part = r"""
|
670 |
$x_{0}$ & $1$ & $1.05$ & $0.0$ \\
|
671 |
$\cos{\left(x_{0} \right)}$ & $2$ & $0.0232$ & $3.82$ \\
|
672 |
-
\
|
673 |
"""
|
674 |
true_latex_table_str = self.create_true_latex(middle_part, include_score=True)
|
675 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
|
|
524 |
true_latex_table_str = r"""
|
525 |
\begin{table}[h]
|
526 |
\begin{center}
|
527 |
+
\begin{tabular}{@{}cccc@{}}
|
528 |
\toprule
|
529 |
Equation & Complexity & Loss & Score \\
|
530 |
\midrule"""
|
|
|
532 |
true_latex_table_str = r"""
|
533 |
\begin{table}[h]
|
534 |
\begin{center}
|
535 |
+
\begin{tabular}{@{}ccc@{}}
|
536 |
\toprule
|
537 |
Equation & Complexity & Loss \\
|
538 |
\midrule"""
|
|
|
669 |
middle_part = r"""
|
670 |
$x_{0}$ & $1$ & $1.05$ & $0.0$ \\
|
671 |
$\cos{\left(x_{0} \right)}$ & $2$ & $0.0232$ & $3.82$ \\
|
672 |
+
\begin{minipage}{0.8\linewidth} \vspace{-1em} \begin{dmath*} x_{0}^{5} + x_{0}^{3} + 3.20 x_{0} + x_{1}^{3} - 1.20 x_{1} - 5.20 \sin{\left(2.60 x_{0} - 0.326 \sin{\left(x_{2} \right)} \right)} - \cos{\left(x_{0} x_{1} \right)} + \cos{\left(x_{0}^{3} + 3.20 x_{0} + x_{1}^{3} - 1.20 x_{1} + \cos{\left(x_{0} x_{1} \right)} \right)} \end{dmath*} \end{minipage} & $30$ & $1.12 \cdot 10^{-15}$ & $1.09$ \\
|
673 |
"""
|
674 |
true_latex_table_str = self.create_true_latex(middle_part, include_score=True)
|
675 |
self.assertEqual(latex_table_str, true_latex_table_str)
|