diff --git a/app/preview_implementations/physical_quantity_preview.py b/app/preview_implementations/physical_quantity_preview.py index 9a0f46c..7653f1a 100644 --- a/app/preview_implementations/physical_quantity_preview.py +++ b/app/preview_implementations/physical_quantity_preview.py @@ -1,3 +1,4 @@ +import tokenize from copy import deepcopy from ..utility.expression_utilities import ( @@ -119,7 +120,7 @@ def preview_function(response: str, params: Params) -> Result: latex_out = res_parsed.latex_string sympy_out = response - except SyntaxError as e: + except (SyntaxError, tokenize.TokenError) as e: raise Exception("Failed to parse Sympy expression") from e except ValueError as e: raise Exception("Failed to parse LaTeX expression") from e diff --git a/app/preview_test.py b/app/preview_test.py index f67aeb4..294ba8b 100644 --- a/app/preview_test.py +++ b/app/preview_test.py @@ -274,6 +274,5 @@ def test_laplace_transforms(self, response, is_latex, latex, sympy): assert result["preview"]["sympy"] == sympy - if __name__ == "__main__": pytest.main(['-xk not slow', "--tb=line", os.path.abspath(__file__)]) diff --git a/app/tests/physical_quantity_preview_test.py b/app/tests/physical_quantity_preview_test.py index f44f7f4..147cf69 100644 --- a/app/tests/physical_quantity_preview_test.py +++ b/app/tests/physical_quantity_preview_test.py @@ -89,6 +89,23 @@ def test_handwritten_input(self): assert result["latex"] == r'162~\frac{\mathrm{newton}}{\mathrm{metre}^{(2)}}' # TODO: Fix so that unnecessary parenthesis are simplified away assert result["sympy"] == "162 newton/metre**(2)" + @pytest.mark.parametrize( + "response, latex, sympy", [ + (r"\frac{F}{p \cdot \mu}", r"\frac{F}{\mu p}", "F/((mu*p))"), + (r"F \cdot p", "F p", "F*p"), + (r"\frac{10}{2} \mathrm{~kg}", r"\frac{10}{2}~\mathrm{kilogram}", "10/2 kilogram"), + ] + ) + def test_latex_physical_quantity_preview(self, response, latex, sympy): + params = { + "is_latex": True, + "physical_quantity": True, + "elementary_functions": True, + } + result = preview_function(response, params)["preview"] + assert result["latex"] == latex + assert result["sympy"] == sympy + if __name__ == "__main__": pytest.main(['-xk not slow', "--tb=line", os.path.abspath(__file__)]) diff --git a/app/utility/physical_quantity_utilities.py b/app/utility/physical_quantity_utilities.py index 33a074d..4b89f4a 100644 --- a/app/utility/physical_quantity_utilities.py +++ b/app/utility/physical_quantity_utilities.py @@ -22,6 +22,7 @@ from ..feedback.physical_quantity import feedback_string_generators as physical_quantity_feedback_string_generators from ..preview_implementations.symbolic_preview import preview_function as symbolic_preview +from .preview_utilities import parse_latex QuantityTags = Enum("QuantityTags", {v: i for i, v in enumerate("UVNR", 1)}) @@ -209,6 +210,10 @@ def _expand_units(self, node): def _all_forms(self): parsing_params = self.parsing_params converted_value = self.value.content_string() if self.value is not None else None + + if converted_value is not None and self.parameters.get("is_latex", False): + symbols = self.parameters.get("symbols", {}) + converted_value = parse_latex(converted_value, symbols, self.parameters.get("simplify", False)) converted_unit = None expanded_unit = None converted_dimension = parse_expression("1", parsing_params)