# Load MathJax

**URL:** <https://forum.elixirforum.com/t/load-mathjax/8471>\
**Category:** Questions\
**Tags:** phoenix\
**Created:** [September 8, 2017, 5:04pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471 "2017-09-08T17:04:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![UnderFenex](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/underfenex/32/21692_2.png) [@UnderFenex](https://forum.elixirforum.com/u/UnderFenex)\
**Post date:** [September 8, 2017, 5:04pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/1 "2017-09-08T17:04:52Z")

</div>

I’m trying to load MathJax, but not from its CDN, but from local files (the server will be offline).

So:

1. I downloaded MathJax: [http://docs.mathjax.org/en/latest/installation.html#obtaining-mathjax-via-an-archive](http://docs.mathjax.org/en/latest/installation.html#obtaining-mathjax-via-an-archive)
2. Placed the whole (2.7MB )in web/static/vendor/js/
3. Load in the App (/web/templates/layout/app.html.eex):

```elixir
<script src="<%= static_path(@conn, "/vendor/mathjax/MathJax.js") %>"></script>

```

1. Configure brunch-config.js to precompile it (am i right?)

```elixir
stylesheets: {
      joinTo: "css/app.css",
      order: {
        before: [
                  "web/static/vendor/js/jquery.min.js",
                  "web/static/vendor/js/lodash.js",
                  "web/static/vendor/js/mathjax/MathJax.js"
                ],
        after: ["web/static/css/app.css"] // concat app.css last
      }
    }

```

1. When I start the server, it takes too long:

```elixir
08 Sep 11:54:29 - info: compiling...
08 Sep 11:54:33 - info: compiling
08 Sep 11:54:37 - info: compiling.
08 Sep 11:54:42 - info: compiling..
08 Sep 11:54:48 - info: still compiling...
08 Sep 11:54:51 - info: compiled 264 files into 2 files, copied 3 in 54.2 sec

```

and the it crashes:

```elixir
GET /vendor/mathjax/MathJax.js
[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /vendor/mathjax/MathJax.js (Project.Router)
    (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
    (cowboy) /vagrant/www/updevs_js/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

```

Why this happen? What do I need to do to load MathJax.js?

---

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [September 8, 2017, 5:19pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/2 "2017-09-08T17:19:06Z")

</div>

1. You are bundling your JavaScript with your CSS.
2. The default configuration bundles all JavaScript into the `assets/js/app.js` bundle. Bundled code isn’t accessed with a separate `<script>` element but via `import` in the JavaScript code itself.

Please have a look at my recent post [here](https://forum.elixirforum.com/t/using-newly-installed-js-library/8067/4).

---

<div class="post-metadata">

**Author:** ![UnderFenex](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/underfenex/32/21692_2.png) [@UnderFenex](https://forum.elixirforum.com/u/UnderFenex)\
**Post date:** [September 8, 2017, 5:29pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/3 "2017-09-08T17:29:50Z")

</div>

Hi, @peerreynders, I read your post, however I don’t understand it too much (I’m still learning phoenix)

1. Should I delete it from brunch-config.js then?
2. How do I import it in app.js?
3. Should I keep the mathjax folder in static/vendor?
4. Should I remove `<script src="<%= static_path(@conn, "/vendor/mathjax/MathJax.js") %>"></script>`, from app.html.eex then?

---

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [September 8, 2017, 5:36pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/4 "2017-09-08T17:36:47Z")

</div>

To get the hang of things I would recommend you go through the motions of that post - using some working minimal html page with a minimal mathjax example - and then splice that into a phoenix practice project as described. [mathjax is available through npm](https://www.npmjs.com/package/mathjax).

Basically this has less to do with Phoenix but more with being forced into the modern world of JavaScript package management with npm (or yarn) and ES6 (or CommonJS) modules.

---

<div class="post-metadata">

**Author:** ![UnderFenex](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/underfenex/32/21692_2.png) [@UnderFenex](https://forum.elixirforum.com/u/UnderFenex)\
**Post date:** [September 8, 2017, 5:39pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/5 "2017-09-08T17:39:17Z")

</div>

I see. MathJax npm doesn’t include PNG fonts. Is there a mechanics with loading the MathJax files from vendor, or any other path to be loaded without using npm?

---

<div class="post-metadata">

**Author:** ![UnderFenex](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/underfenex/32/21692_2.png) [@UnderFenex](https://forum.elixirforum.com/u/UnderFenex)\
**Post date:** [September 8, 2017, 5:48pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/6 "2017-09-08T17:48:38Z")

</div>

I am loading MathJax from app.js as you said:

```elixir
import MathJax from "mathjax/MathJax.js"

```

But the server keeps taking so much time, and also got this error (But the package is not and npm one, this is pretty confusing :icon_sad: )

```elixir
error: Resolving deps of web/static/js/app.js failed. Could not load module '/MathJax.js' from '/vagrant/www/updevs_js/web/static/js'. Possible solution: add '' to package.json and `npm install`.

```

---

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [September 8, 2017, 6:32pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/7 "2017-09-08T18:32:32Z")

</div>

If you look in the default `brunch-config.js`:

```javascript
  conventions: {
    // This option sets where we should place non-css and non-js assets in.
    // By default, we set this to "/assets/static". Files in this directory
    // will be copied to `paths.public`, which is "priv/static" by default.
    assets: /^(static)/
  },

```

So if you put mathjax somewhere under `assets/static` (like `assets/static/vendor`) it should be copied to the equivalent public location (e.g. `/vendor`) at which point it should become accessible legacy-style via a `<script>` tag. `/js` and `/css` are essentially destinations for the brunch bundles.

So if you have `assets/static/vendor/js/mathjax/MathJax.js` a script element should be able to pick it up at `/vendor/js/mathjax/MathJax.js` (you were missing the `/js` in your opening post).

The [Static Assets – Phoenix v1.3.0 - HexDocs](https://hexdocs.pm/phoenix/static_assets.html) guide seems to be AWOL right now but Google still has a cached copy.

FYI: [mathjax-node-svg2png](https://www.npmjs.com/package/mathjax-node-svg2png) seems to supply the PNG functionality.

---

<div class="post-metadata">

**Author:** ![UnderFenex](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/underfenex/32/21692_2.png) [@UnderFenex](https://forum.elixirforum.com/u/UnderFenex)\
**Post date:** [September 8, 2017, 9:43pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/8 "2017-09-08T21:43:53Z")

</div>

I am making progress, I decided to go your way and installed the npm package. It being loaded fast as a node module, however the server keeps asking for more extensions:

```elixir
GET /extensions/MathMenu.js
[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /extensions/MathMenu.js

```

What should I do to load complete mathjax and to configure it? Have you done it already?

---

<div class="post-metadata">

**Author:** ![UnderFenex](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/underfenex/32/21692_2.png) [@UnderFenex](https://forum.elixirforum.com/u/UnderFenex)\
**Post date:** [September 8, 2017, 10:15pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/9 "2017-09-08T22:15:21Z")

</div>

I follow the requiremente using:

static/js/mathjax.js

```elixir
import mathjax from "mathjax"
import MathMenu from "mathjax/extensions/MathMenu.js"
import MathZoom from "mathjax/extensions/MathZoom.js"

```

and in static/js/mathjax.js

```elixir
import mathjax from "./mathjax"

```

Now there are no errors, but MathJax doesn’t render.

---

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [September 9, 2017, 3:08am UTC](https://forum.elixirforum.com/t/load-mathjax/8471/10 "2017-09-09T03:08:16Z")

</div>

I’m getting the impression that MathJax’s entire architecture [is inherently “Module hostile”](https://github.com/mathjax/MathJax/issues/951#issuecomment-77339248) (i.e. resistant to JS modernization - due to it’s size it heavily relies on lazy loading parts of itself) - support for bundling (browserify, webpack, etc.) was once a [v3.0 milestone](https://github.com/mathjax/MathJax-node/issues/147#event-554781257) but has since been removed. Also the `mathjax-node-*` packages seem to be entirely targeted for server-side nodeJS use rather than “modularized browser JS”.

So I’m left with the impression that this has to be done “old-school” (though frankly I’d try to avoid this monster at all cost). I essentially dropped the entire _massive_ archive into `priv/static` - i.e.

`priv/static/MathJax-2.7.2`

i.e. MathJax is **_so_** massive that you need to keep it away from brunch.

in `lib/mjx_web/templates/layout/app.html.eex` I placed:

```xml
    <title>Hello Mjx!</title>
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>">

    <script type="text/x-mathjax-config">
     MathJax.Hub.Config({
       extensions: ["tex2jax.js"],
       jax: ["input/TeX","output/HTML-CSS"],
       tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]}
     });
    </script>
    <script type="text/javascript" src="<%= static_path(@conn, "/MathJax-2.7.2/MathJax.js") %>"></script>
  </head>

```

In `lib/mjx_web/templates/page/index.html.eex` I placed (between the two `col-lg-6` `<div>` elements):

```xml
  <p>
    \begin{align}
    \dot{x} & = \sigma(y-x) \\
    \dot{y} & = \rho x - y - xz \\
    \dot{z} & = -\beta z + xy
    \end{align}
  </p>

```

Finally I had to go into `lib/mjx_web/endpoint.ex`

```iex
  plug Plug.Static,
    at: "/", from: :mjx, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

```

and add the mathjax folder `MathJax-2.7.2`

```iex
  plug Plug.Static,
    at: "/", from: :mjx, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt MathJax-2.7.2)

```

at that point the Lorenz Equations showed up on the home page.

However rendering is sluggish enough for the unrendered source to briefly show at times upon reloading.

Edit: Modification of `endpoint.ex` is unnecessary if instead of

`priv/static/MathJax-2.7.2`

you use

`priv/static/js/MathJax-2.7.2` (and of course change the reference in the `app.html.eex` template).

---

<div class="post-metadata">

**Author:** ![UnderFenex](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/underfenex/32/21692_2.png) [@UnderFenex](https://forum.elixirforum.com/u/UnderFenex)\
**Post date:** [September 10, 2017, 5:53pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/11 "2017-09-10T17:53:03Z")

</div>

Thanks! @peerreynders. I know that MathJax is not suited as a node module but the solution you gave is perfect 🙂

---

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [September 10, 2017, 7:03pm UTC](https://forum.elixirforum.com/t/load-mathjax/8471/12 "2017-09-10T19:03:23Z")

</div>

> [@UnderFenex](#):
>
> I know that MathJax is not suited as a node module

Ultimately it has nothing to do with NodeJS - modules have become an essential component of modern JavaScript as a means of dependency management - once JavaScript was used to do more than just twiddle with the DOM and CSS.

Browserify was created in 2011 to leverage “dependency management” via modules on browsers - and with ES2015 modules have become an intrinsic part of JavaScript.

`<script type="module">` is now supported in [Chrome 61](https://www.chromestatus.com/feature/5365692190687232) but effectively modules have been available for in-browser development for the past 6 years through build tools (just like transpilers made it possible to use future ECMAScript features before browsers implemented them).

Now this is total speculation on my part but I suspect that the “MathJax consortium” has seen the writing on the wall. Back in 2009 it was probably OK to just cater to PC-based browsers with unmetered connections and co-opt precious processing time from the only available GUI thread to do **_a lot_** of background rendering work. These days performance could be improved by using [web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) but that wouldn’t address the real issue - web content consumption through mobile devices via metered connections and power-conscious browsers.

So I interpret the proliferation of `MathJax-node-*` projects as a potential move toward “server-supported-rendering” - i.e. while the “math mark-up” may be assembled inside the browser, that mark-up is sent to the server for rendering and the resulting SVG is returned for inclusion in the page. That should allow for a minimally sized client-side MathJax library with the tradeoff of increased client-server chatter - and of course the need for server-side (NodeJS) support.

---

<div class="post-metadata">

**Author:** ![ram\_vishy](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/r/48db29/32.png) [@ram\_vishy](https://forum.elixirforum.com/u/ram_vishy)\
**Post date:** [November 27, 2017, 4:28am UTC](https://forum.elixirforum.com/t/load-mathjax/8471/13 "2017-11-27T04:28:39Z")

</div>

> [@peerreynders](#):
>
> \<p\>  
> \begin{align}  
> \dot{x} & = \sigma(y-x) \  
> \dot{y} & = \rho x - y - xz \  
> \dot{z} & = -\beta z + xy  
> \end{align}  
> \</p\>

Thanks, @peerreynders. Your step-by-step instructions were very helpful to me as well!
