@@ -408,16 +408,22 @@ def visit_Call(self, node: ast.Call) -> str:
408
408
409
409
if rule .is_unary and len (node .args ) == 1 :
410
410
# Unary function. Applies the same wrapping policy with the unary operators.
411
+ precedence = expression_rules .get_precedence (node )
412
+ arg = node .args [0 ]
411
413
# NOTE(odashi):
412
414
# Factorial "x!" is treated as a special case: it requires both inner/outer
413
415
# parentheses for correct interpretation.
414
- precedence = expression_rules .get_precedence (node )
415
- arg = node .args [0 ]
416
- force_wrap = isinstance (arg , ast .Call ) and (
416
+ force_wrap_factorial = isinstance (arg , ast .Call ) and (
417
417
func_name == "factorial"
418
418
or ast_utils .extract_function_name_or_none (arg ) == "factorial"
419
419
)
420
- arg_latex = self ._wrap_operand (arg , precedence , force_wrap )
420
+ # Note(odashi):
421
+ # Wrapping is also required if the argument is pow.
422
+ # https://github.com/google/latexify_py/issues/189
423
+ force_wrap_pow = isinstance (arg , ast .BinOp ) and isinstance (arg .op , ast .Pow )
424
+ arg_latex = self ._wrap_operand (
425
+ arg , precedence , force_wrap_factorial or force_wrap_pow
426
+ )
421
427
elements = [rule .left , arg_latex , rule .right ]
422
428
else :
423
429
arg_latex = ", " .join (self .visit (arg ) for arg in node .args )
@@ -490,7 +496,7 @@ def _wrap_operand(
490
496
latex = self .visit (child )
491
497
child_prec = expression_rules .get_precedence (child )
492
498
493
- if child_prec < parent_prec or force_wrap and child_prec == parent_prec :
499
+ if force_wrap or child_prec < parent_prec :
494
500
return rf"\mathopen{{}}\left( { latex } \mathclose{{}}\right)"
495
501
496
502
return latex
0 commit comments