Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make formula with mathjax responsive

I have a big problem with displaying math expressions in my responsive website. In particular, if you visit this page with a smartphone, you can see that most of the math expressions are cut off due to responsive design. I'm using Joomla and the Jextbox Equation plugin for displaying them.

I want formulas on my page to behave like this one on Wikipedia. If you open this page in your smartphone you can see that, where formulas are too long, they are not cut off. They are displayed if you scroll from left to right. I would like to do the same in my page.

like image 857
mbistato Avatar asked Apr 27 '15 11:04

mbistato


People also ask

Is MathJax slow?

I found that MathJax is extremely slow. Is there a better, lighter and faster Javascript library that can render mathematical notation equally well? It may be your browser that is slow.


2 Answers

From the documentation on activating MathJax's line-breaking:

The HTML-CSS and SVG output processors implement (most of) the MathML3 >automatic line-breaking specification. (The NativeMML output processor relies on the browser’s native MathML support to handle line breaking when it is used.) Since line-breaking takes extra processing and so can slow down the mathematical output, it is off by default, but you can enable it by adding

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
 "HTML-CSS": { linebreaks: { automatic: true } },
         SVG: { linebreaks: { automatic: true } }
});
</script>

to your page just before the tag that loads MathJax.js itself.

However, in your example, there's a lot of tabular mathematics, i.e., layout using <mtables> (though people using TeX as input are often not aware, MathJax converts all inputs to MathML internally).

The problem is: mtables can't really be line-broken, much like html tables can't be line-broken -- responsive tables need some JS help and, more importantly, additional semantic markup.

PS: For what it's worth, we're actually working on semantic heuristics for mathematics to do something better than linebreaking but that's ongoing R&D with no ETA.

like image 190
Peter Krautzberger Avatar answered Dec 14 '22 01:12

Peter Krautzberger


I have visited user3700643's page and see that there are still problems--- this when I use Firefox's Responsive Design View at 320×487. My solution is doofus I know, as it requires some duplication, but I have used it with not only MathJax (just now) but in the past for tables and other things--- all in a Twitter Bootstrap grid environment, but I'm pretty sure that that doesn't matter.

I have been using the CSS3 @media Rule. Here is my particular version:

@media(max-width:768px){
  .wide {display:none;}
  .mobilefont {font-size:0.8em;}
}
@media(min-width:768px){
  .tight {display:none;}
}

I just threw the mobilefont bit in just to further inspire you. I do sometimes use it. So, what I do is wrap an equation--- I'm using Latex with MathJax--- with a div and give the div the class "wide" if the equation is formatted for a wide screen and "tight" if it's formatted for a small screen. You can see how the max-width/min-width values in the CSS3 effect the disappearance of divs that have those class names according to whether the screen width is greater than or less than 768px, which is the popular breakpoint (but others can be defined by you).

That is, I duplicate my big-screen \begin{align}\end{align} block. I paste the copy in right under the original and wrap each with divs and give the two divs those two class names. I then edit the one with the "tight" class name to make it work in the Responsive Design View with a small screen size.

I have also done this outside of the MathJax context, with plain tables. I have, for example, used <td class="wide"> in every <td> of a given column of a table to make that column disappear on small-screen devices.

On your page where you have "L'integrale di partenza sarà allora la somma degli integrali sui due domini", inside your "tight" div version you could, if you were using a \begin{align}\end{align} block (but I see you're actually using \begin{eqnarray*} which I haven't tested with this scheme), simply put an ampersand in front of that double integral on the left, and then new-line characters \\ after that integral to break to a new line. And put ampersands in front of each equal sign on every line. That way the lines that start with equals signs will be moved far to the left, aligned under the expression for the integral instead of to the right of it.

That would not be too confusing, I don't think. It's only slightly unorthodox. But see my comment to Peter Krautzberger's answer... I have found that his script doesn't work when I use the ampersand. But the point is that it isn't needed at all with my CSS3 approach since I simply specify all of the re-formatting myself.

like image 41
Mike O'Connor Avatar answered Dec 14 '22 01:12

Mike O'Connor