
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
mathlifier
Advanced tools
A wrapper for KaTeX renderToString
for inline and displayed math
Using KaTeX with dynamic/reactive mathematical content, or
with server side rendering, typically mean calling
katex.renderToString()
many times.
Mathlifier repackages these function calls as math()
and display()
, along with
3 opinions:
While KaTeX sets throwOnError to
true
by default, we have opted to set it to false
We think that this facilitates quicker debugging (especially useful when hot module reloading (HMR) is active).
By default, we wrap all inputs with braces to prevent automatic line-breaking.
Disable this with an option
By default, we place displayed math inside a container styled with
overflow-x: auto
. We believe this modification makes the output more mobile-friendly.
Disable this with an option
npm i mathlifier
// import functions
import { math, display } from 'mathlifier';
// example of using these functions
const inlineMath = math('ax^2+bx+c=0');
const displayedMath = display('x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}');
Subsequently, attach the HTML string(s) generated to the DOM.
Just like in KaTeX, we will need to add a stylesheet. Refer to the KaTeX Documentation for more details, or add the following into the head element.
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.css"
integrity="sha384-vKruj+a13U8yHIkAyGgK1J3ArTLzrFGBbBc0tDp4ad/EyewESeXE/Iv67Aj8gKZ0"
crossorigin="anonymous"
/>
We can disable the default behavior (opinions 2 and 3 above) of Mathlifier vs regular KaTeX
// example of Mathlifier options
const allowBreak = math('e^{i\\pi} = -1', { wrap: true });
const noContainer = display('\\sum_{r=1}^n = \\frac{n(n+1)}{2}', { overflowAuto: false });
All KaTeX options are passed along.
// example of KaTeX options
const leftEqn = display('\\begin{equation} A = \\pi r^2 \\end{equation}', {
leqno: true,
fleqn: true,
});
We also have quick wrappers for four of the commonly used display environments: align
, align*
, gather
, gather*
.
// display environments
import { align, alignStar, gather, gatherStar } from 'mathlifier';
const gatherEnv = alignStar(`
x+3y &= 3 \\\\
2x-y &= -2
`);
// equivalent to
// display(`\\begin{align*}
// x+3y &= 3 \\\\
// 2x-y &= -2
// \\end{align*}
// `);
We have also added linebreak
(to add the html <br>
tag) and the function bold(x)
(to wrap x
in the <strong></strong>
environment).
This is to facilitate using the same javascript code to generate both HTML (via KaTeX) and LaTeX by swapping out this library for an upcoming package (Mathlifier2?).
FAQs
> [!IMPORTANT] Breaking change in v2: we now use Temml instead of KaTeX for > rendering
The npm package mathlifier receives a total of 70 weekly downloads. As such, mathlifier popularity was classified as not popular.
We found that mathlifier demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.