+ def _repr_latex_(self):
+ string = ''
+ for i, (symbol, coefficient) in enumerate(self.coefficients()):
+ if coefficient == 1:
+ if i != 0:
+ string += ' + '
+ elif coefficient == -1:
+ string += '-' if i == 0 else ' - '
+ elif i == 0:
+ string += '{}'.format(coefficient._repr_latex_().strip('$'))
+ elif coefficient > 0:
+ string += ' + {}'.format(coefficient._repr_latex_().strip('$'))
+ elif coefficient < 0:
+ string += ' - {}'.format((-coefficient)._repr_latex_().strip('$'))
+ string += '{}'.format(symbol._repr_latex_().strip('$'))
+ constant = self.constant
+ if len(string) == 0:
+ string += '{}'.format(constant._repr_latex_().strip('$'))
+ elif constant > 0:
+ string += ' + {}'.format(constant._repr_latex_().strip('$'))
+ elif constant < 0:
+ string += ' - {}'.format((-constant)._repr_latex_().strip('$'))
+ return '${}$'.format(string)
+