Vue KaTeX Plugin
vue-katex is a lightweight Vue plugin designed to integrate KaTeX, a fast math typesetting library, into Vue applications. The current stable version is 0.5.0, which notably adds support for KaTeX 0.12.0. The project's release cadence appears to be driven by updates to its peer dependency, KaTeX, ensuring compatibility with newer versions of the typesetting library. Key differentiators include its dual approach to usage, offering both a `v-katex` directive for inline or display-mode rendering and a `<katex-element>` component for more structured integration. It also supports KaTeX's auto-render functionality, simplifying the display of math expressions embedded directly in HTML. This package streamlines the process of rendering complex mathematical formulas within Vue ecosystems.
Common errors
-
KaTeX parse error: Undefined control sequence: \frac at position 1
cause Incorrect TeX syntax or unescaped backslashes in a template string.fixDouble-check your TeX expression for syntax errors and ensure all backslashes are escaped (e.g., `v-katex="'\\frac{a}{b}'"`). -
Failed to resolve component: katex-element
cause The `vue-katex` plugin (which registers `KatexElement` and `v-katex`) has not been registered with Vue.fixEnsure you have called `Vue.use(VueKatex)` in your application's entry point after importing `Vue` and `VueKatex`. -
Math expressions appear unstyled, in plain text, or with incorrect fonts.
cause KaTeX stylesheet `katex.min.css` is not loaded in your application.fixImport `katex/dist/katex.min.css` in your main JavaScript file or include it via a `<link>` tag in your `index.html`.
Warnings
- gotcha Ensure KaTeX peer dependency matches `vue-katex` version support. `vue-katex@0.5.0` is specifically designed for KaTeX `0.12.0`.
- gotcha KaTeX's essential stylesheet (`katex.min.css`) is not automatically bundled or injected by `vue-katex`. Failure to import it will result in unstyled or incorrectly rendered math expressions.
- gotcha When using the `v-katex` directive with string literals in Vue templates, remember to escape all backslashes (`\`) in your TeX expressions, as single backslashes are interpreted as JavaScript escape sequences.
- gotcha Local KaTeX options provided to `v-katex` or `KatexElement` will override globally defined options, except for `object` or `array` type options, which are merged.
Install
-
npm install vue-katex -
yarn add vue-katex -
pnpm add vue-katex
Imports
- Vue
const Vue = require('vue')import Vue from 'vue'
- VueKatex
import { VueKatex } from 'vue-katex'import VueKatex from 'vue-katex'
- katex.min.css
require('katex/dist/katex.min.css')import 'katex/dist/katex.min.css'
Quickstart
import Vue from 'vue';
import VueKatex from 'vue-katex';
import 'katex/dist/katex.min.css';
Vue.use(VueKatex, {
globalOptions: {
displayMode: true, // Default to display mode globally
throwOnError: false,
errorColor: '#FF0000'
}
});
new Vue({
el: '#app',
data: {
inlineMath: '\\frac{a_i}{1+x^2}',
displayMath: '\\int_0^1 x^2 dx = \\frac{1}{3}',
autoRenderContent: 'The quadratic formula is \\(x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\\)
and then some display math: $$ E=mc^2 $$'
},
template: `
<div id="app">
<h1>Vue KaTeX Example</h1>
<h2>Using v-katex directive</h2>
<p>Inline: <span v-katex="inlineMath"></span></p>
<p>Display mode (local override): <div v-katex:display="displayMath"></div></p>
<p>With options: <div v-katex="{ expression: '\\sqrt{2}', options: { throwOnError: false, errorColor: '#00F' } }"></div></p>
<h2>Using v-katex:auto directive</h2>
<div v-katex:auto>
{{ autoRenderContent }}
</div>
<h2>Using KatexElement component</h2>
<katex-element :expression="displayMath" :display-mode="false" />
<katex-element expression="\\sum_{n=1}^\\infty \\frac{1}{n^2} = \\frac{\\pi^2}{6}" :display-mode="true" :throw-on-error="false" error-color="#00A" />
<p>Check the console for any KaTeX errors if throwOnError is false.</p>
</div>
`
});