Skip to content

Commit

Permalink
feat: Add new flag ? to EVAL tag, used to hide implicit 1 when mu…
Browse files Browse the repository at this point in the history
…ltiplying.
  • Loading branch information
wxgeo committed Nov 22, 2023
1 parent d43e807 commit f1025c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 13 additions & 3 deletions ptyx/latex_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,10 @@ def _parse_EVAL_tag(self, node: Node):
self.flags["round"] = int(arg)
elif arg == ".":
self.flags["."] = True
elif arg == "?":
self.flags["?"] = True
elif arg in ("floats", "float"):
self.flags["floats"] = True

elif arg == "str":
self.flags["str"] = True
else:
Expand Down Expand Up @@ -735,6 +736,7 @@ def _eval_and_format_python_expr(self, code: str) -> str:
code = code.rstrip(";")
display_result = False

result = ""
for subcode in advanced_split(code, ";", brackets=()):
result = self._eval_python_expr(subcode)
# Note that only last result will be displayed.
Expand All @@ -745,15 +747,23 @@ def _eval_and_format_python_expr(self, code: str) -> str:
if not display_result:
return ""

if flags.get("?"):
if result == 1:
if flags.get("+"):
return "+"
return ""
elif result == -1:
result = "-"

if sympy_code and not flags.get("str"):
from ptyx.printers import sympy2latex

latex = sympy2latex(result, **flags)
else:
latex = str(result)

def neg(latex):
return latex.lstrip().startswith("-")
def neg(latex_):
return latex_.lstrip().startswith("-")

if flags.get("+"):
if result == 0:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ def test_ADD():
assert latex == result


def test_EVAL_1():
test = r"""
#PYTHON
a = 1
b = 1
c = -1
#END
$#[?]a x #+ #[?]b y #+ #[?]c z = 0$"""
target = "$x+y-z=0$" ""
c = Compiler()
latex = c.parse(code=test)
assert latex.replace(" ", "").strip() == target


def test_INCLUDE():
os.chdir(TEST_DIR)
test = "#SEED{99}First, $a=#{a=7}$#INCLUDE{include_example.txt}Last, $a=#a$ still."
Expand Down

0 comments on commit f1025c9

Please sign in to comment.