Vue KaTeX Plugin

0.5.0 · maintenance · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the setup of `vue-katex` by registering it globally with `Vue.use()`, including the necessary stylesheet. It then showcases both the `v-katex` directive for rendering inline and display math with options, including `auto-render` functionality, and the `<katex-element>` component for declarative TeX expression rendering.

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>
  `
});

view raw JSON →