#header,next=MISC.html,prev=./FXY2.html

As a post-Covid project I have implemented a javascript preprocessor which converts $\TeX$ code into responsive HTML.

There are some obvious things that can be done to improve the display of math on small touch screens. From most desirable to least

However those things are not always enough for display equations. This is where the preprocessor comes in.

Preprocessor

The preprocessor converts TeX code to responsive HTML code. These are the steps in the process:

Algorithms

flow

The flow algorithm is very similar to normal text flow and is most suitable for homogenous text like polynomials. The indent parameter tells the algorithm to indent lines after the first to the level of the first equals sign. The left parameter tells the algorithm to break just before plus and minus signs (normally it breaks just after).

#flow,indent,left
\Delta = 256a^3e^3 - 192a^2bde^2 - 128a^2c^2e^2 + 144a^2cd^2e
- 27a^2d^4 + 144ab^2ce^2 - 6ab^2d^2e - 80abc^2de
+ 18abcd^3 + 16ac^4e - 4ac^3d^2 - 27b^4e^2
+ 18b^3cde - 4b^3d^3 - 4b^2c^3e + b^2c^2d^2

This is the above code rendered with an equation label.

#flow,indent,left \Delta = 256a^3e^3 - 192a^2bde^2 - 128a^2c^2e^2 + 144a^2cd^2e - 27a^2d^4 + 144ab^2ce^2 - 6ab^2d^2e - 80abc^2de + 18abcd^3 + 16ac^4e - 4ac^3d^2 - 27b^4e^2 + 18b^3cde - 4b^3d^3 - 4b^2c^3e + b^2c^2d^2

To see the math respond to screen size change on a mobile device, rotate the device to switch between portrait and landscape mode.

fold

The fold algorithm is a text flow algorithm in which the line breaks occur at predetermined positions and in a predetermined order. It is better for inhomogenous text like matrices and integrals. For a large equation which won't fit on one line it usually looks best to do the first break after the top level equals sign. Then if needed, another break around the middle of what's remaining.

#fold
\begin{vmatrix}
1 & x_1 & x_1^3 & x_1^4 \\
1 & x_2 & x_2^3 & x_2^4 \\
1 & x_3 & x_3^3 & x_3^4 \\
1 & x_4 & x_4^3 & x_4^4 \\
\end{vmatrix} = $
\big(x_1x_2 + x_1x_3 + x_1x_4 + x_2x_3 + $
x_2x_4 + x_3x_4\big) \cdot \prod_{i\lt j} (x_j-x_i)

This is the above code rendered with an equation label.

#fold \begin{vmatrix} 1 & x_1 & x_1^3 & x_1^4 \\ 1 & x_2 & x_2^3 & x_2^4 \\ 1 & x_3 & x_3^3 & x_3^4 \\ 1 & x_4 & x_4^3 & x_4^4 \\ \end{vmatrix} = $ \big(x_1x_2 + x_1x_3 + x_1x_4 + x_2x_3 + $ x_2x_4 + x_3x_4\big) \cdot \prod_{i\lt j} (x_j-x_i)

stack

A stack is a set of equations which can be written on one line separated by comma's if there is room. Otherwise they can be stacked and aligned vertically at the equals signs. The width parameter is the container width which trigger's the transition from horizontal to vertical format.

#stack,width=500
x_3 = a \cdot \frac {x_1y_1 + x_2y_2} {x_1x_2 + y_1y_2}
y_3 = a \cdot \frac {x_1y_1 - x_2y_2} {x_1y_2 - x_2y_1}

This is the above code rendered with an equation label.

#stack,width=500 x_3 = a \cdot \frac {x_1y_1 + x_2y_2} {x_1x_2 + y_1y_2} y_3 = a \cdot \frac {x_1y_1 - x_2y_2} {x_1y_2 - x_2y_1}

train

A train is a string of equations coupled together with equals signs. On a wide screen they look good written on one line, but on a narrow screen they look better written as a vertical stack with the equals signs vertically aligned. The width parameter is the container width which trigger's the transition from horizontal to vertical format.

#train,width=700
g_2
= -4(\epsilon_1\epsilon_2 + \epsilon_2\epsilon_3 + \epsilon_3\epsilon_1)
= \tfrac {1} {24} (A^2 + B^2 + C^2)
= \tfrac {1} {12} (12ae - 3bd + c^2)

This is the above code rendered with an equation label.

#train,width=700 g_2 = -4 (\epsilon_1\epsilon_2 + \epsilon_2\epsilon_3 + \epsilon_3\epsilon_1) = \tfrac {1} {24} (A^2 + B^2 + C^2) = \tfrac {1} {12} (12ae - 3bd + c^2)

Where To Next?

The above set of algorithms are a good starting point for implementing responsive layout features in a LaTeX to HTML javascript library. Implementing natively would have the following advantages