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

222 Views Asked by At

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?

1

There are 1 best solutions below

1
On

Hmm, Sage isn't necessarily designed to be a typesetting tool. Sage (via Pynac) sorts these symbolic expressions in a certain canonical way for output. It's not so easy to change this, nor desirable from Sage's point of view.

If you are looking for something a little more programmatic and your only use case is LaTeX, then I highly suggest using SageTeX and putting the pieces you need Sage to do in there. Then you have complete control over order (because it's just LaTeX and you can decide exactly what Sage should compute) but you also can use Sage to compute some of the more tedious expressions.

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.

Hope this helps; perhaps your use case is more complex and this might not work, but in your example it definitely would.