Am Freitag, 7. Mai 2010, um 05:54:50 schrieb ablondin: > Ok, I think I understand your problem. I had something similar > before. The problem is that the object returned by the function > sarrus is a 'str', > which sagetex format so that it replaces every underscore, etc. > with \_ to make it work in Latex. If you don't want the output > to be modified, > you have to use LatexExpr object > > def sarrus(A): > bracket_neg = lambda i:str(i) if i >= 0 else '(' + str(i) + > ')' res = '[' > > for i in range(3): > for j in range(3): > a_ij = A[j, (i+j)%3] > if j > 0: res += ' \\cdot ' > res += bracket_neg(a_ij) > if i < 2: res += ' + ' > > res += '] - [' > > for i in range(3): > for j in range(3): > a_ij = A[j, (2-i-j)%3] > if j > 0: res += ' \\cdot ' > res += bracket_neg(a_ij) > if i < 2: res += ' + ' > > res += ']' > > from sage.misc.latex import LatexExpr > return LatexExpr(res) > > and when you call \sage{sarrus(A)}, you must put in between > dollar signs. Feel free to ask further if it doesn't solve your > problem !
Thanks, that is exactly what i was looking for. Georg > On 6 mai, 10:29, "Georg Damm" <georg.d...@web.de> wrote: > > Hi Alex, > > > > > If I understand correctly your question, you would like to > > > convert arithmetical expressions directly into Latex. > > > > I would like to convert a string into a sage-Latex-expression > > (in sagetex). > > > > > I don't know if it is possible to do it in Sage, but you > > > could always write your own function. I've written a small > > > one that should do the trick. > > > > [...] > > > > > sage: sarrus(matrix) > > > '[1 \\cdot 0 \\cdot 1 + 1 \\cdot (-1) \\cdot (-1) + 1 \\cdot > > > 1 \\cdot 0] - [1 \\cdot 0 \\cdot (-1) + 1 \\cdot 1 \\cdot 1 > > > + 1 \\cdot (-1) \ \cdot 0]' > > > sage: print sarrus(matrix) > > > [1 \cdot 0 \cdot 1 + 1 \cdot (-1) \cdot (-1) + 1 \cdot 1 > > > \cdot 0] - [1 \cdot 0 \cdot (-1) + 1 \cdot 1 \cdot 1 + 1 > > > \cdot (-1) \cdot 0] > > > > > > Does that answer your problem ? > > > > Thank you for the code (it's more elegant than my first try :) > > ). It doesn't work too, because the problem is that sagetex is > > putting the string in some kind of (typewriter-)enviroment. > > After some experimentation i found a possible solution is to > > replace "\cdot" with "$\cdot$". It doesn't look nice, but > > better than before. > > > > Georg > > > > > On 5 mai, 11:28, "Dr. Georg Damm" <georg.d...@web.de> wrote: > > > > Hi all, > > > > > > > > i've written a function to explain the rule of sarrus. Is > > > > there a possibility to return latex code insted of a text? > > > > I'd like to use something like $\sage{sarrus(A)}$ in > > > > sagetex. > > > > > > > > Thanks a lot, > > > > > > > > Georg > > > > > > > > P.S.: I expirimented with JSMathExpr from sage.misc.latex, > > > > but i couldn't improve the output. > > > > > > > > def sarrus(A): > > > > res="" > > > > for i in range(3): > > > > > > > > if (A[0,i]>=0): > > > > res+="+"+latex(A[0,i]) > > > > else: > > > > res+="+("+latex(A[0,i])+")" > > > > if (A[1,(i+1)%3]>=0): > > > > res+="*"+latex(A[1,(i+1)%3]) > > > > else: > > > > res+="*("+latex(A[1,(i+1)%3])+")" > > > > if (A[2,(i+2)%3]>=0): > > > > res+="*"+latex(A[2,(i+2)%3]) > > > > else: > > > > res+="*("+latex(A[2,(i+2)%3])+")" > > > > > > > > for i in range(2,5): > > > > if (A[0,i%3]>=0): > > > > res+="-"+latex(A[0,i%3]) > > > > else: > > > > res+="-("+latex(A[0,i%3])+")" > > > > if (A[1,(i-1)%3] >= 0): > > > > res+="*"+latex(A[1,(i-1)%3]) > > > > else: > > > > res+="*("+latex(A[1,(i-1)%3])+")" > > > > if (A[2,(i-2)%3]>=0): > > > > res+="*"+latex(A[2,(i-2)%3]) > > > > else: > > > > res+="*("+latex(A[2,(i-2)%3])+")" > > > > return res > > > > > > > > -- > > > > To post to this group, send email to > > > > sage-support@googlegroups.com To unsubscribe from this > > > > group, send email to > > > > sage-support+unsubscr...@googlegroups.com For more > > > > options, visit this group > > > > athttp://groups.google.com/group/sage-support > > > > URL:http://www.sagemath.org > > > > -- > > To post to this group, send email to > > sage-support@googlegroups.com To unsubscribe from this group, > > send email to sage-support+unsubscr...@googlegroups.com For > > more options, visit this group > > athttp://groups.google.com/group/sage-support > > URL:http://www.sagemath.org -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org