1 | initial version |
The object you want to construct is a rational function (quotient of polynomials), and we can define it as such:
R.<x> = QQ[]
s = (x^2 + 2*x + 1) + 1/(x^2 + 2*x + 1)
We can obtain the two parts you want e.g. as follows (or you could have defined them individually):
sage: q, r = s.numerator().quo_rem(s.denominator())
sage: parts = (q, r/s.denominator())
sage: parts
(x^2 + 2*x + 1, 1/(x^2 + 2*x + 1))
Now we can generate the LaTeX for each part individually and join them by a plus symbol:
sage: latex_code = latex(parts[0]) + '+' + latex(parts[1])
sage: latex_code
x^{2} + 2 x + 1 + \frac{1}{x^{2} + 2 x + 1}
sage: show(latex_code)
Showing the LaTeX gives the desired output: $x^{2} + 2 x + 1 + \frac{1}{x^{2} + 2 x + 1}$.