# How to get latex expression in exponential notation?

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.

edit retag close merge delete

Sort by » oldest newest most voted

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+'$'

more

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

( 2016-11-29 02:07:45 -0600 )edit

The following may help:

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


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

more

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

( 2016-11-27 17:07:04 -0600 )edit