Ask Your Question
0

Is there a function to render latex or html in a notebook from a string ?

asked 2010-11-20 06:08:27 -0500

updated 2015-01-13 15:51:25 -0500

FrédéricC gravatar image

Some user-interface method are:

  1. set the "typeset" flag at the top of the notebook.
  2. Use %html or %latex in the beginning of a cell.
  3. Use "shift+click" to open a new "tinymce" cell (for both: latex and html and more)

or code can be called like:

  1. view(...some sage expression...): to produce rendered latex
  2. print '...some html...no latex....': renders html without latex

I'm looking for a "render" command like

render('< html > < b > Header < /b > < p >Some $ x ^ 2 $ formula< /p >< /html >')

for mix html and latex formulas. Maybe calling tinymce functionality from my code, but how ?

Header

Some $ x ^ 2 $ formula.

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted
8

answered 2010-11-20 07:26:23 -0500

Does this do what you want?

html('<b>Header</b> <p>Some $x^2$ formula</p>')

(Don't include <html> and </html> tags.) You could use <h4>Header</h4>, for example, instead of <b>.... You can even leave out the <p>:

html('<h4>Header</h4>Some $x^2$ formula')
edit flag offensive delete link more

Comments

The html( ..string...) command uses jsmath to produce the output. However, the mark "%latex" on the top of the cell uses the full latex compiler to produce the output. What is the sage command to do the same effect ? For example: latex(..sageobj..) is used for printing a latex representation of a sageobject. I'm looking for a (%latex)(...text...) command that calls the latex compiler and shows an image of the output.

Pedro Cruz gravatar imagePedro Cruz ( 2011-01-10 23:15:09 -0500 )edit

There doesn't seem to be anything built in, but see my other answer.

John Palmieri gravatar imageJohn Palmieri ( 2011-01-31 08:31:17 -0500 )edit
3

answered 2011-01-31 08:31:00 -0500

Try this:

def compile_latex(s):
    """
    Compile string `s` using LaTeX, and display a png picture of the output.
    """
    from sage.misc.misc import tmp_dir, graphics_filename
    from sage.misc.latex import _run_latex_, LATEX_HEADER
    import os, shutil

    base_dir = os.path.abspath("")
    png_file = graphics_filename(ext='png')
    png_link = "cell://" + png_file
    tmp = tmp_dir()
    tmp = tmp_dir('sage_viewer')
    tex_file = os.path.join(tmp, "sage.tex")
    tmp_png_file = os.path.join(tmp, "sage.png")

    latex_code = LATEX_HEADER + '\n\\begin{document}\n' + s + '\\end{document}'          
    open(tex_file, 'w').write(latex_code)
    e = _run_latex_(tex_file, png=True)
    if e.find("Error") == -1:
        shutil.copy(tmp_png_file, os.path.join(base_dir, png_file))
    print '<html><img src="%s"></html>'%png_link
    return

Then run it with compile_string('some math: $x=y$. Some more: $x^2 = y^2$.').

edit flag offensive delete link more
0

answered 2017-02-04 07:03:40 -0500

You may call MathJax directly and then render the result with the html command

x, y = var('x,y')
f(x,y) = x**2 + y**2
from sage.misc.latex import MathJax
mj = MathJax()
html(mj.eval(latex(f)))
edit flag offensive delete link more

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: 2010-11-20 06:08:27 -0500

Seen: 1,624 times

Last updated: Jan 31 '11