Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
Created January 31, 2020 03:05
Show Gist options
  • Save cosinekitty/a936b1a7f19506a1c5871c4d318ed61d to your computer and use it in GitHub Desktop.
Save cosinekitty/a936b1a7f19506a1c5871c4d318ed61d to your computer and use it in GitHub Desktop.
Shows optimizing parentheses in TeX output for a left-associative operator.
PrettyMath_Binary_LeftAssoc(opsymbol) {
let left = this.arglist[0].PrettyMath();
let right = this.arglist[1].PrettyMath();
// Use parentheses around the left child expression
// if its operator precedence is less than this node's precedence.
// If it is equal, assume left-associativity means parentheses are not needed.
if (this.arglist[0].precedence < this.precedence) {
left = '\\left(' + left + '\\right)';
}
// Use parentheses around the right child expression
// if operator precedence is lower or equal.
// Even if equal, parentheses are needed because we are
// overriding left-associativity.
if (this.arglist[1].precedence <= this.precedence) {
right = '\\left(' + right + '\\right)';
}
return left + (opsymbol || this.optoken.text) + right;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment