inducer / relate

RELATE is an Environment for Learning And TEaching
http://documen.tician.de/relate
Other
379 stars 113 forks source link

Discuss: it is a good idea to render latex generated images in flow page? #203

Open dzhuang opened 8 years ago

dzhuang commented 8 years ago

I have many material which require latex generated images (not just math equations), like complex tables with math, and some tikz pictures. Now I hope to generate those images by Relate, rather than generate them on my pc and then upload them by course git.

Is that a sound functionality?

I find this, which uses a templatetag named latex and uses a subprocess which call latex to generate those images in site media folder, and then render them. For Relate, my current idea is to add an allowed_attribute named latex, which requires a list of string (those latex code), and add a method get_latex_png_uri to do the compilation and conversion work and return a list of uri of those images, so that it can be referred to in the body method of PageBase.

inducer commented 8 years ago

I've had the same need, and I'm symathetic to adding something. My preferred way might be through a Jinja macro, like

{% call latex() %}
\begin{tikzpicture}
...
\endtikzpicture}
{% endcall %}

This could then be rendered as an <img> tag with a data: URL, which would remove concerns of having to serve the resulting image from somewhere.

dzhuang commented 8 years ago

That sounds good. But I have little knowledge about Jinja right now. My limit understanding for Jinja is that it is a template renderer, so I don't know how that macro latex can call a python subprocess to compile the block inside. And I also have not idea how to generate those data: URL. Can you give some hint or example? Thanks.

Update: Do you mean that the macro is to pass some variables to the latex code inside that block. And the data: URL is actually pointed to a .tex file in git. Then the compilation of the tex file is done if the url starts with data:?

dzhuang commented 8 years ago

OIC, by data: url, you mean base64.

inducer commented 8 years ago

This is how this could fit together:

from jinja2 import Template

template = Template(r"""
Hi there!
{% call latex() %}
\begin{tikzpicture}
  \node [circle] {Hi!};
\end{tikzpicture}
{% endcall %}
""")

def latex_macro(caller):
    tex_source = caller()
    return "<img src='data:base64...' alt='%s'>" % tex_source

print(template.render(latex=latex_macro))         
dzhuang commented 8 years ago

Thanks, I'll have a try! Shall I make latex a built-in macro for RELATE?

dzhuang commented 8 years ago

@inducer can you looking into this https://github.com/inducer/relate/pull/207? Thanks.

inducer commented 8 years ago

I'm really overloaded right now. It'll be another few days until I have time to take a look. Sorry!

dzhuang commented 8 years ago

That's OK.

2016-05-18 6:19 GMT+08:00 Andreas Klöckner notifications@github.com:

I'm really overloaded right now. It'll be another few days until I have time to take a look. Sorry!

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/inducer/relate/issues/203#issuecomment-219871554