Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering latex/mathjax equations in django

The first thing I want to do is profusely thank any person that takes the time to read this. The post looks long, but this is mostly because I formatted it with bullet points and I wanted to be as detailed as possible and provide a minimal working example. This is my first time embarking on an independent coding project and my first ever Stack Exchange post so even though I checked to not break any rules, I might have missed something.

I am working on my first django project (supposed to be a simple blog) and I think I am running into a lot of unknown unknowns. I want to: - Render [;\LaTeX] style mathematical formulae in an article template that I am using. The template is an HTML file and the source code is found here. It extends this base template

I have tried

  • the django-mathjax package. After I followed the instructions and my project did not compile, I tired the suggestion in this stack exchange post to no avail.
  • With code from this youtube video, I created a sample html file that did what I wanted but it fell apart when I added it to the base_layout template in my project.

At this point, I got desperate. I then tried:

  • django-tex
  • django-latexify
  • I read this stack exchange post and it almost made sense
  • this post in /r/django which does say to use mathjax, but I unfortunately have been failing at this so far Based off of all this, I have some questions that hopefully you all can help me out with.
    1. Suppose you want to do something in python. You find a random package on github that claims to do it for you, but you have never heard of it before, it has not been updated in 2 years. Do you trust it? How do you go about selecting packages for a project/ goal if it requires using a package that is not one of the canonical ones (like numpy) that you KNOW you can trust?
    2. Why is MathJax the best way to incorporate latex style equations to websites/ HTML/ CSS
    3. I feel like the first package should have worked and maybe I am making a mistake with my virtual environment. Is there a way to confirm this (sorry I know this is super vague)
    4. Most important: Given that I have tried so many different things and none of them worked, how do I go about discerning what my next step is. Should the fact that my templates are HTML files guide me?
    5. If the answer to my dilemma is in the MathJax documentation or the Django documentation, how would I effectively search for it?
like image 928
Carlos Avatar asked Dec 12 '19 07:12

Carlos


1 Answers

I'll risk expanding on one aspect, that of template files. It's perhaps a shame that Django doesn't come with a common base template, that one has to actively replace should it not fulfill one's needs. Sooner or later one will need one, and it would be nice if installing Django gave you a canonical one and all the beginner tutorials used it.

Anyway, taking Kent Shikama's answer, my site's template for generating his html would look like

{% extends "myproject/base.html" %}

{% block extrascripts %}
  <script type="text/javascript" id="MathJax-script" async
    src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js">
  </script>
{% endblock extrascripts %}

{% block content %}
$$x=\frac{-b+\sqrt{b^2-4ac}}{2a}$$
{% endblock content %}

As for what that base.html might be, a simple one that would yield the the html in that answer plus a couple of HTML comments, would be

<html>
<head>
{% block scripts %}
<!-- put here any scripts (such as JQuery ) that you want to be loaded 
     UNLESS the specific template requires otherwise 
     by overriding this block. Add scripts via extrascripts below. 
-->
{% endblock scripts}
{% block extrascripts %}{%endblock extrascripts%}

{% block css %}
<!-- put here any CSS styling you want to be loaded 
     UNLESS the specific template requires otherwise, 
     by overriding this block. Add extra styling via extracss below. 
-->
{% endblock css %}
{% block extracss %}{% endblock extracss %}

</head>

<body> 
  {% block content %}{% endblock content %}
</body>
</html>

If it's a maths blog project, you might well promote the MathJax script from this individual template, into block scripts in base.html. Your templates would then contain only a content block and could freely use MathJax.

The key thing is that your specific page templates only need to include scripts and styling that are different from the scripts and styling that you use with every page on your project. You can then re-style your entire site just be altering that base html.

In the real world an intermediate level of templates that extend base.html and are in turn extended by individual page templates is common, so an entire class of pages can be specified without repetition.

like image 105
nigel222 Avatar answered Oct 13 '22 11:10

nigel222