Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

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

asked 14 years ago

Pedro gravatar image

updated 10 years ago

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 x2 formula< /p >< /html >')

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

Header

Some x2 formula.

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
8

answered 14 years ago

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')
Preview: (hide)
link

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 ( 14 years ago )

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

John Palmieri gravatar imageJohn Palmieri ( 14 years ago )
4

answered 14 years ago

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

Preview: (hide)
link

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 ( 5 years ago )

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 ( 5 years ago )

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

Sorry

ortollj gravatar imageortollj ( 5 years ago )
1

answered 8 years ago

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)))
Preview: (hide)
link

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: 14 years ago

Seen: 4,214 times

Last updated: Jan 31 '11