{"id":26761850,"url":"https://github.com/sinhaparth5/mathsnap","last_synced_at":"2026-03-13T22:03:03.318Z","repository":{"id":284582627,"uuid":"955401782","full_name":"sinhaparth5/mathsnap","owner":"sinhaparth5","description":"The goal is to provide a reusable component that abstracts away KaTeX configuration and makes it easy to use ","archived":false,"fork":false,"pushed_at":"2025-03-29T15:53:29.000Z","size":225,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-04T05:21:12.015Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sinhaparth5.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-26T15:25:14.000Z","updated_at":"2025-03-29T15:53:33.000Z","dependencies_parsed_at":"2025-03-26T16:39:39.695Z","dependency_job_id":"7aca12f3-69a5-43ba-bb79-5a0864fed2a8","html_url":"https://github.com/sinhaparth5/mathsnap","commit_stats":null,"previous_names":["sinhaparth5/mathsnap"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sinhaparth5/mathsnap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinhaparth5%2Fmathsnap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinhaparth5%2Fmathsnap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinhaparth5%2Fmathsnap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinhaparth5%2Fmathsnap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sinhaparth5","download_url":"https://codeload.github.com/sinhaparth5/mathsnap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinhaparth5%2Fmathsnap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30475659,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-13T20:45:58.186Z","status":"ssl_error","status_checked_at":"2026-03-13T20:45:20.133Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-28T18:33:03.253Z","updated_at":"2026-03-13T22:03:03.312Z","avatar_url":"https://github.com/sinhaparth5.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MathSnap\n\n[![npm version](https://img.shields.io/npm/v/mathsnap.svg)](https://www.npmjs.com/package/mathsnap)\n[![license](https://img.shields.io/npm/l/mathsnap.svg)](https://github.com/sinhaparth5/mathsnap/blob/master/LICENSE)\n[![test](https://github.com/sinhaparth5/mathsnap/actions/workflows/release.yml/badge.svg)](https://github.com/sinhaparth5/mathsnap/actions)\n\nA lightweight, framework-agnostic wrapper around [KaTeX](https://katex.org/) that makes rendering LaTeX math equations simple. Works with **React**, **Vue**, **Svelte**, and **vanilla JS/TS** — ships with its own optimised CSS, no KaTeX install needed.\n\n## Features\n\n- Render LaTeX equations with a single import\n- Components for React (`.jsx` / `.tsx`), Vue (`.vue`), and Svelte (`.svelte`)\n- Ships with `mathsnap.min.css` — 23 KB instead of the 1.4 MB you'd get from inlining KaTeX fonts\n- Responsive by default (`max-width: 100%`, `overflow-x: auto`)\n- Built-in error handling — renders an error message instead of crashing\n- Full TypeScript support\n- Tree-shakeable\n\n## Installation\n\n```bash\nnpm install mathsnap\n# or\nyarn add mathsnap\n# or\npnpm add mathsnap\n```\n\n\u003e KaTeX is a direct dependency — no separate install needed.\n\n## CSS\n\nAdd **one** import at the top of your app's entry file:\n\n```ts\nimport 'mathsnap/mathsnap.min.css';\n// or the alias:\nimport 'mathsnap/style';\n```\n\nThis loads the 23 KB KaTeX stylesheet (fonts are separate files loaded on demand by the browser). Do this **once** — you don't need it in every component file.\n\n---\n\n## Usage\n\n### React (`.jsx` / `.tsx`)\n\n```tsx\nimport { MathEquation, equations } from 'mathsnap/react';\n\nexport default function App() {\n  return (\n    \u003c\u003e\n      {/* Block / display mode */}\n      \u003cMathEquation equation={equations.quadratic} displayMode /\u003e\n\n      {/* Inline mode */}\n      \u003cp\u003e\n        Einstein proved that \u003cMathEquation equation={equations.einstein} /\u003e relates\n        mass and energy.\n      \u003c/p\u003e\n\n      {/* Custom equation */}\n      \u003cMathEquation\n        equation=\"f(x) = \\int_{-\\infty}^{\\infty} \\hat{f}(\\xi)\\,e^{2\\pi i \\xi x}\\,d\\xi\"\n        displayMode\n        className=\"my-equation\"\n        style={{ fontSize: '1.2rem' }}\n        onError={(err) =\u003e console.error(err)}\n      /\u003e\n    \u003c/\u003e\n  );\n}\n```\n\nYou can also import directly from the main entry:\n\n```tsx\nimport { ReactMathEquation, equations } from 'mathsnap';\n\n\u003cReactMathEquation equation={equations.euler} displayMode /\u003e\n```\n\n---\n\n### Vue (`.vue`)\n\n```vue\n\u003ctemplate\u003e\n  \u003c!-- Block / display mode --\u003e\n  \u003cMathEquation :equation=\"equations.circleArea\" :display-mode=\"true\" /\u003e\n\n  \u003c!-- Inline mode --\u003e\n  \u003cp\u003e\n    The formula \u003cMathEquation :equation=\"equations.pythagorean\" /\u003e is the\n    Pythagorean theorem.\n  \u003c/p\u003e\n\n  \u003c!-- Error handling via event --\u003e\n  \u003cMathEquation\n    equation=\"\\nabla \\times \\vec{B} = \\mu_0 \\vec{J}\"\n    :display-mode=\"true\"\n    @error=\"handleError\"\n  /\u003e\n\u003c/template\u003e\n\n\u003cscript setup lang=\"ts\"\u003e\nimport MathEquation from 'mathsnap/vue';\nimport { equations } from 'mathsnap';\n\nfunction handleError(err: Error) {\n  console.error('Render error:', err);\n}\n\u003c/script\u003e\n```\n\n---\n\n### Svelte (`.svelte`)\n\n```svelte\n\u003cscript lang=\"ts\"\u003e\n  import MathEquation from 'mathsnap/svelte';\n  import { equations } from 'mathsnap';\n\u003c/script\u003e\n\n\u003c!-- Block / display mode --\u003e\n\u003cMathEquation equation={equations.schrodinger} displayMode={true} /\u003e\n\n\u003c!-- Inline mode --\u003e\n\u003cp\u003e\n  Euler's identity \u003cMathEquation equation={equations.euler} /\u003e is considered\n  the most beautiful equation in mathematics.\n\u003c/p\u003e\n\n\u003c!-- Error handling --\u003e\n\u003cMathEquation\n  equation=\"\\frac{d}{dx} e^x = e^x\"\n  displayMode={true}\n  onError={(err) =\u003e console.error(err)}\n/\u003e\n```\n\n---\n\n### Vanilla JS / TS (`.js` / `.ts`)\n\n```ts\nimport { renderMath, equations } from 'mathsnap';\n\nconst container = document.getElementById('math')!;\n\nconst { html, error } = renderMath({\n  equation: equations.normalDistribution,\n  displayMode: true,\n});\n\nif (!error.hasError) {\n  container.innerHTML = html;\n}\n```\n\n---\n\n## Predefined Equations\n\n```ts\nimport { equations } from 'mathsnap';\n\nequations.quadratic          // Quadratic formula\nequations.einstein           // E = mc²\nequations.pythagorean        // a² + b² = c²\nequations.euler              // Euler's identity  e^{iπ} + 1 = 0\nequations.circleArea         // Area of a circle\nequations.normalDistribution // Normal distribution\nequations.derivative         // Derivative definition\nequations.integral           // Integral definition (FTC)\nequations.maxwellDivergenceE // Maxwell's equations (∇·E)\nequations.schrodinger        // Schrödinger equation\n```\n\n---\n\n## API\n\n### `renderMath(options)`\n\nRenders a LaTeX string to an HTML string using KaTeX. Returns `{ html, error }`.\n\n```ts\nimport { renderMath } from 'mathsnap';\n\nconst { html, error } = renderMath({\n  equation: '\\\\sqrt{a^2 + b^2}',\n  displayMode: true,\n  katexOptions: {\n    macros: { '\\\\RR': '\\\\mathbb{R}' },\n  },\n  onError: (err) =\u003e console.error(err),\n});\n\nif (!error.hasError) {\n  document.getElementById('math')!.innerHTML = html;\n}\n```\n\n### `isValidEquation(equation)`\n\nReturns `true` if the string is valid LaTeX, `false` otherwise.\n\n```ts\nimport { isValidEquation } from 'mathsnap';\n\nisValidEquation('x^2 + y^2 = z^2'); // true\nisValidEquation('\\\\badinvalid{');    // false\n```\n\n### `sanitizeEquation(equation)`\n\nStrips HTML/script tags from a string before rendering. Called automatically by all components.\n\n```ts\nimport { sanitizeEquation } from 'mathsnap';\n\nsanitizeEquation('\u003cscript\u003ealert(1)\u003c/script\u003ex^2'); // 'x^2'\n```\n\n---\n\n## Component Props\n\nAll framework components share the same props:\n\n| Prop | Type | Default | Description |\n|------|------|---------|-------------|\n| `equation` | `string` | **required** | LaTeX equation string |\n| `displayMode` | `boolean` | `false` | Block/centred mode (`true`) vs inline (`false`) |\n| `className` | `string` | `''` | Extra CSS class on the container element |\n| `style` | `object` / `string` | `{}` / `''` | Inline styles — object for React/Vue, string for Svelte |\n| `as` | `'div' \\| 'span' \\| 'p'` | auto | Override the container element |\n| `katexOptions` | `KatexOptions` | `{}` | Extra [KaTeX options](https://katex.org/docs/options) |\n| `onError` | `(error: Error) =\u003e void` | — | Called when KaTeX rendering fails |\n\n\u003e Vue additionally emits an `error` event — use `@error=\"handler\"` in the template.\n\n---\n\n## Advanced\n\n### Custom KaTeX macros\n\n```tsx\n\u003cMathEquation\n  equation=\"\\RR \\to \\CC\"\n  katexOptions={{\n    macros: {\n      '\\\\RR': '\\\\mathbb{R}',\n      '\\\\CC': '\\\\mathbb{C}',\n    },\n  }}\n/\u003e\n```\n\n### Error handling\n\nWhen rendering fails, the component renders an inline error span styled in red rather than throwing. You can also receive the error via `onError`:\n\n```tsx\n\u003cMathEquation\n  equation=\"\\invalid{\"\n  onError={(err) =\u003e console.error('Math error:', err.message)}\n/\u003e\n```\n\n---\n\n## Contributing\n\nPRs are welcome. Please open an issue first for larger changes.\n\n## License\n\nMIT © [Parth Sinha](https://github.com/sinhaparth5)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinhaparth5%2Fmathsnap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsinhaparth5%2Fmathsnap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinhaparth5%2Fmathsnap/lists"}