# mathjax/MathJax

### Subversion checkout URL

You can clone with
or
.

# MathJax_Font_Test breaks responsive layouts in mobile browsers#681

Closed
opened this Issue · 10 comments

### 4 participants

MathJax adds a MathJax_Font_Test div into the body element when typesetting math formulas. Sometimes this div is too wide that it breaks responsive layouts, and add extra spaces to the web pages in mobile browsers.

In my case, I add viewport meta tag to the head of the html file

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

This makes my web page occupy the whole width of the browser. But when MathJax starts to typeset some formulas in this page, such as

$\xi\in(a,b),f'(\xi)=0$

It inserts a div of size 431px by 59px as follows:

<div id="MathJax_Font_Test" style="position:absolute;visibility:hidden;
top: 0px; left: 0px;width: auto; padding: 0px; border: 0px; margin: 0px;
white-space: nowrap; text-align: left; text-indent: 0px;
text-transform: none;  line-height: normal; letter-spacing: normal;
word-spacing: normal; font-size: 40px; font-weight: normal;
font-style: normal; font-family: MathJax_AMS, Times;">
MATHJAX AMS ˆ&#xEFFD; &#xEFFF;</div>

At present, most of the mobile phones have device widths between 320px and 360px, therefore this div makes browsers add extra spaces to the page.

In desktop browsers, I could solve this problem by adding "overflow: hidden" to body element. But this way is not working in mobile browsers.

Owner

Thanks for reporting this. Do you have a minimal live example to demonstrate what goes wrong (on which systems)?

In android 4.1, I have tested the stock browser, mobile firefox and mobile chrome with the following web page:

<!DOCTYPE html>
<html>
<title>MathJax Font Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
<style type="text/css">
html, body { margin: 0; width: 100%; overflow: hidden; }
#main { background: gray; }
</style>
<body>
<div id="main">$$\xi\in(a,b),f'(\xi)=0$$</div>
<script>
var width = window.innerWidth, height = window.innerHeight;
var main = document.getElementById("main");
main.style.width = width + "px";
main.style.height = height + "px";
</script>
</body>
</html>

You could see extra spaces in the right when draging the web page.

I'm wondering why we need so many characters MATHJAX AMS ˆ&#xEFFD; &#xEFFF; in MathJax_Font_Test div?

Owner

Thanks for the example. I can reproduce the problem on Android 4.3 with the stock browser. This also happens on Chrome for Android (though less pronounced).

@dpvc is this expected behavior?

Owner

Also confirmed on mobile Firefox for Android.

Collaborator

is this expected behavior?

It is something we know about, and it is working as it "should" based on the current font test mechanism, so in that sense, "yes". But it would be good to be able to avoid the problem. I suspect that one solution would be to clear the font test div after it is used, so that the long string isn't left in place, and the content size would not be excessively long. That is probably easiest.

We ran into this some time ago and I suggested to the user at the time to alter the font sizes used for the test (which apparently worked for him). It might be that we could do that, either conditionally for mobile devices, or if the width of the page is small, or by some other condition.

In any case, I think it can be fixed, and could be included in the bug fix release that we are planning.

@zohooo, the reason that the string is so long is that it helps to have more characters involved in order to be able to detect the difference in width that indicates the presence of the font. The font detection is delicate, and longer strings help make it more reliable.

referenced this issue
Closed

### bullet-prime sequence causes horizontal overflow #591

Hey there. Any news about when this might happen?

I just put something like this into our page. It seems to at least clean up the problem, but it'd be nice to be able to remove the code at some point.

remove_div = function() {
div = \$("#MathJax_Font_Test");
if (div.length > 0) {
div.remove();
}
else {
console.log("cannot find mathjax font div");
}
}

MathJax.Hub.Startup.signal.Interest(function(message) {
if (message == "HTML-CSS Jax - Web-Font TeX/Main/Regular") {
MathJax.Hub.Queue(remove_div);
}
});
referenced this issue from a commit in dpvc/MathJax
 dpvc Clear font test div so that its width doesn't affect the page size. r… …esolves issue #681. 6c50f1d
referenced this issue from a commit in dpvc/MathJax
 dpvc Merge branch 'issue681' into develop. Issue #681. 76a4ee3
Collaborator

=> Merged

closed this
referenced this issue from a commit in dpvc/MathJax
 dpvc Fixed accidental switching of true and false when testing for local f… …ont. Issue #681. ba238a6
referenced this issue from a commit in dpvc/MathJax
 dpvc Merge branch 'issue681' into develop. Issue #681. 2afb801
Collaborator

I can't come up with a test case for this on one of the browsers we use in the test framework, so I'm marking it "do not write test case".

Owner

+1

added v2.4 Fixed and removed Merged labels