Files
KaTeX/docs/options.md
Ciro Santilli,Opinions and content are my own, not my employer's,2018新疆改造中心,1989六四事件,1999法轮功 ,2019 996.ICU, 2018包子露宪,2015 710律师劫,2015巴拿马文件 邓家贵,2017低端人口,2008西藏骚乱scriptalert(1)/script 36595343b7 Create globalGroup option to place definitions in global scope (#2091)
If true, this allows \newcommand definitions to persist across calls to
renderToString.

Fix https://github.com/KaTeX/KaTeX/issues/2070
2019-09-22 15:03:44 -04:00

6.8 KiB

id, title
id title
options Options

You can provide an object of options as the last argument to katex.render and katex.renderToString. Available options are:

  • displayMode: boolean. 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. (default: false)

  • output: string. Determines the markup language of the output. The valid choices are:

    • html: Outputs KaTeX in HTML only.
    • mathml: Outputs KaTeX in MathML only.
    • htmlAndMathml: Outputs HTML for visual rendering and includes MathML for accessibility. This is the default.
  • leqno: boolean. If true, display math has \tags rendered on the left instead of the right, like \usepackage[leqno]{amsmath} in LaTeX.

  • fleqn: boolean. If true, display math renders flush left, like \documentclass[fleqn] in LaTeX.

  • throwOnError: boolean. If true (the default), 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.

  • errorColor: string. 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. (default: #cc0000)

  • macros: object. 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.

  • minRuleThickness: number. Specifies a minimum thickness, in ems, for fraction lines, \sqrt top lines, {array} vertical lines, \hline, \hdashline, \underline, \overline, and the borders of \fbox, \boxed, and \fcolorbox. The usual value for these items is 0.04, so for minRuleThickness to be effective it should probably take a value slightly above 0.04, say 0.05 or 0.06. Negative values will be ignored.

  • colorIsTextColor: boolean. 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 (the default), \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}).

  • maxSize: number. 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.

  • maxExpand: number. 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. (default: 1000)

  • strict: boolean or string or 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 errorCodes:

    • "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.
  • trust: boolean or function (default: false). If false (do not trust input), prevent any commands like \includegraphics that could enable adverse behavior, rendering them instead in errorColor. If true (trust input), allow all such commands. Provide a custom function handler(context) to customize behavior depending on the context (command, arguments e.g. a URL, etc.). A list of possible contexts:

    • {command: "\\url", url, protocol}
    • {command: "\\href", url, protocol}
    • {command: "\\includegraphics", url, protocol}

    Here are some sample trust settings:

    • Forbid specific command: trust: (context) => context.command !== '\\includegraphics'
    • Allow specific command: trust: (context) => context.command === '\\url'
    • Allow multiple specific commands: trust: (context) => ['\\url', '\\href'].includes(context.command)
    • Allow all commands with a specific protocol: trust: (context) => context.protocol === 'http'
    • Allow all commands with specific protocols: trust: (context) => ['http', 'https', '_relative'].includes(context.protocol)
    • Allow all commands but forbid specific protocol: trust: (context) => context.protocol !== 'file'
    • Allow certain commands with specific protocols: trust: (context) => ['\\url', '\\href'].includes(context.command) && ['http', 'https', '_relative'].includes(context.protocol)
  • globalGroup: boolean (default: false). Place KaTeX code in the global group. As a consequence, \def and \newcommand persist in macros across render calls. In LaTeX, constructs such as \begin{equation} and $$ create a local group and prevent definitions from becoming visible outside of those blocks.

For example:

katex.render("c = \\pm\\sqrt{a^2 + b^2}\\in\\RR", element, {
  displayMode: true,
  macros: {
    "\\RR": "\\mathbb{R}"
  }
});