django-mathfield

A Django model field for writing and displaying LaTeX


Keywords
django, math, latex, katex
License
BSD-3-Clause
Install
pip install django-mathfield==0.1.0

Documentation

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 Setup

Your server needs to have Python 2.7 and Django 1.7.

Get it installed with:

$ pip install django-mathfield

Add 'mathfield' to your INSTALLED_APPS in your Django project's settings.py.

Add a MathField to one of your models like this:

from django.db import models
import mathfield

class Lesson(models.Model):
    lesson_plan = mathfield.MathField()

Get live previews of the rendered LaTeX while you're editing in the Django admin by adding MathFieldWidget as a widget when registering your model in admin.py:

from django.contrib import admin
from django import forms
from yourapp.models import Lesson
import mathfield

class LessonAdminForm(forms.ModelForm):

    class Meta:
        widgets = {
            'lesson_plan': mathfield.MathFieldWidget
        }


class LessonAdmin(admin.ModelAdmin):
    form = LessonAdminForm


admin.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 %}
        <link rel="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 the mathfield.css stylesheet in your template head, and include |safe with 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 API

You 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 have node.js (v0.10+) installed and it must be on your system path as an executable called node. 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 Models

There are two ways to pass data to a MathField: as a string, or as a dictionary with the keys raw and html. If you pass a string, the html will be rendered for you.

Let's say you are using the Lesson model from above, which has a lesson_plan column 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 key raw and the already rendered HTML under the key html. 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 than KaTeX.

The function store_math provided 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:

import mathfield

# 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 Lookups

When you look up an existing MathField, you get a dictionary with the keys raw and html:

lesson = Lesson.objects.get(id=0)
print lesson.lesson_plan['raw']
# One half is $\frac{1}{2}$

print lesson.lesson_plan['html']
# the html for your template...

Just Getting Some HTML

If you just want to pass in a string and get the HTML, use render_to_html:

import mathfield

html = mathfield.render_to_html('One half is $\\frac{1}{2}$.')