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

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 close merge delete

Sort by » oldest newest most voted

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_()
'\\left(1,\\,2,\\,3\\right)'


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)
\left(1,\,2,\,3\right)
sage: my_vector_latex(v)
\left(1\mid\,2\mid\,3\right)

more

1

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.

( 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.

( 2011-05-09 07:21:05 -0500 )edit