All Projects → LingDong- → hfmath

LingDong- / hfmath

Licence: other
Render LaTeX math with Hershey Fonts

Programming Languages

typescript
32286 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to hfmath

React Katex
Display math in TeX with KaTeX and ReactJS
Stars: ✭ 345 (+353.95%)
Mutual labels:  latex, math
Mathview
One stop for all Math formula and Science equation rendering in Android
Stars: ✭ 71 (-6.58%)
Mutual labels:  latex, math
Fiduswriter
Fidus Writer is an online collaborative editor for academics.
Stars: ✭ 405 (+432.89%)
Mutual labels:  latex, math
Calculatex
in progress pretty printing calculator language
Stars: ✭ 302 (+297.37%)
Mutual labels:  latex, math
Jekyll Spaceship
🚀 A Jekyll plugin to provide powerful supports for table, mathjax, plantuml, mermaid, emoji, video, audio, youtube, vimeo, dailymotion, soundcloud, spotify, etc.
Stars: ✭ 196 (+157.89%)
Mutual labels:  latex, math
Wpf Math
.NET library for rendering mathematical formulae using the LaTeX typsetting style, for the WPF framework
Stars: ✭ 339 (+346.05%)
Mutual labels:  latex, math
Algebra Latex
Parse and calculate latex formatted math
Stars: ✭ 20 (-73.68%)
Mutual labels:  latex, math
Nerdamer
a symbolic math expression evaluator for javascript
Stars: ✭ 322 (+323.68%)
Mutual labels:  latex, math
Texme
Self-rendering Markdown + LaTeX documents
Stars: ✭ 1,970 (+2492.11%)
Mutual labels:  latex, math
Remark Math
remark and rehype plugins to support math
Stars: ✭ 129 (+69.74%)
Mutual labels:  latex, math
Angourimath
Open-source symbolic algebra library for C# and F#. One of the most powerful in .NET
Stars: ✭ 266 (+250%)
Mutual labels:  latex, math
Katex
Fast math typesetting for the web.
Stars: ✭ 14,623 (+19140.79%)
Mutual labels:  latex, math
Mdmath
LaTeX Math for Markdown inside of Visual Studio Code.
Stars: ✭ 675 (+788.16%)
Mutual labels:  latex, math
Mathbin
Math pastebin with LaTeX and Markdown support
Stars: ✭ 83 (+9.21%)
Mutual labels:  latex, math
Csharpmath
LaTeX. in C#. (ported from the wonderful iosMath project).
Stars: ✭ 205 (+169.74%)
Mutual labels:  latex, math
Upmath.me
Markdown and LaTeX online editor - create text for web with equations and diagrams
Stars: ✭ 234 (+207.89%)
Mutual labels:  latex, math
phplatex
Inline TeX in PHP pages
Stars: ✭ 34 (-55.26%)
Mutual labels:  latex
spatialmath-matlab
Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
Stars: ✭ 142 (+86.84%)
Mutual labels:  math
Numbase
Arbitrary number base converter.
Stars: ✭ 22 (-71.05%)
Mutual labels:  math
thesisthemeCSU
A template for the thesis of CSU (Central South University).
Stars: ✭ 13 (-82.89%)
Mutual labels:  latex

hfmath

Render LaTeX math with Hershey Fonts.

Download | Try Online | API

hfmath is a tiny JS tool for rendering mathematics using single-line Hershey Fonts. ~140 KB script with no dependencies: LaTeX goes in, SVG paths comes out. No CSS. No font loading. Lightning fast. Works for browsers and servers. Customizable export for polyline data, SVG or PDF. Display math in your blog. Draw your homework with a plotter. Engrave your favorite equation on a plate. Animate.

For example:

new hfmath(
  `f(x)=
    \\frac{1}{2\\sqrt{2\pi} }
    e^{-\\frac{1}{2}(\\frac{x-\\mu}{\\sigma})^2}`
).svg();

Yields an SVG containing a single <path/> element that looks something like this:

Give it some animation if you're feeling cheerful:

Supports nested fractions, nested square roots, and other gnarly math you might have:

As well as integrals, vectors, matrices, and aligned equations, just to name a few:

\mathbf \mathfrac etc. are mapped to the variety of fonts in Hershey:

Check out more samples here.

For a list of supported LaTeX commands and symbols, check out ./src/symbols.ts (There're about 200 of them). Note that the original Hershey font does not cover all math symbols available in LaTeX. Check out ./tools/sheet.svg for the full Hershey set. If you would like to have a new symbol manually added to hfmath, please feel free to open an Issue/PR!

hfmath is written from scratch, and probably does not match LaTeX visuals and behaviors perfectly (nor is it the goal). However, if something looks buggy or ugly, feel free to let me know in an Issue.

Usage

Install:

npm i hfmath

or

<script src="https://unpkg.com/hfmath@latest"></script>

Basics: render an equation and write SVG:

//Import
const {hfmath} = require('./hfmath');

//Typeset a new equation
let eq = new hfmath("x^2 + 2x + 1");

//Write SVG
let svgStr = eq.svg();
fs.writeFileSync('out.svg',svgStr);

For the browser

<script src="hfmath.global.js"></script>
<script>
let eq = new HFMATH.hfmath("x^2 + 2x + 1");
let svgStr = eq.svg();
document.write(svgStr);
</script>

You can also obtain the raw polylines data:

let eq = new hfmath("x^2 + 2x + 1");

let plines = eq.polylines();

console.log(polylines)
// [[[x0,y0],[x1,y1],...],...]

Or you can have it as the d attribute of an SVG <path/> element:

let d = eq.pathd()

console.log(d)
//"M x y L x y ..."

Other possibilities include:

// pdf
let pdfStr = eq.pdf();
fs.writeFileSync('out.pdf',pdfStr);

// bounding box for each symbol
let boxes = eq.boxes();
console.log(boxes)
//[{x,y,w,h},...]

Note that internally, hfmath uses the unit of ex, (approximately the height of a lower case "x"). The export functions have default settings to upscale by 16. You can fine-tune it by passing optional arguments to any export function, like so:

let eq = new hfmath("x^2 + 2x + 1");
eq.svg({
  SCALE_X:32,
  SCALE_Y:32,
  MARGIN_X:16,
  MARGIN_Y:16,
  BG_COLOR:'antiquewhite',
});

All available options are:

interface ExportOpt{
  MIN_CHAR_H?:number;
  MAX_W?:number;
  MAX_H?:number;
  MARGIN_X?:number;
  MARGIN_Y?:number;
  SCALE_X?:number;
  SCALE_Y?:number;
  STROKE_W?:number;
  FG_COLOR?:string;
  BG_COLOR?:string;
}

Advanced

You can tweak typesetting parameters such as line spacing, factor to downscale subscripts and superscripts, by modifying the CONFIG object. For example:

let {CONFIG,hfmath} = require('./hfmath');

// make subscripts appear smaller
CONFIG.SUB_SUP_SCALE = 0.5;

// new setting now in place:
let eq = new hfmath("x^2");

All available options:

let CONFIG : Record<string,number> = {
  SUB_SUP_SCALE:0.75,
  SQRT_MAG_SCALE:0.5,
  FRAC_SCALE:0.85,
  LINE_SPACING:0.5,
  FRAC_SPACING:0.4,
}

Algorithm

Upon given a LaTeX expression, hfmath first parses into tokens then into a syntax tree, and generate nested bounding boxes for each node:

Next, it flattens bounding boxes into an array of instructions for exactly where and at what size to draw each symbol:

Finally it transforms the Hershey font coordinates to draw the symbols:

The bounding boxes can be quite cool to look at for more complicated equations:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].