Display math in TeX with KaTeX and ReactJS
Branch: master
Clone or download
Latest commit 3ae5cbd Apr 9, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist Update related to new version (#38) Mar 21, 2018
docs Moving images used in README into docs dir Feb 9, 2018
example Bump version Apr 9, 2018
src Requested change for error catching Mar 21, 2018
test Removed displayMode from test case Mar 21, 2018
.babelrc Use Rollup instead of Webpack (#18) Jun 27, 2017
.editorconfig First commit May 5, 2015
.eslintrc Update dependencies and lock file (#20) Jun 27, 2017
.gitignore Updating yarn.lock Mar 21, 2018
.npmignore Update and modernize tooling (#3) Oct 13, 2016
.travis.yml Older yarn version incompatible with node v4 Mar 4, 2018
README.md Update related to new version (#38) Mar 21, 2018
package.json Bump version Apr 9, 2018
rollup.config.js
yarn.lock Updating yarn.lock Mar 21, 2018

README.md

react-katex

Build Status Code Climate Coverage Status

Display math expressions with KaTeX and React

Installation

  $ npm install react-katex
  # or
  $ yarn add react-katex

Usage

import 'katex/dist/katex.min.css';
import { InlineMath, BlockMath } from 'react-katex';

InlineMath

Display math in the middle of the text.

  var InlineMath = ReactKaTeX.InlineMath;

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

  // or

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

It will be rendered like this:

Inline math

BlockMath

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

  var BlockMath = ReactKaTeX.BlockMath;

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

  // or

  ReactDOM.render(<BlockMath>\int_0^\infty x^2 dx</BlockMath>,
                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:

var BlockMath = ReactKaTeX.BlockMath;

ReactDOM.render(
  <BlockMath
    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:

var BlockMath = ReactKaTeX.BlockMath;
  
ReactDOM.render(
  <BlockMath
    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

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(<BlockMath>{"\\frac{\\text{m}}{\\text{s}^2}"}</BlockMath>,
                document.getElementById('math'));

Or Multiline

ReactDOM.render(<BlockMath>{`\\frac{\\text{m}}
{\\text{s}^2}`}</BlockMath>,
                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(<BlockMath>{String.raw`\frac{\text{m}}{\text{s}^2}`}</BlockMath>,
                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(<BlockMath>{latex`\``}</BlockMath>,
                document.getElementById('math'));

You can even do variable substitution

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