Ask Your Question
1

How to get latex expression in exponential notation?

asked 8 years ago

stan gravatar image

updated 8 years ago

I need a latex representation of equations in exponential notation, e.g. units of J/K should be represented as:

J K^{-1}

Is there a way to force sage to return such a result? Here is an example:

var('J K ')
unit1 = J/K
latex(unit1)

returns:

 \frac{J}{K}

I tried expand(), but it does not seem to expand fractions to their exponential representation.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 8 years ago

castor gravatar image

The following may help:

P.<J,K>=LaurentPolynomialRing(QQ,2)
u1=J/K
latex(u1)

it gives what you need J K^{-1}.

Preview: (hide)
link

Comments

Wow, thanks. Not sure what side effects converting my symbolic expressions to LaurentPolynomial would have, so I added below another solution.

stan gravatar imagestan ( 8 years ago )
2

answered 8 years ago

stan gravatar image

I found an ugly way to get the latex representation step by step:

var('J K ')
units1 = J/K
facs = units1.factor_list()
str1 = ''
for term1 in facs:
    str1 = str1 +' ' + str(term1[0])
    op1 = term1[1]
    if op1!=1:
        str1 = str1 + '^{' + str(op1) + '} '
latex_expr = '$'+str1+'$'
Preview: (hide)
link

Comments

Notice that in the last line, you can do instead latex_expr = LatexExpr(str1). If you have %display typeset allowed in you notebook, it will display in latex; alternatively, you can use show(LatexExpr(str1)).

mforets gravatar imagemforets ( 8 years ago )

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 8 years ago

Seen: 1,303 times

Last updated: Nov 28 '16