Ask Your Question

How do I change the default latex output of a vector?

asked 2011-05-08 02:21:01 -0500

Georg Damm gravatar image

I want a vector v=vector([1,2,3]) to be diplayed in sagetex as (1|2|3) an not as (1,2,3). v=var('v',latex_name=latex(v).replace(',', '|')) does not work.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2011-05-08 11:07:59 -0500

benjaminfjones gravatar image

updated 2011-05-09 05:36:26 -0500

Using the latex_name parameter of var just changes the way the variable v is printed. You want to change the way specific vector objects are printed, if I understand correctly. Calling latex() on v calls the _latex_() method of v, followed by converting to a LatexExpr object.

sage: v = vector([1,2,3])
sage: v._latex_()

So you could define your own latex method for vectors that does the same thing, but puts \mid between entries instead of commas. Below, I copied the code from v._latex_() that I looked at using v._latex_?? in the Sage console and then modified the last line (adding LatexExpr) and the second to last line (replacing the comma with \mid).

def my_vector_latex(v):
    latex = sage.misc.latex.latex
    LatexExpr = sage.misc.latex.LatexExpr
    vector_delimiters = latex.vector_delimiters()
    s = '\\left' + vector_delimiters[0]
    s += '\\mid\\,'.join([latex(a) for a in v.list()]) # different than usual _latex_()
    return LatexExpr(s + '\\right' + vector_delimiters[1])

Then, you can do:

sage: latex(v)
sage: my_vector_latex(v)
edit flag offensive delete link more



I'd prefer to overwrite the global function latex for vectors in a document. To replace \sage{v} with \sage{my_vector_latex(v)} for every vector is possible but a lot of work.

Georg Damm gravatar imageGeorg Damm ( 2011-05-09 02:47:24 -0500 )edit

Yes, thanks for the reminder about escaped backslashes. @Georg, there should be a class _latex_() method to override, I just can't find it at the moment. Maybe someone with more familiarity can chime in.

benjaminfjones gravatar imagebenjaminfjones ( 2011-05-09 07:21:05 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-05-08 02:21:01 -0500

Seen: 424 times

Last updated: May 09 '11