在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):talyssonoc/react-katex开源软件地址(OpenSource Url):https://github.com/talyssonoc/react-katex开源编程语言(OpenSource Language):JavaScript 98.5%开源软件介绍(OpenSource Introduction):react-katex
Installation $ npm install react-katex
# or
$ yarn add react-katex Usageimport 'katex/dist/katex.min.css';
import { InlineMath, BlockMath } from 'react-katex'; InlineMathDisplay 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: BlockMathDisplay 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: Note: import 'katex/dist/katex.min.css'; Error handlingDefault error messageBy default the error rendering is handled by KaTeX. You can optionally pass 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: Custom error messageIt's possible to handle parse errors using the prop 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 Escaping expressionsIn addition to using the 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 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')); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论