# How do I force Sage to order my expressions a particular way, in LaTeX code? [closed]

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?

## closed as off-topic by mrf, mickep, Harish Chandra Rajpoot, Jean-Claude Arbaut, Najib IdrissiOct 7 '15 at 7:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question is not about mathematics, within the scope defined in the help center." – mrf, mickep, Harish Chandra Rajpoot, Jean-Claude Arbaut, Najib Idrissi
If this question can be reworded to fit the rules in the help center, please edit the question.

• I don't know how to get rid of the white space (\,), but concerning the order, just change the term order to neglex. – Ben Oct 7 '15 at 7:36
• That's nice to know but in general doesn't solve my issue. Sometimes I'm working with many variables and want them in a particular order, to match the notation in my writing. – j0equ1nn Oct 7 '15 at 7:40
• @mrf (and other voters): Is there a place on Stack exchange where questions about Sage code are appropriate? I have more but don't want my posts closed. – j0equ1nn Oct 15 '15 at 2:27

To be specific, you could do various computations in a sagesilent block, and then insert them in the 'right' places using LaTeX's normal facilities for this (along with the \sage stuff). Think of it as string formatting; you really need to do this by hand, but once you know what you want it to look like, then you can do a million of them at once by just replacing the variables with your list of inputs.