MilesCranmer commited on
Commit
2a802ab
1 Parent(s): be72c13

Automatically break longer equations in latex table

Browse files
Files changed (2) hide show
  1. pysr/export_latex.py +22 -6
  2. test/test.py +25 -0
pysr/export_latex.py CHANGED
@@ -60,6 +60,7 @@ def generate_single_table(
60
  indices: List[int] = None,
61
  precision: int = 3,
62
  columns=["equation", "complexity", "loss", "score"],
 
63
  ):
64
  """Generate a booktabs-style LaTeX table for a single set of equations."""
65
  assert isinstance(equations, pd.DataFrame)
@@ -88,18 +89,33 @@ def generate_single_table(
88
  row_pieces = []
89
  for col in columns:
90
  if col == "equation":
91
- row_pieces.append(latex_equation)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  elif col == "complexity":
93
- row_pieces.append(complexity)
94
  elif col == "loss":
95
- row_pieces.append(loss)
96
  elif col == "score":
97
- row_pieces.append(score)
98
  else:
99
  raise ValueError(f"Unknown column: {col}")
100
 
101
- row_pieces = ["$" + piece + "$" for piece in row_pieces]
102
-
103
  latex_table_content.append(
104
  " & ".join(row_pieces) + r" \\",
105
  )
 
60
  indices: List[int] = None,
61
  precision: int = 3,
62
  columns=["equation", "complexity", "loss", "score"],
63
+ max_equation_length: int = 50,
64
  ):
65
  """Generate a booktabs-style LaTeX table for a single set of equations."""
66
  assert isinstance(equations, pd.DataFrame)
 
89
  row_pieces = []
90
  for col in columns:
91
  if col == "equation":
92
+ if len(latex_equation) < max_equation_length:
93
+ row_pieces.append("$" + latex_equation + "$")
94
+ else:
95
+ broken_latex_equation = " ".join(
96
+ [
97
+ r"\vbox{",
98
+ r"\vspace{-1em}",
99
+ r"\begin{flushleft}",
100
+ r"$\displaystyle",
101
+ latex_equation,
102
+ "$",
103
+ r"\end{flushleft}",
104
+ r"\vspace{-1em}",
105
+ "}",
106
+ ]
107
+ )
108
+ row_pieces.append(broken_latex_equation)
109
+
110
  elif col == "complexity":
111
+ row_pieces.append("$" + complexity + "$")
112
  elif col == "loss":
113
+ row_pieces.append("$" + loss + "$")
114
  elif col == "score":
115
+ row_pieces.append("$" + score + "$")
116
  else:
117
  raise ValueError(f"Unknown column: {col}")
118
 
 
 
119
  latex_table_content.append(
120
  " & ".join(row_pieces) + r" \\",
121
  )
test/test.py CHANGED
@@ -648,3 +648,28 @@ class TestLaTeXTable(unittest.TestCase):
648
  self.assertEqual(
649
  to_latex(expr, prec=8), "3232.3249 x - 1.4857485 \cdot 10^{-10}"
650
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
648
  self.assertEqual(
649
  to_latex(expr, prec=8), "3232.3249 x - 1.4857485 \cdot 10^{-10}"
650
  )
651
+
652
+ def test_latex_break_long_equation(self):
653
+ """Test that we can break a long equation inside the table"""
654
+ long_equation = """
655
+ - cos(x1 * x0) + 3.2 * x0 - 1.2 * x1 + x1 * x1 * x1 + x0 * x0 * x0
656
+ + 5.2 * sin(0.3256 * sin(x2) - 2.6 * x0) + x0 * x0 * x0 * x0 * x0
657
+ + cos(cos(x1 * x0) + 3.2 * x0 - 1.2 * x1 + x1 * x1 * x1 + x0 * x0 * x0)
658
+ """
659
+ long_equation = "".join(long_equation.split("\n")).strip()
660
+ equations = pd.DataFrame(
661
+ dict(
662
+ equation=["x0", "cos(x0)", long_equation],
663
+ loss=[1.052, 0.02315, 1.12347e-15],
664
+ complexity=[1, 2, 30],
665
+ )
666
+ )
667
+ model = manually_create_model(equations)
668
+ latex_table_str = model.latex_table()
669
+ middle_part = r"""
670
+ $x_{0}$ & $1$ & $1.05$ & $0.0$ \\
671
+ $\cos{\left(x_{0} \right)}$ & $2$ & $0.0232$ & $3.82$ \\
672
+ \vbox{ \vspace{-1em} \begin{flushleft} $\displaystyle 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{flushleft} \vspace{-1em} } & $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)