I started learning Sage a couple of weeks ago and I'm working on getting output which I can copy/paste into LaTeX. I've got the general idea but I have to tweek things to get it how I really want it, and it's still very time consuming to typeset anything lengthy.
For a minimal example, consider the function $$f:\mathbb{Z}^2\rightarrow\mathbb{Z}[x], (a,b)\mapsto a + bx$$ and suppose (for whatever reason) I want my output to look just like that. For instance I want $f(1,2)$ to look like $1+2b$. To get $1+2b$ into my LaTeX document by hand, I would simply type
$1+2b$
How would I get Sage to list the function's output in this form? To start off, I might try typing in Sage
x = var('x')
f(a,b) = a + b*x
latex(f(1,2))
upon which Sage would output
2 \, x + 1
Well that does me no good, in fact it would take longer to edit that to what I want than it would to type it from scratch. I want my terms in the opposite order, I need \$ symbols around it, and I want my $2$ and $x$ juxtaposed, not a space between them.
I have been able to get around this to some extent by combining the commands latex and LatexExpr as explained here: http://doc.sagemath.org/html/en/reference/misc/sage/misc/latex.html But it does not solve all the issues, and behaves poorly within functions. The best I've come up with so far is to instead type in Sage
x = var('x')
a = 1
b = 2
LatexExpr(r"$") + latex(a) + LatexExpr(r"+") + latex(b) + latex(x) + LatexExpr(r"$")
which outputs
$ 1 + 2 x $
This has extra spaces in it, but will typeset as desired in LaTex. Then to evaluate "$f$" on other input I just change the 2nd and 3rd lines as needed. The solution is not good in general though, for instance making $a=1$ and $b=-2$ outputs
$ 1 + -2 x $
not to mention how nasty this could get for significantly complicated functions.
This must be done all the time, what do people do?