To use the script, put this in the head of the script
<script src="https://cdn.jsdelivr.net/gh/TejasIsCool/MathToHTML@v0.1.7/dist/math-eq.min.js" defer></script>You can then enter your math equations in the body of the page like this
<m-eq>2^{2^3}</m-eq>Making content with <m-eq> preserves the spaces as written in the html file.
If you do not want to preserve spaces, you can use the <m-eqi> tag instead.
The tags will render inline (on the same line as written). They can wrap the text after them, if its big enough. By default, equations use the KaTeX math font ('KaTeX_Main', serif), which is loaded automatically. You can easily customize or override the font in your own CSS (e.g. m-eq { font-family: 'Times New Roman', serif; }).
The syntax has similarities with latex, but it a lot less features and more simpler than it.
-
Subscript/Superscript: You can write subscripts and superscripts. You can also write both together.
Syntax:base^{superscript}orbase_{subscript}orbase_{subscript}^{superscript}. The curly braces are not necessary, but are required if you want to have multiple stuff/characters in the superscript or subscript.
You can also chain this, so2^{3^{4^5}}works. -
Groupings: You can use curly braces
{}to "group" stuff into a single element.
Like in the example above<m-eq>2^{2^3}</m-eq>, the2^3is made into one element, which is then added as superscript to the 2 infront. You can also group stuff inside groups, recursively! So{{{{Also works}}}}.
To actually use the curly braces, you will have to escape them with a backslash, like\{and\}. -
Math:
-
Fractions:
\frac{numerator}{denominator}. Can use\atop{up}{down}to get a fraction like object without a line. -
Roots:
\sqrt{content}for square root, and\root{index}{content}for n-th root. -
Brackets and Delimiters:
\left{bracket}{content},\right{bracket}{content},\lr{bracket1bracket2}{content}.
The brackets will scale to the height of the content. You can use\{and\}for curly braces, and\langleand\ranglefor angle brackets.
This is useful for things like piecewise functions, or making matrices (below).Example:
<m-eq> T = \left{[}{\right{]}{ 1 2 3<br> 4 5 6<br> 7 8 9 }} </m-eq>
(Here, the right bracket is scaled to the height of the matrix, and the left bracket is scaled to the height of the right bracket, which is the same as the height of the matrix.)
There is no matrix command, so this is how they are made.
Can also do<m-eq>\lr{()}{\atop{n}{r}}</m-eq> is equivalent to <m-eq>\lr{(}{\atop{n}{r}}</m-eq> <!-- lr can recognize the corresponding right bracket for some symbols --> is equivalent to <m-eq>\lr({\atop{n}{r}}</m-eq>
You can do
\left[{...},\left({...},\left\{{...},\left|{...}, and similar forrightandlr -
Overline and Underline:
\overline{content}: Draws a horizontal line above the content. Shorter or taller text/content will auto adjust the line position.\underline{content}: Draws a horizontal line below the content.
-
Cancel:
\cancel{stuff}will draw a line over the content from top right to bottom left\cancelangle{angle}{stuff}draws a line at your specified angle. The angle must be in degrees.\canceldir{dir}{stuff}to set a direction to draw the lines at. The possible directions are:- Top left to bottom right:
tlbr|brtl|diagonal|diag|\|d - Top tight to bottom left:
trbl|bltr|antidiagonal|antidiag|adiag|/|ad - Top to bottom:
ud|du|vertical|vert|v|| - Left to right:
lr|rl|horizontal|horiz|h|-
- Top left to bottom right:
-
-
Fonts and Alphabets:
\mathbb{content}: Converts ASCII letters and digits inside{content}into blackboard bold / double-struck characters (e.g.,\mathbb{R}becomes ℝ).\mathcal{content}: Converts ASCII letters inside{content}into calligraphic / script characters (e.g.,\mathcal{L}becomes ℒ).
-
Positioning:
- Over and Under:
\over{top}{main}and\under{bottom}{main}. This will place the first argument directly over or under the second argument. - Scaling and Rotation:
\scale{factor}{content}: Scales the content by a factor (uses CSS zoom).\scalew{factor}{content}: Scales only the width.\scaleh{factor}{content}: Scales only the height.\rotate{degrees}{content}: Rotates the content.
- Attaching:
\attach{main}{tr}{br}{tl}{bl}{up}{down}: Attaches the scripts to the main element. The first argument is the main element, and the rest are the scripts. The order is top-right, bottom-right, top-left, bottom-left, up, down. If you don't want to use a script, just leave it empty.
Example: Suppose i wanted to attach text both above and below a line, I would do this:<m-eq>\attach{AB}{}{}{}{}{Above}{Below}</m-eq>
\attacho{main}{tr}{br}{tl}{bl}{up}{down}{overlap}: Same as above, but with an overlap parameter. This will pull the scripts closer to the main element. The overlap is in ems, so0.3emis a good value, that the attach function uses by default.\attachos{main}{tr}{br}{tl}{bl}{up}{down}{overlap}{scale}: Same as above, but with both an overlap parameter and a custom scale for the scripts. The scale allows you to change the size of the attached elements. By default, attach uses a scale of0.6, but you can change it to whatever you want with this.
(attachos is fun to say)
- Over and Under:
-
Symbols
To use these symbols, just write\command, where the command is the command of the symbol as below, or\alias, if the command has an alias.Symbol Command Alias α alphaaβ betabγ gammagδ deltadε epsiloneζ zetazη etahθ thetathι iotaiκ kappakλ lambdalμ mumν nunξ xixο omicronoπ pipρ rhorσ sigmasτ tautυ upsilonuφ phifχ chicψ psipsω omega-
Symbol Command Alias Α AlphaAΒ BetaBΓ GammaGΔ DeltaDΕ EpsilonEΖ ZetaZΗ EtaHΘ ThetaThΙ IotaIΚ KappaKΛ LambdaLΜ MuMΝ NuNΞ XiXΟ OmicronOΠ PiPΡ RhoRΣ SigmaS,sumbΤ TauTΥ UpsilonUΦ PhiFΧ ChiCΨ PsiPsΩ Omega-
Symbol Command Σ sumΣ sumb∏ prod∏ prodb∫ int∫ intb∬ iint∭ iiint∮ oint√ sqrtsumcontains a hidden zero-width character (U+200) at the end of the key, so the parser distinguishes it fromSigma/S/sumbeven though both render asΣ. Same withprodandint.
These render inline versions of the symbols when paired with ^ and _ (ie, the superscript and subscript are on sides), whereas thesumb,prodb,intbhave the subscript and superscript on top and below them.
Symbol Command Alias(es) ∪ cup- ∪ unionbigcup∩ cap- ∩ intersectbigcap∈ in- ∉ notin- ⊂ subset- ⊆ subseteq- ⊃ supset- ⊇ supseteq- cupandcapalso contain the hidden zero-width character, and act the same as sum.
Symbol Command Alias(es) = eqequal,equals< lt- > gt- ≤ leq- ≥ geq- ≠ neq- ≈ approx- ≅ cong- ≡ equiv- ∼ sim- ≃ simeq- ≢ nequiv- ≇ ncong- ≁ nsim- ≄ nsimeq- ≉ napprox- ∝ proppropto
Symbol Command Alias(es) ∀ forall- ∃ exists- ∴ therefore- ∵ becausesince
Symbol Command Alias(es) × timescross÷ div- ± pm- ∓ mp- ⋅ cdot-
Symbol Command Alias(es) ∞ infty- ⌊ lfloor- ⌋ rfloor- ⌈ lceil- ⌉ rceil- ⟨ langle- ⟩ rangle-
Symbol Command Alias(es) → rightarrowto← leftarrow- ↑ uparrow- ↓ downarrow- ↔ leftrightarrow- ⇌ rightleftarrows- ↦ mapsto- ⇒ impliesRightarrow⇔ iffequivalent,LeftRightarrow⇐ Leftarrow- ⇒ Rightarrow- ⇑ Uparrow- ⇓ Downarrow- ⇔ LeftRightarrow- ⟶ Longrightarrow- ⟵ Longleftarrow- ⟹ LongRightarrow- ⟸ LongLeftarrow- ⟷ Longleftrightarrow- ⟼ Longmapsto- ⟺ LongEquiv- More kinds of arrows are supported, to get a full list, go to Arrow documentation
These have more meaning to the parser, but just escaping them will print the symbol itself.
Symbol Command Purpose ^^Literal caret (escapes superscript trigger) __Literal underscore (escapes subscript trigger) \\Literal backslash
- You need not use text in your equations, you can use any other element as well! For instance, images! Most of the commands should be able to adapt to the sizes of the images.
Example usageThe sqrt symbol will go over the image in this case.<m-eq>\sqrt{<img src="./testimg.png">}_2^3</m-eq>
You can use the function mathStrToHtml. It takes in a string, and it will output a HTMLDIVELEMENT that has the converted version of the string.