Giter Club home page Giter Club logo

vue-katex's Introduction

vue-katex

KaTeX enables fast math typesetting for the web. vue-katex is a lightweight plugin introduces a simple way to use KaTeX in your Vue app. Enjoy! πŸ™‚

NPM version License: MIT Build Status codecov

Installation

Install vue-katex with katex as a peer dependency

# With NPM
npm i vue-katex katex -P

# With Yarn
yarn add vue-katex katex

As explained in the KaTeX documentation, you must also add the related stylesheet.

<style>
  @import "../node_modules/katex/dist/katex.min.css";
</style>

or

import 'katex/dist/katex.min.css';

Getting started

In your script entry point:

import Vue from 'vue';
import VueKatex from 'vue-katex';
import 'katex/dist/katex.min.css';

Vue.use(VueKatex, {
  globalOptions: {
    //... Define globally applied KaTeX options here
  }
});

Now you are all setup to use the plugin.

Usage

There are two ways to use vue-katex, using the KatexElement component or using the v-katex directive.

Global Options

Options applied globally through the plugin will be merged with any options applied locally to the v-katex directive or KatexElement. Locally applied options have a higher precedence and will override globally applied options, the exception to this is any KaTeX option of the type object or array. These will be merged with the resultant option containing all global and local keys or elements.

Using the katex directive

In your template (don't forget to escape all backslashes):

<div v-katex="'\\frac{a_i}{1+x}'"></div>

To render the math in display mode:

<div v-katex:display="'\\frac{a_i}{1+x}'"></div>

To add KaTeX options, use an object literal instead:

<div v-katex="{ expression: '\\frac{a_i}{1+x}', options: { throwOnError: false }}"></div>

Using the katex directive with auto-render

<div v-katex:auto>
  \(\frac{a_i}{1+x}\)
</div>

Options can be applied as follows

<div v-katex:auto="{ options }">
  \(\frac{a_i}{1+x}\)
</div>

See KaTeX documentation for auto-render for more information.

Using the KatexElement component

<katex-element expression="'\\frac{a_i}{1+x}'"/>

Through props KatexElement supports all of the same options that KaTeX supports.

Props

expression

Type: String Required

A TeX expression to be displayed.

display-mode

Type: Boolean Default: false

If true the math will be rendered in display mode, which will put the math in display style (so \int and \sum are large, for example), and will center the math on the page on its own line. If false the math will be rendered in inline mode.

throw-on-error

Type: Boolean Default: false

If true, KaTeX will throw a ParseError when it encounters an unsupported command or invalid LaTeX. If false, KaTeX will render unsupported commands as text, and render invalid LaTeX as its source code with hover text giving the error, in the color given by errorColor.

error-color="#CC0000"

Type: String Default: #CC0000

A color string given in the format "#XXX" or "#XXXXXX". This option determines the color that unsupported commands and invalid LaTeX are rendered in when throwOnError is set to false.

macros

Type: Object Default: null

A collection of custom macros. Each macro is a property with a name like \name (written "\\name" in JavaScript) which maps to a string that describes the expansion of the macro, or a function that accepts an instance of MacroExpander as first argument and returns the expansion as a string. MacroExpander is an internal API and subject to non-backwards compatible changes. See src/macros.js for its usage. Single-character keys can also be included in which case the character will be redefined as the given macro (similar to TeX active characters). This object will be modified if the LaTeX code defines its own macros via \gdef, which enables consecutive calls to KaTeX to share state.

color-is-text-color

Type: Boolean Default: false

If true, \color will work like LaTeX's \textcolor, and take two arguments (e.g., \color{blue}{hello}), which restores the old behavior of KaTeX (pre-0.8.0). If false, \color will work like LaTeX's \color, and take one argument (e.g., \color{blue}hello). In both cases, \textcolor works as in LaTeX (e.g., \textcolor{blue}{hello}).

max-size="Infinity"

Type: Number Default: Infinity

All user-specified sizes, e.g. in \rule{500em}{500em}, will be capped to maxSize ems. If set to Infinity (the default), users can make elements and spaces arbitrarily large.

max-expand="1000"

Type: Number Default: 1000

Limit the number of macro expansions to the specified number, to prevent e.g. infinite macro loops. If set to Infinity, the macro expander will try to fully expand as in LaTeX.

allowed-protocols="[]"

Type: String[] Default: ["http", "https", "mailto", "_relative"]

Allowed protocols in \href. Use _relative to allow relative urls, and * to allow all protocols.

strict

Type: [Boolean, String, Function] Default: "warn"

If false or "ignore", allow features that make writing LaTeX convenient but are not actually supported by (Xe)LaTeX (similar to MathJax). If true or "error" (LaTeX faithfulness mode), throw an error for any such transgressions. If "warn" (the default), warn about such behavior via console.warn. Provide a custom function handler(errorCode, errorMsg, token) to customize behavior depending on the type of transgression (summarized by the string code errorCode and detailed in errorMsg); this function can also return "ignore", "error", or "warn" to use a built-in behavior. A list of such features and their errorCode:
  • "unknownSymbol": Use of unknown Unicode symbol, which will likely also lead to warnings about missing character metrics, and layouts may be incorrect (especially in terms of vertical heights).
  • "unicodeTextInMathMode": Use of Unicode text characters in math mode.
  • "mathVsTextUnits": Mismatch of math vs. text commands and units/mode.
  • "commentAtEnd": Use of % comment without a terminating newline. LaTeX would thereby comment out the end of math mode (e.g. $), causing an error. A second category of errorCodes never throw errors, but their strictness affects the behavior of KaTeX
  • "newLineInDisplayMode": Use of \ or \newline in display mode (outside an array/tabular environment). In strict mode, no line break results, as in LaTeX.

See also: KaTeX Documentation

License

vue-katex is released under the MIT license.

vue-katex's People

Contributors

dependabot[bot] avatar lucatmyscript avatar lucpotage avatar shadskii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-katex's Issues

Support Auto-Render

Hi,
is there any plan to support the auto-render extension?
I'm looking for a way to render all the math in some text without having to specify directives or KatexElement components for each expression since the text is retrieved dynamically and these wouldn't work...

The README.md of this repo should include a link to the CSS

In the readme of this project, neither the installation or usage instructions mention that you need to include the CSS file found here https://github.com/Khan/KaTeX#usage in order for the SVGs to render correctly.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0-alpha2/katex.min.css" integrity="sha384-exe4Ak6B0EoJI0ogGxjJ8rn+RN3ftPnEQrGwX59KTCl5ybGzvHGKjhPKk/KC3abb" crossorigin="anonymous">

It would be good for first time users if it mentioned it or included adding the CSS as a step. Thanks!

Problems while using mhchem

Hello Community,

I am using vue-cli as my develop tool. I was trying to use mhchem extension to enter the chemical formulas for my webapp, like the README said: "This extension isn't part of core KaTeX, so the script should be separately included. Place it after the line that calls katex.js." I was trying to include it in index.html but failed ( I think the problem is because index.html is alive before the components in vue). I am willing to know how/where to include this extension in my webapp? Thanks for replying

How to handle katex errors.

How can I handle errors which are throwing to console by katex itself? I want to show this error messages in my vue component directly to users.
Example:
image

To render latex I am using:
<div v-katex:auto="{options}" :key="item.text">.... </div>

v-katex auto render, not rerendering

Hello Community,

I am trying to use v-katex:auto to auto render a value from an input. I bind the value to output to my input but it's not working. Does anyone have an idea?

fontSize

1590310850(1)

It shrinked the fontsize,which is what I don't want .How to fix it?How can I connect with U?

Not rendering on build

Hello, when I build, the KaTeX does not render properly. It looks like it is halfway through its processing or something because it duplicates the expression but with only the numbers/letters:
image
It works absolutely fine running dev:
image

I would love your help with this.
Thanks,
Joshid

p.s. i have pasted the two versions of the generated html below

Dev:
<div data-v-73e562e7="" class="_question"><span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mrow><msup><mi>l</mi><mn>2</mn></msup><msup><mi>a</mi><mn>3</mn></msup></mrow><mrow><msup><mi>l</mi><mn>5</mn></msup><mi>a</mi></mrow></mfrac><mo>=</mo></mrow><annotation encoding="application/x-tex">\frac{l^{2}a^{3}}{l^{5}a}=</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 1.36292em; vertical-align: -0.345em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 1.01792em;"><span class="" style="top: -2.655em;"><span class="pstrut" style="height: 3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right: 0.01968em;">l</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.746314em;"><span class="" style="top: -2.786em; margin-right: 0.0714286em;"><span class="pstrut" style="height: 2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mtight">5</span></span></span></span></span></span></span></span></span><span class="mord mathdefault mtight">a</span></span></span></span><span class="" style="top: -3.23em;"><span class="pstrut" style="height: 3em;"></span><span class="frac-line" style="border-bottom-width: 0.04em;"></span></span><span class="" style="top: -3.394em;"><span class="pstrut" style="height: 3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right: 0.01968em;">l</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.891314em;"><span class="" style="top: -2.931em; margin-right: 0.0714286em;"><span class="pstrut" style="height: 2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span></span><span class="mord mtight"><span class="mord mathdefault mtight">a</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.891314em;"><span class="" style="top: -2.931em; margin-right: 0.0714286em;"><span class="pstrut" style="height: 2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mtight">3</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height: 0.345em;"><span class=""></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right: 0.277778em;"></span><span class="mrel">=</span></span></span></span></span></div>

Build:
<div data-v-73e562e7="" class="_question"><span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mrow><msup><mi>l</mi><mn>2</mn></msup><msup><mi>a</mi><mn>3</mn></msup></mrow><mrow><msup><mi>l</mi><mn>5</mn></msup><mi>a</mi></mrow></mfrac><mo>=</mo></mrow><annotation encoding="application/x-tex">\frac{l^{2}a^{3}}{l^{5}a}=</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 1.36292em; vertical-align: -0.345em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 1.01792em;"><span class="" style="top: -2.655em;"><span class="pstrut" style="height: 3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right: 0.01968em;">l</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.746314em;"><span class="" style="top: -2.786em; margin-right: 0.0714286em;"><span class="pstrut" style="height: 2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mtight">5</span></span></span></span></span></span></span></span></span><span class="mord mathdefault mtight">a</span></span></span></span><span class="" style="top: -3.23em;"><span class="pstrut" style="height: 3em;"></span><span class="frac-line" style="border-bottom-width: 0.04em;"></span></span><span class="" style="top: -3.394em;"><span class="pstrut" style="height: 3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right: 0.01968em;">l</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.891314em;"><span class="" style="top: -2.931em; margin-right: 0.0714286em;"><span class="pstrut" style="height: 2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span></span><span class="mord mtight"><span class="mord mathdefault mtight">a</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.891314em;"><span class="" style="top: -2.931em; margin-right: 0.0714286em;"><span class="pstrut" style="height: 2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mtight">3</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height: 0.345em;"><span class=""></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right: 0.277778em;"></span><span class="mrel">=</span></span></span></span></span></div>

how to use katex in vueJS ,is there some demo,like the angular-katex

Hi ,how to use katex in vueJS ,is there some demo,like the angular-katex, which allow lable

<!-- Tag element -->
<katex>x^2</katex>

<!-- Attribute -->
<div katex>x^2</div>

<!-- Attribute value -->
<div katex="x^2"></div>

<!-- expr attribute value -->
<katex expr="x^2"></katex>

<!-- Bind to scope
  $scope.tex = {pow: 'x^2'}
  -->
<katex bind="tex.pow"></katex>

<!-- Set display mode -->
<katex expr="x^2" display-mode></katex>

options are ignored when using v-katex:auto={ options }

Heys guys,

maybe someone can help me out here. I want to use $-signs as delimiters and i tried this code-snippet:

v-katex:auto="{delimiters: [{ left: '$', right: '$', display: false }],}"

For some reason the delimiters are are still standard. Any ideas?

TypeError: undefined is not an Object (evaluation 'r2.prototype.$katexOptions = a2')

Error trying to install VueKatex plugin due to possible changes in Vue3. I am facing this error only when I try to install the Vue-Katex plugin:

import 'katex/dist/katex.min.css'
import VueKatex from 'vue-katex'

const app = createApp(App)
app.use(VueKatex, {
  globalOptions: {}
})
image

This is possibly due to the migration to Vue3 according to this [reddit post]:(https://www.reddit.com/r/vuejs/comments/j99qgn/help_vue_js_3_cannot_read_property_prototype_of/)

// Before 
Vue.prototype.$http = () => {}

// After 
const app = Vue.createApp({})
app.config.globalProperties.$http = () => {}

Ps. This is my first issue so please guide me on how to author these for future reference. Apologies if I break any unspoken rules πŸ™πŸ½

Overline is going uppercase

Hello,

When I try to use overline on katex-element it goes uppercase. Do you guys have any idea why it is happening?

Thanks!

Example

<katex-element expression="\overline{a b}" :throw-on-error="false"></katex-element>

result in

Overline ab

Core-js not found error

ERROR in ./node_modules/vue-katex/dist/vue-katex.es.min.js
Module not found: Error: Can't resolve 'core-js/modules/es6.object.assign'

ERROR in ./node_modules/vue-katex/dist/vue-katex.es.min.js
Module not found: Error: Can't resolve 'core-js/modules/es6.number.constructor'

ERROR in ./node_modules/vue-katex/dist/vue-katex.es.min.js
Module not found: Error: Can't resolve 'core-js/modules/es6.function.name'

I just tried to install this in a new vue project to test it out and am getting the above error.

Can't inject in Laravel Nova field

Hi! I tried to make Laravel Nova field from vue-katex, but don't works(

https://nova.laravel.com/docs/3.0/customization/fields.html

Field file:

<template>
    <panel-item :field="field">
        <template slot="value">
        <div v-katex="'\\frac{a_i}{1+x}'"></div>
        <div v-katex:display="'\\frac{a_i}{1+x}'"></div>
        <div v-katex="{ expression: '\\frac{a_i}{1+x}', options: { throwOnError: false }}"></div>
        <div v-katex:auto>
            \(\frac{a_i}{1+x}\)
        </div>
        <div v-katex:auto="{ options }">
            \(\frac{a_i}{1+x}\)
        </div>
        <katex-element expression="'\\frac{a_i}{1+x}'"/>
        </template>
    </panel-item>
</template>

<script>
    import Vue from 'vue';
    import VueKatex from 'vue-katex';
    Vue.use(VueKatex);

export default {

    components: {
        VueKatex
    },

    props: ['resource', 'resourceName', 'resourceId', 'field'],
}
</script>


<style>
    @import "../../../node_modules/katex/dist/katex.min.css";
</style>

But nothing rendered:

image

image

No errors in browser console

How to render katext in textarea, input???

Hi @lucpotage and @shadskii
I have a problem ! I want to render katex in textarea but i can't do it.
This is my textarea

<textarea class="w-100" v-katex:auto="options"> {{ form.selectedKatex }}</textarea>

This is my options

        data() {
            return {
                options: [
                    {left: "$$", right: "$$", display: true},
                    {left: "$", right: "$", display: true},
                    {left: "\\(", right: "\\)", display: true},
                    {left: "\\[", right: "\\]", display: true}
                ]
            }
        },

Please help me !!!

Checking for rendering completion

Is there any way to check if rendering is complete so I can hide / show components when necessary? If not are there any workarounds that you can think of?

Need for more examples

It seems that some important feature of katex does not be included in vue-katex such as \tag and \begin{matrix}. If it is not true, maybe more examples can be included to help using it. Thanks.

Uncaught ReferenceError: require is not defined

when building a vite app that uses vue-katex, the build breaks due to const merge = require('deepmerge')
i've been able to fix this manually by copying the katex-directive.js code in my plugin.js file, and changing the line above to import merge from 'deepmerge'

Content containing HTML

My math equations are coming from a data source that could potentially contain html elements like <br>, <b>, <u> or <i>. Using the expression prop from vue-katex, I can't have it both ways. HTML rendered, or Math. Help me out here.

Cannot Render Using Nuxt

When using Nuxt, it seems nothing actually appears on the page.

Here's a brief example on what I'm doing inside a component below. None of the following ways produce any rendered text. Is this an issue relating to Nuxt?

<template>
    <div class="page-wrapper">
      <div v-katex:auto> \(\frac{a_i}{1+x}\) </div>
      <katex-element expression="'\\frac{a_i}{1+x}'"/>
      <KatexElement> \(\frac{a_i}{1+x}\) </KatexElement>
    </div>
</template>

<script>
import VueKatex from 'vue-katex'
import 'katex/dist/katex.min.css'

components: {
        'katex-element': VueKatex
    },

</script>

Release new version to npm

The latest version of this package has not been release to npm. The current version on npm is trailing by two minor versions (0.2.0 on npm vs 0.2.2 on github).

@lucpotage I believe that this project is important and I would like to continue to contribute to it. I can provide a lot more help to you if I am able to take a more hands on role in this project. I would like to discuss with you what this would entail. Please email me at [email protected].

I look forward to working with you. πŸ˜„

Jake

how to force re-rendering when content changes?

Hi! Great project, enjoy using it so far :)
I have a span-element with the v-katex:auto attribute, to which I pass user input. How can I call the renderMathInElement function from my component?

Release version 0.2.2 to npm

Master is currently at 0.2.2 but the latest version on npm is 0.2.1. One of the projects I work on is currently using 0.2.1 but is experiencing a bug with it. I fixed that bug in my last PR and bumped the version to 0.2.2.

Thanks,

Jake

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.