![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
react-hook-mathjax
Advanced tools
Lightweight package using MathJax to allow inline rendering of latex as svg elements
Lightweight, strongly typed package with almost no dependencies. It uses MathJax v3 and React Hooks to allow inline conversion of a LaTeX string into an svg. For more details, take a look at the example project (which parses user input and handles error states) or the live demo of the example project.
yarn add react-hook-mathjax
import React from "react";
import Tex2SVG from "react-hook-mathjax";
function App() {
return (
<div className="App">
<header className="App-header">
<Tex2SVG display="inline" latex="e^{i \pi} + 1 = 0" />
</header>
</div>
);
}
export default App;
import React from "react";
import Tex2SVG, { MathJaxProvider } from "react-hook-mathjax";
// This object contains the default options, more info at:
// http://docs.mathjax.org/en/latest/options/output/svg.html
const mathJaxOptions = {
svg: {
scale: 1, // global scaling factor for all expressions
minScale: .5, // smallest scaling factor to use
mtextInheritFont: false, // true to make mtext elements use surrounding font
merrorInheritFont: true, // true to make merror text use surrounding font
mathmlSpacing: false, // true for MathML spacing rules, false for TeX rules
skipAttributes: {}, // RFDa and other attributes NOT to copy to the output
exFactor: .5, // default size of ex in em units
displayAlign: 'center', // default for indentalign when set to 'auto'
displayIndent: '0', // default for indentshift when set to 'auto'
fontCache: 'local', // or 'global' or 'none'
localID: null, // ID to use for local font cache (for single equation processing)
internalSpeechTitles: true, // insert <title> tags with speech content
titleID: 0 // initial id number to use for aria-labeledby titles
}
}
function App() {
return (
<MathJaxProvider options={mathJaxOptions}>
<div className="App">
<header className="App-header">
<Tex2SVG display="inline" latex="e^{i \pi} + 1 = 0" />
</header>
</div>
</MathJaxProvider>
);
}
export default App;
import React from "react";
import Tex2SVG from "react-hook-mathjax";
function App() {
const [inputValue, setInputValue] = React.useState(
"G_{\\mu\\nu} + \\Lambda g_{\\mu\\nu} = \\kappa T_{\\mu\\nu}",
);
return (
<div className="App">
<header className="App-header">
<h3>React Hook MathJax</h3>
<input
type="text"
defaultValue={inputValue}
onChange={e => setInputValue(e.target.value)}
/>
<div className="tex-container">
<Tex2SVG class="tex" tabindex={-1} latex={inputValue} />
</div>
</header>
</div>
);
}
export default App;
import React from "react";
import Tex2SVG from "react-hook-mathjax";
const getErrorFromHTML = (html) =>
html.children[1].firstChild.firstChild.attributes["data-mjx-error"].value;
function App() {
const [inputValue, setInputValue] = React.useState(
"G_{\\mu\\nu} + \\Lambda g_{\\mu\\nu} = \\kappa T_{\\mu\\nu}",
);
const [lastValidInput, setLastValidInput] = React.useState("");
const [error, setError] = React.useState(null);
const hasError = error !== null;
return (
<div className="App">
<header className="App-header">
<h3>React Hook MathJax</h3>
<input
className={`${hasError ? "error" : ""}`}
type="text"
defaultValue={inputValue}
onChange={e => {
setInputValue(e.target.value);
setError(null);
}}
/>
<div className="tex-container">
<Tex2SVG
class="tex"
tabindex={-1}
latex={hasError ? lastValidInput : inputValue}
onSuccess={() =>
setLastValidInput(hasError ? lastValidInput : inputValue)
}
onError={html => setError(getErrorFromHTML(html))}
/>
</div>
{hasError && <div>hint: {error}</div>}
</header>
</div>
);
}
export default App;
MathJaxProvider
props:options
Object, optionalTex2SVG
props:latex
string, requiredsvg
''
onSuccess
(HTMLElement) => void, optionalsvg
- it receives the html object generated by MathJax(html: HTMLElement) => {}
onError
(HTMLElement) => void, optionalsvg
- it receives the html object generated by MathJax(html: HTMLElement) => {}
Other html attributes
{[key: string]: any} optionalclass
- not className
etcFAQs
Lightweight package using MathJax to allow inline rendering of latex as svg elements
The npm package react-hook-mathjax receives a total of 433 weekly downloads. As such, react-hook-mathjax popularity was classified as not popular.
We found that react-hook-mathjax demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.