In some notebooks, I have large LaTeX formulas which get chopped off for mobile displays. Has anyone figured out a convenient needs-no-manual-fiddling way to work around this?
My quick hacky workaround is a combination of width-based optional line breaks and CSS transformation based resizing.
Notice that the second equation has an optional line break that gets inserted between two parts of the right hand side, and then the whole formula gets scaled down and some negative vertical margins inserted if the view gets too narrow. The numbers there are more or less based on trial and error until something more or less works, rather than being based on directly measuring the size of the rendered LaTeX.
It might be nice to figure out a way to build resizing into the tex.block template literal, or figure out a way to automate creating a wrapper div with the appropriate CSS.
Has anyone else dealt with this in their notebooks? What did you do?
For context, @tom has a nice notebook about other kinds of responsive content,
Itâs not great, but maybe overflow-x: scroll would work? The output would still be cropped, but you can scroll horizontally. (Weâve been considering adding this as a default style for KaTeX output, but it breaks margin collapsing, so weâre still looking for a better solution.)
Aside: the rainbow colors youâre using for the cyclic cubic spline are nice. How did you choose them?
Thanks! I chose them manually, with more effort than this kind of thing really should need because I donât have proper tooling. Took about 15 minutes with a lot of copy/pasting of coordinates from one text box to another, when it really should be achievable in 2 minutes. (Also, I have spent many hours in the past on similar tasks, so am pretty efficient using hacky mishmashes of tools.)
One of these days Iâll get around to making some proper color pickers âŚ
As for the idea of the color choices: CIELAB L* goes in increments of 10, and a*/b* were just eyeballed.
Itâs not great, but maybe overflow-x: scroll would work?
I think scaling a formula down is actually better than scrolling, at least for iPhone (I donât have other smartphones to test). Even at half size or so the rendering is sharp enough to read if you look close, and mobile safari makes it pretty easy to zoom in on html/svg/etc. content using a pinch gesture. So a reader can see both a whole equation or a small zoomed section.
I use a similar scaling technique with SVG for visualizations that canât be simplified for smaller screens, such as this choropleth. With SVG, itâs just a matter of setting a viewBox and width & height styles:
Unfortunately this doesnât work with KaTeX because itâs renders to HTML. Maybe it would be nice to fork KaTeX and implement a pure SVG renderer⌠but thatâs a pretty big project.
I also tried using foreignObject to embed KaTeX within SVG to use the above technique, but it seems that KaTeX has some line-wrapping logic thatâs based on the window width, and so it wraps the output before it can be scaled down by SVG.
Alternatively, you could document.body.appendChild(element) and then element.remove after youâve measured the width and height. Or, you could defer the getBoundingClientRect using requestAnimationFrame (or possibly Promise.resolve().then would be sufficient).
It seems that when I call document.body.appendChild(elem), I get a different width than when it gets added in place. I canât quickly figure out why.
But await new Promise(callback => requestAnimationFrame(callback)); seems to be fine instead of waiting 10ms. (Though in practice there isnât too much difference as re-rendering all of the math elements on the page usually takes a good bit longer than that anyway.)
(1) I wrapped any math I knew would be too wide with a div with class âfitToWidthâ. After the math has been processed by KaTeX (to save rendering the whole page again), I measured the width of the div.fitToWidth, then dropped the font-size of the div, measured again, dropped again, until it fitted.
However, it often looked funny being smaller than the surrounding math.
(2) I now mostly use overflow-x:auto. When I first used it, the problem for the user was it wasnât always obvious that horizontal scrolling was required (especially with certain screen widths that looked on first sight that the math was âcompleteâ already). A horizontal scroll element only appears once scrolling starts, in most browsers. So I use the pseudo element ::-webkit-scrollbar to make it obvious a scroll is possible. (While MDN advises against its use, itâs better than nothing.)
There are some examples on this page, starting about 2/3 of the way down (horizontal scrolling is needed for most phone widths):