Ask Your Question
0

print LaTeX in notebook from script

asked 2012-09-27 19:35:22 +0100

burningbright gravatar image

I've read through

www.sagemath.org/doc/tutorial/latex.html

but still can't seem to find the equivalent to using "view" or "pretty-print" to display equations at LaTeX in a notebook cell when running a Python script from the notebook. (I do have "Typeset" checked at the start of the notebook.) For example, suppose my file foo.py has a function:

def show_all(self):
  y = var('y')
  x = 2.0*y**2
  view(x)
  pretty_print(x)
  show(x)
  JSMath()(x)
  JSMath().eval(x)
  print JSMath()(x)
  print JSMath().eval(x)
  html(JSMath()(x))
  html(JSMath().eval(x))
  print html(JSMath()(x))
  print html(JSMath().eval(x))

Running this function from a notebook cell returns lines of the form either

  \newcommand{\Bold}[1]{\mathbf{#1}}2.0 \, y^{2}

or

  \newcommand{\Bold}[1]{\mathbf{#1}}2.0 \, y^{2}
  </font></html>

But I just want a nice LaTeX display of 2y**2 equivalent to typing (from a notebook cell)

view(2.0*y**2)

which returns

2.00000000000000y2 (prettier than this of course)

So I'm still missing the right combination of LaTeX library functions. As a follow-up question, is dealing with LaTeX macros the only way to get the "2.00000000000" in the above to display as 2.0, 2.00, etc.?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-09-28 15:33:54 +0100

I think you need to upgrade your version of Sage. Many of the commands in your script work for me, either with Sage on my own computer or with sagenb on line. For example,

def show_all():
  y = var('y')
  x = 2.0*y**2
  view(x)
  pretty_print(x)
  show(x)
  print MathJax()(x)

prints x nicely four times.

For decimal expansions, you can force the coefficient to have lower precision:

def show_all():
  y = var('y')
  x = 2.0*y**2
  view(x)
  a = RDF(2.0)
  a = a.n(digits=3)
  x = a*y**2
  view(x)

For me, this prints "2.00000000000000y^2" and "2.00y^2" (formatted nicely).

edit flag offensive delete link more

Comments

I'm using Sage version 5.2. Will post an update when I get 5.3 installed. Thanks.

burningbright gravatar imageburningbright ( 2012-09-28 17:39:19 +0100 )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

Stats

Asked: 2012-09-27 19:35:22 +0100

Seen: 2,051 times

Last updated: Sep 28 '12