All Projects → MatejBransky → react-katex

MatejBransky / react-katex

Licence: MIT license
Display math in TeX with KaTeX and ReactJS

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to react-katex

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 (+45.19%)
Mutual labels:  math, katex
showdown-katex
Math typesetting for showdown
Stars: ✭ 32 (-76.3%)
Mutual labels:  math, katex
augmath
Interactive Computer Algebra System. Augmenting how we *do* mathematics using computers
Stars: ✭ 41 (-69.63%)
Mutual labels:  math, katex
Mdmath
LaTeX Math for Markdown inside of Visual Studio Code.
Stars: ✭ 675 (+400%)
Mutual labels:  math, katex
Prosemirror Math
Schema and plugins for "first-class" math support in ProseMirror!
Stars: ✭ 43 (-68.15%)
Mutual labels:  math, katex
Dominhhai.github.io
My Blog
Stars: ✭ 8 (-94.07%)
Mutual labels:  math, katex
React Katex
Display math in TeX with KaTeX and ReactJS
Stars: ✭ 345 (+155.56%)
Mutual labels:  math, katex
Remark Math
remark and rehype plugins to support math
Stars: ✭ 129 (-4.44%)
Mutual labels:  math, katex
Katex
Fast math typesetting for the web.
Stars: ✭ 14,623 (+10731.85%)
Mutual labels:  math, katex
speedy-math
An application which allows user (small kids) to practice basic Mathematics operations
Stars: ✭ 28 (-79.26%)
Mutual labels:  math
alokmenghrajani.github.com
Alok Menghrajani's Blog
Stars: ✭ 64 (-52.59%)
Mutual labels:  math
Math
考研数学,数学一,包括高等数学、线性代数、概率统计
Stars: ✭ 300 (+122.22%)
Mutual labels:  math
d rive
c++17 compile time math(derivation/integration)
Stars: ✭ 16 (-88.15%)
Mutual labels:  math
Upsurge
Multi-dimensional Swift math
Stars: ✭ 180 (+33.33%)
Mutual labels:  math
mml-book-chinese
mml-book-chinese《Mathematics For Machine Learning》机器学习中的数学 中文版
Stars: ✭ 113 (-16.3%)
Mutual labels:  math
bewl
A DSL for the internal language of a topos
Stars: ✭ 41 (-69.63%)
Mutual labels:  math
notes
Simple text editor for your Markdown and LaTeX notes.
Stars: ✭ 24 (-82.22%)
Mutual labels:  katex
good-reads
List of inspiring articles, blogs, tutorials and books. Tech stuff.
Stars: ✭ 14 (-89.63%)
Mutual labels:  math
class
Rust library for building IQC: cryptography based on class groups of imaginary quadratic orders
Stars: ✭ 54 (-60%)
Mutual labels:  math
VSCode-LaTeX-Inkscape
✍️ A way to integrate LaTeX, VS Code, and Inkscape in macOS
Stars: ✭ 62 (-54.07%)
Mutual labels:  math

react-katex


Display math expressions with KaTeX and React.

Examples

(based on the https://github.com/talyssonoc/react-katex)
(the readme and the demo are "forked" from https://github.com/talyssonoc/react-katex)


Comparison with react-katex: talyssonoc/react-katex#49.

npm type definitions

Installation

  $ npm install katex @matejmazur/react-katex
  # or
  $ yarn add katex @matejmazur/react-katex

Usage

import 'katex/dist/katex.min.css';
import TeX from '@matejmazur/react-katex';

Inline math

Display math in the middle of the text.

ReactDOM.render(
  <TeX math="\int_0^\infty x^2 dx" />,
  document.getElementById('math')
);

// or

ReactDOM.render(
  <TeX>\int_0^\infty x^2 dx</TeX>,
  document.getElementById('math')
);

It will be rendered like this:

Inline math

Block math

Display math in a separated block, with larger font and symbols.

ReactDOM.render(
  <TeX math="\int_0^\infty x^2 dx" block />,
  document.getElementById('math')
);

// or

ReactDOM.render(
  <TeX block>\int_0^\infty x^2 dx</TeX>,
  document.getElementById('math')
);

It will be rendered like this:

Block math

Note:
Don't forget to import KaTeX CSS file.

import 'katex/dist/katex.min.css';

Error handling

Default error message

By default the error rendering is handled by KaTeX. You can optionally pass errorColor (defaults to #cc0000) as a prop:

ReactDOM.render(
  <TeX math={'\\int_0^\\infty x^2 dx \\inta'} errorColor={'#cc0000'} />,
  document.getElementById('math')
);

This will be rendered like so:

KaTeX error

Custom error message

It's possible to handle parse errors using the prop renderError. This prop must be a function that receives the error object and returns what should be rendered when parsing fails:

ReactDOM.render(
  <TeX
    math="\\int_{"
    renderError={(error) => {
      return <b>Fail: {error.name}</b>;
    }}
  />,
  document.getElementById('math')
);

// The code above will render '<b>Fail: ParseError</b>' because it's the value returned from `renderError`.

This will render <b>Fail: ParseError</b>:

renderError

Custom wrapper component

You can change the wrapper component (block math uses div and inline uses span) by whatever you want via props.as attribute.

ReactDOM.render(
  <TeX
    math="y = x^2"
    as="figcaption"
  />,
  document.getElementById('math')
);

Escaping expressions

In addition to using the math property, you can also quote as a child allowing the use of { } in your expression.

ReactDOM.render(
  <TeX>{'\\frac{\\text{m}}{\\text{s}^2}'}</TeX>,
  document.getElementById('math')
);

Or Multiline

ReactDOM.render(
  <TeX>{`\\frac{\\text{m}}
{\\text{s}^2}`}</TeX>,
  document.getElementById('math')
);

However, it can be annoying to escape backslashes. This can be circumvented with the String.raw tag on a template literal when using ES6.

ReactDOM.render(
  <TeX>{String.raw`\frac{\text{m}}{\text{s}^2}`}</TeX>,
  document.getElementById('math')
);

Backticks must be escaped with a backslash but would be passed to KaTeX as \`. A tag can be created to replace \` with `

const latex = (...a) => String.raw(...a).replace('\\`', '`');
ReactDOM.render(<TeX>{latex`\``}</TeX>, document.getElementById('math'));

You can even do variable substitution

const top = 'm';
const bottom = 's';
ReactDOM.render(
  <TeX>{String.raw`\frac{\text{${top}}}{\text{${bottom}}^2}`}</TeX>,
  document.getElementById('math')
);

Configuring KaTeX

You can change directly all KaTeX options via props.settings:

Example of adding a custom macro:

ReactDOM.render(
  <TeX settings={{ macros: { '*': '\\cdot' } }}>y = k * x + c</TeX>
);

Result:

macros

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].