Ask Your Question
0

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

asked 2010-11-20 13:08:27 +0200

Pedro gravatar image

updated 2015-01-13 22:51:25 +0200

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 14:26:23 +0200

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 gravatar imagePedro ( 2011-01-11 06:15:09 +0200 )edit

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

John Palmieri gravatar imageJohn Palmieri ( 2011-01-31 15:31:17 +0200 )edit
4

answered 2011-01-31 15:31:00 +0200

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

Comments

this code above does not work for me :W10 Sagemath 8.8 maybe because did change :from sage.misc.temporary_file import tmp_dir,graphics_filename ? (compile_latex replace compile_string) however, the code of Valentin below works and is elegant

ortollj gravatar imageortollj ( 2019-07-28 07:39:14 +0200 )edit

The answer is over 8 years old, so it would be a little surprising if it did still work. Sage has changed, the notebook has changed, etc.

John Palmieri gravatar imageJohn Palmieri ( 2019-07-28 22:09:25 +0200 )edit

Oops , I did not pay attention to the post date !

Sorry

ortollj gravatar imageortollj ( 2019-07-29 08:15:50 +0200 )edit
1

answered 2017-02-04 14:03:40 +0200

valentin gravatar image

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

2 followers

Stats

Asked: 2010-11-20 13:08:27 +0200

Seen: 3,996 times

Last updated: Jan 31 '11