How to change alignment of displayed equations in IPython Notebook?

gauss256 picture gauss256 · Feb 5, 2015 · Viewed 14.7k times · Source

I would like my MathJax displayed equations in IPython Notebook to be aligned at the left instead of centered. This is controlled by a core configuration option displayAlign in MathJax as described here.

I have tried to set this option in IPython Notebook by adding this to my config.js file

MathJax.Hub.Config({ 
    displayAlign: "left"
});

but it doesn't have any effect.

How can I set MathJax core configuration options in IPython Notebook?

[Update] I have found one way that works: add the above configuration lines not to config.js but to mathjaxutils.js. In my case (Windows 8) this file is found here: C:\Anaconda\Lib\site-packages\IPython\html\static\notebook\js\mathjaxutils‌​.js. This is not a great solution though because it involves modifying a file that will presumably get overwritten the next time I update IPython.

[Update] The technique suggested by @Ian in the comments does work, but just one notebook at a time. To summarize, I created a file my_css.css whose content is

<script>
    MathJax.Hub.Config({
        displayAlign: 'left'
    });
</script>

In the notebook, if I run this cell

from IPython.core.display import HTML
css_file = 'my_css.css'
HTML(open(css_file, "r").read())

displayed equations do get left aligned, as desired.

However, I would like this to be the default for all my notebooks. I tried adding this to my custom.js

MathJax.Hub.Config({
    displayAlign: 'left'
});

and for good measure added this to my custom.css

<script>
    MathJax.Hub.Config({
        displayAlign: 'left'
    });
</script>

But neither has any effect. If there is a way to make this setting a default for all notebooks without modifying the core IPython files, that would be perfect.

Answer

Dustin Helliwell picture Dustin Helliwell · May 24, 2017

Use \begin{align} and \end{align}. This does not exactly answer the question but it has the desired effect. For example try:

$
\begin{align}
\frac{1}{2} \times \frac{3}{2} = \frac{3}{4}
\end{align}
$

The above renders exactly the same as,

$$
\frac{1}{2} \times \frac{3}{2} = \frac{3}{4}
$$

except that it is left justified.

This approach has the added advantage that other alignments can be be added, as in:

$
\begin{align}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{align}
$

This last code block is from Motivating Examples in the Jupyter Notebook docs.

Other examples of aligning equations can be found here Aligning several equations