Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix brackets display #112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/integration_tests/regression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def sum_with_limit(n):
return sum(i**2 for i in range(n))

latex = (
r"\mathrm{sum_with_limit}(n) = \sum_{i = {0}}^{{n - 1}} \left({i^{{2}}}\right)"
r"\mathrm{sum_with_limit}(n) = \sum_{i = {0}}^{{n - 1}}"
r" \mathopen{}\left({i^{{2}}}\mathclose{}\right)"
)
_check_function(sum_with_limit, latex)

Expand All @@ -99,7 +100,8 @@ def sum_with_limit(a, n):
return sum(i**2 for i in range(a, n))

latex = (
r"\mathrm{sum_with_limit}(a, n) = \sum_{i = a}^{{n - 1}} \left({i^{{2}}}\right)"
r"\mathrm{sum_with_limit}(a, n) = \sum_{i = a}^{{n - 1}} "
r"\mathopen{}\left({i^{{2}}}\mathclose{}\right)"
)
_check_function(sum_with_limit, latex)

Expand All @@ -110,7 +112,7 @@ def prod_with_limit(n):

latex = (
r"\mathrm{prod_with_limit}(n) = "
r"\prod_{i = {0}}^{{n - 1}} \left({i^{{2}}}\right)"
r"\prod_{i = {0}}^{{n - 1}} \mathopen{}\left({i^{{2}}}\mathclose{}\right)"
)
_check_function(prod_with_limit, latex)

Expand All @@ -121,7 +123,7 @@ def prod_with_limit(a, n):

latex = (
r"\mathrm{prod_with_limit}(a, n) = "
r"\prod_{i = a}^{{n - 1}} \left({i^{{2}}}\right)"
r"\prod_{i = a}^{{n - 1}} \mathopen{}\left({i^{{2}}}\mathclose{}\right)"
)
_check_function(prod_with_limit, latex)

Expand Down Expand Up @@ -171,7 +173,7 @@ def f(x):
)
_check_function(
f,
r"\mathrm{f}(x) = {3} \left( x + x \right)",
r"\mathrm{f}(x) = {3} \mathopen{}\left( x + x \mathclose{}\right)",
reduce_assignments=True,
)

Expand All @@ -194,7 +196,7 @@ def f(x):
_check_function(f, latex_without_option, reduce_assignments=False)
_check_function(
f,
r"\mathrm{f}(x) = {3} \left( x^{{2}} + x^{{2}} \right)",
r"\mathrm{f}(x) = {3} \mathopen{}\left( x^{{2}} + x^{{2}} \mathclose{}\right)",
reduce_assignments=True,
)

Expand Down Expand Up @@ -228,6 +230,8 @@ def solve(a, b):

latex = (
r"\mathrm{solve}(a, b) = "
r"\frac{a + b - b}{a - b} - \left( a + b \right) - \left( a - b \right) - a b"
r"\frac{a + b - b}{a - b} - \mathopen{}\left( "
r"a + b \mathclose{}\right) - \mathopen{}\left( "
r"a - b \mathclose{}\right) - a b"
)
_check_function(solve, latex)
19 changes: 13 additions & 6 deletions src/latexify/codegen/function_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ def visit_Return(self, node: ast.Return) -> str:

def visit_Tuple(self, node: ast.Tuple) -> str:
elts = [self.visit(i) for i in node.elts]
return r"\left( " + r"\space,\space ".join(elts) + r"\right) "
return (
r"\mathopen{}\left( "
+ r"\space,\space ".join(elts)
+ r"\mathclose{}\right) "
)

def visit_List(self, node: ast.List) -> str:
elts = [self.visit(i) for i in node.elts]
Expand Down Expand Up @@ -334,7 +338,7 @@ def visit_comprehension(self, node: ast.comprehension) -> str:
return target

conds = [target] + [self.visit(cond) for cond in node.ifs]
wrapped = [r"\left( " + s + r" \right)" for s in conds]
wrapped = [r"\mathopen{}\left( " + s + r" \mathclose{}\right)" for s in conds]
return r" \land ".join(wrapped)

def visit_Call(self, node: ast.Call) -> str:
Expand All @@ -352,13 +356,16 @@ def visit_Call(self, node: ast.Call) -> str:
# Obtains wrapper syntax: sqrt -> "\sqrt{" and "}"
lstr, rstr = constants.BUILTIN_FUNCS.get(
func_str,
(r"\mathrm{" + func_str + r"}\left(", r"\right)"),
(r"\mathrm{" + func_str + r"}\mathopen{}\left(", r"\mathclose{}\right)"),
)

if func_str in ("sum", "prod") and isinstance(node.args[0], ast.GeneratorExp):
elt, scripts = self._get_sum_prod_info(node.args[0])
scripts_str = [rf"\{func_str}_{{{lo}}}^{{{up}}}" for lo, up in scripts]
return " ".join(scripts_str) + rf" \left({{{elt}}}\right)"
return (
" ".join(scripts_str)
+ rf" \mathopen{{}}\left({{{elt}}}\mathclose{{}}\right)"
)

arg_strs = [self.visit(arg) for arg in node.args]
return lstr + ", ".join(arg_strs) + rstr
Expand Down Expand Up @@ -432,7 +439,7 @@ def _wrap_operand(self, child: ast.expr, parent_prec: int) -> str:
latex = self.visit(child)
if _get_precedence(child) >= parent_prec:
return latex
return rf"\left( {latex} \right)"
return rf"\mathopen{{}}\left( {latex} \mathclose{{}}\right)"

def _wrap_binop_operand(
self,
Expand Down Expand Up @@ -468,7 +475,7 @@ def _wrap_binop_operand(
):
return latex

return rf"\left( {latex} \right)"
return rf"\mathopen{{}}\left( {latex} \mathclose{{}}\right)"

def visit_BinOp(self, node: ast.BinOp) -> str:
"""Visit a BinOp node."""
Expand Down
Loading