Package: django-mathfield
MathField is a model field for Django that allows you to input LaTeX and store
the compiled HTML on your database. It comes with a form for the Django Admin
that provides live previews of your rendered LaTeX.Installation and SetupYour server needs to havePython 2.7andDjango 1.7.Get it installed with:$ pip install django-mathfieldAdd'mathfield'to yourINSTALLED_APPSin your Django project’ssettings.py.Add aMathFieldto one of your models like this:fromdjango.dbimportmodelsimportmathfieldclassLesson(models.Model):lesson_plan=mathfield.MathField()Get live previews of the rendered LaTeX while you’re editing in the Django admin
by addingMathFieldWidgetas a widget when registering your model inadmin.py:fromdjango.contribimportadminfromdjangoimportformsfromyourapp.modelsimportLessonimportmathfieldclassLessonAdminForm(forms.ModelForm):classMeta:widgets={'lesson_plan':mathfield.MathFieldWidget}classLessonAdmin(admin.ModelAdmin):form=LessonAdminFormadmin.site.register(Lesson,LessonAdmin)After adding some data to your database, you can output the rendered HTML to
a template:<!DOCTYPE HTML><html><head>{% load staticfiles %}<linkrel="stylesheet"type="text/css"href="{% static 'mathfield/css/mathfield.css' %}"></head><body><div>Raw text/LaTeX: {{ lesson.lesson_plan.raw }}</div><div>Rendered HTML: {{ lesson.lesson_plan.html|safe }}</div></body></html>Make sure that you include themathfield.cssstylesheet in your template
head, and include|safewith the MathField HTML value. This will
give Django permission to render the text in that field as HTML. It is safe to
do this provided that you only update the HTML using the form in the Django
admin or the functions provided in the MathField API. Be very careful when
updating the HTML yourself!Developer APIYou can modify MathField data and compile LaTeX on your server without the admin
form if you would like. To be able to compile LaTeX serverside, you must havenode.js(v0.10+) installed and it must be on
your system path as an executable callednode. Note that this is not
necessary if you just use the admin form, as all compilation will occur in the
browser in this case.Instantiating ModelsThere are two ways to pass data to a MathField: as a string, or as a dictionary
with the keysrawandhtml. If you pass a string, the html will
be rendered for you.Let’s say you are using theLessonmodel from above, which has alesson_plancolumn that is a MathField. You can create a new instance
with:new_lesson=Lesson(lesson_plan='One half is $\\frac{1}{2}$.')new_lesson.save()You can also pass a dictionary that contains the raw text under the keyrawand the already rendered HTML under the keyhtml. This is
particularly useful if you want to generate the HTML yourself, perhaps because
you can’t install node.js on your server, or because you want to use a typesetting
library other thanKaTeX.The functionstore_mathprovided in the mathfield API is provided for
convenience. If you don’t know the HTML, you don’t have to provide it, and it
will be generated for you. Otherwise, you can pass in the HTML and it will just
use that. For example:importmathfield# if you already know the HTML:math_data=mathfield.store_math(raw_text,html)# if you don't:math_data=mathfield.store_math(raw_text)new_lesson=Lesson(lesson_plan=math_data)new_lesson.save()Database LookupsWhen you look up an existing MathField, you get a dictionary with the keysrawandhtml:lesson=Lesson.objects.get(id=0)printlesson.lesson_plan['raw']# One half is $\frac{1}{2}$printlesson.lesson_plan['html']# the html for your template...Just Getting Some HTMLIf you just want to pass in a string and get the HTML, userender_to_html:importmathfieldhtml=mathfield.render_to_html('One half is $\\frac{1}{2}$.')