How to use katex - 10 common examples

To help you get started, we’ve selected a few katex examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lutece-awesome / lutece-backend / frontend / src / plugins / katex.js View on Github external
Vue.directive('katex', (el, binding) => {
			// const displayStyle = binding.arg === 'display';
			// const expression = binding.value.expression ? binding.value.expression : binding.value;
			if (binding.value.expression) {
				if (binding.value.options) {
					renderMathInElement(
						el,
						...binding.value.options,
					);
				} else {
					renderMathInElement(
						el,
					);
				}
			} else {
				// console.log(el);
				renderMathInElement(
					el,
					{
						delimiters: [
							{ left: '$$', right: '$$', display: true },
							{ left: '$', right: '$', display: false },
							{ left: '\\[', right: '\\]', display: true },
							{ left: '\\(', right: '\\)', display: false },
						],
					},
				);
github tylingsoft / markdown-core / markdown-core-new.js View on Github external
mdc.renderer.rules.code_inline = function(tokens, idx) {
  var code = tokens[idx].content;
  if(code.startsWith('$') && code.endsWith('$')) { // inline math
    code = code.substr(1, code.length-2);
    try{
      return katex.renderToString(code);
    } catch(err) {
      return '<code>' + err + '</code>';
    }
  }
  return '<code>' + code + '</code>'; // not math
}
github lx4r / markdownfox / src / renderer / components / LandingPage.vue View on Github external
updated: function () {
      // TODO: use smaller scope here
      KatexAutoRender(document.body, {delimiters: [
        {
          left: '$$',
          right: '$$',
          display: false
        }
      ]})
    }
  }
github Khan / react-components / js / katex-a11y.js View on Github external
var parseMath = function(text) {
    // NOTE: `katex` is a global. We assume it has been imported somehow.
    //
    // colorIsTextColor is an option added in KaTeX 0.9.0 for backward
    // compatibility. It makes \color parse like \textcolor. We use it
    // in the KA webapp, and need it here because the tests are written
    // assuming it is set.
    return katex.__parse(text, {colorIsTextColor: true});
};
github MatejBransky / react-katex / index.js View on Github external
{},
        {
          displayMode: !!props.block,
          errorColor: props.errorColor,
          throwOnError: !!props.renderError
        },
        // you can rewrite all KaTeX options directly
        props.settings
      )
    );

    return (
      
    );
  } catch (error) {
    if (error instanceof KaTeX.ParseError || error instanceof TypeError) {
      return props.renderError ? (
        props.renderError(error)
      ) : (
        
      );
    }

    throw error;
  }
}
github KaTeX / KaTeX / contrib / auto-render / auto-render.js View on Github external
for (let i = 0; i &lt; data.length; i++) {
        if (data[i].type === "text") {
            fragment.appendChild(document.createTextNode(data[i].data));
        } else {
            const span = document.createElement("span");
            let math = data[i].data;
            // Override any display mode defined in the settings with that
            // defined by the text itself
            optionsCopy.displayMode = data[i].display;
            try {
                if (optionsCopy.preProcess) {
                    math = optionsCopy.preProcess(math);
                }
                katex.render(math, span, optionsCopy);
            } catch (e) {
                if (!(e instanceof katex.ParseError)) {
                    throw e;
                }
                optionsCopy.errorCallback(
                    "KaTeX auto-render: Failed to parse `" + data[i].data +
                    "` with ",
                    e
                );
                fragment.appendChild(document.createTextNode(data[i].rawData));
                continue;
            }
            fragment.appendChild(span);
        }
    }

    return fragment;
};
github ink-components / ink-components / src / InkArticle.js View on Github external
renderMath() {
        // This is necessary to render inline math for firefox.
        // s are treated differently.
        renderMathInElement(this, MATH_RENDER_OPTIONS);
    }
    renderOutline() {
github obedm503 / showdown-katex / src / showdown-katex.js View on Github external
);
              node.nodeValue = newText;
            });
          });
        }

        // find the math in code blocks
        const latex = wrapper.querySelectorAll('code.latex.language-latex');
        const asciimath = wrapper.querySelectorAll(
          'code.asciimath.language-asciimath',
        );

        renderBlockElements({ elements: latex, config });
        renderBlockElements({ elements: asciimath, config, isAsciimath: true });

        renderMathInElement(wrapper, config);

        // return html without the wrapper
        return wrapper.innerHTML;
      },
    },
github twosigma / beakerx / js / beakerx_tabledisplay / src / tableDisplay / dataGrid / cell / LatexRenderer.ts View on Github external
*  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import * as katex from 'katex';

export const katexCss = require('katex/dist/katex.css').toString();

const latexFormulaRegex = /^(?:\$)(.+)(?:\$)$/; // match strings like '$e^{i\pi} + 1 = 0$'
const latexCache = {};

export default class LatexRenderer {

    /**
     * Convert given LaTeX formula into HTML representation.
     * @param text - ex. '$e^{i\pi} + 1 = 0$'
     */
    static latexToHtml(text: string): string {
        let [, formula] = text.match(latexFormulaRegex);

        if (latexCache[formula]) {
            return latexCache[formula];
        }
github talyssonoc / react-katex / test / sharedExamples.js View on Github external
it('renders correctly', () =&gt; {
      const math = shallow({ integralFormula });

      expect(math.html()).to.equal(
        `&lt;${wrapperTag}&gt;${ KaTeX.renderToString(integralFormula, { displayMode }) }`
      );
    });