-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a KaTeX and MathJax based LaTeX pane (#311)
- Loading branch information
1 parent
f9d1f61
commit 16f32ea
Showing
9 changed files
with
304 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import panel as pn\n", | ||
"\n", | ||
"pn.extension('katex', 'mathjax')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The ``LaTeX`` pane allows rendering LaTeX equations as HTML. It uses either [MathJax](https://www.mathjax.org) or [KaTeX](https://katex.org/) depending on the defined renderer, by default it will use the renderer loaded in the extension (e.g. ``pn.extension('katex')``) and will default to KaTeX.\n", | ||
"\n", | ||
"#### Parameters:\n", | ||
"\n", | ||
"For layout and styling related parameters see the [layout user guide](../../user_guide/Layout.ipynb).\n", | ||
"\n", | ||
"* **``options``** (list or dict): A list or dictionary of options to select from\n", | ||
"* **``renderer``** (object): The current value, must be one of the option values\n", | ||
"* **``style``** (dict): Dictionary specifying CSS styles\n", | ||
"\n", | ||
"___" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"A ``LaTeX`` pane will render any object with a ``_repr_latex_`` method as well as SymPy expressions, or any string containing HTML. Any ``LaTeX`` section should be wrapped in `$` or ``\\(`` and ``\\(`` delimiters, e.g.:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"latex = pn.pane.LaTeX('The LaTeX pane supports two delimiters: $LaTeX$ and \\(LaTeX\\)', style={'font-size': '18pt'}, width=800)\n", | ||
"latex" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The ``LaTeX`` pane can be updated like other panes:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"latex.object = '$\\sum_{j}{\\sum_{i}{a*w_{j, i}}}$'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"And can also be composed like any other pane:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"maxwell = pn.pane.LaTeX(r\"\"\"\n", | ||
"$\\begin{aligned}\n", | ||
" \\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\\n", | ||
" \\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\\n", | ||
" \\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\\n", | ||
" \\nabla \\cdot \\vec{\\mathbf{B}} & = 0\n", | ||
"\\end{aligned}\n", | ||
"$\"\"\", style={'font-size': '24pt'})\n", | ||
"\n", | ||
"cauchy_schwarz = pn.pane.LaTeX(object=r\"\"\"\n", | ||
"$\\left( \\sum_{k=1}^n a_k b_k \\right)^2 \\leq \\left( \\sum_{k=1}^n a_k^2 \\right) \\left( \\sum_{k=1}^n b_k^2 \\right)$\n", | ||
"\"\"\", style={'font-size': '24pt'})\n", | ||
"\n", | ||
"cross_product = pn.pane.LaTeX(object=r\"\"\"\n", | ||
"$\\mathbf{V}_1 \\times \\mathbf{V}_2 = \\begin{vmatrix}\n", | ||
"\\mathbf{i} & \\mathbf{j} & \\mathbf{k} \\\\\n", | ||
"\\frac{\\partial X}{\\partial u} & \\frac{\\partial Y}{\\partial u} & 0 \\\\\n", | ||
"\\frac{\\partial X}{\\partial v} & \\frac{\\partial Y}{\\partial v} & 0\n", | ||
"\\end{vmatrix}\n", | ||
"$\"\"\", style={'font-size': '24pt'})\n", | ||
"\n", | ||
"spacer = pn.Spacer(width=50)\n", | ||
"\n", | ||
"pn.Column(\n", | ||
" pn.pane.Markdown('# The KaTeX Pane'),\n", | ||
" pn.Row(maxwell, spacer, cross_product, spacer, cauchy_schwarz)\n", | ||
")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python", | ||
"pygments_lexer": "ipython3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
Defines a custom KaTeX bokeh model to render text using KaTeX. | ||
""" | ||
import os | ||
|
||
from bokeh.models import Markup | ||
|
||
from ..util import CUSTOM_MODELS | ||
|
||
|
||
class KaTeX(Markup): | ||
""" | ||
A bokeh model that renders text using KaTeX. | ||
""" | ||
|
||
__javascript__ = ["https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.js", | ||
"https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/contrib/auto-render.min.js"] | ||
|
||
__css__ = ["https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.css"] | ||
|
||
__implementation__ = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'katex.ts') | ||
|
||
|
||
CUSTOM_MODELS['panel.models.katex.KaTeX'] = KaTeX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as p from "core/properties" | ||
import {Markup, MarkupView} from "models/widgets/markup" | ||
|
||
export class KaTeXView extends MarkupView { | ||
model: KaTeX | ||
|
||
render(): void { | ||
super.render(); | ||
this.markup_el.innerHTML = this.model.text; | ||
(window as any).renderMathInElement(this.el, { | ||
delimiters: [ | ||
{left: "$$", right: "$$", display: true}, | ||
{left: "\\[", right: "\\]", display: true}, | ||
{left: "$", right: "$", display: false}, | ||
{left: "\\(", right: "\\)", display: false} | ||
] | ||
}) | ||
} | ||
} | ||
|
||
export namespace KaTeX { | ||
export type Attrs = p.AttrsOf<Props> | ||
export type Props = Markup.Props & { | ||
text: p.Property<string> | ||
} | ||
} | ||
|
||
export interface KaTeX extends KaTeX.Attrs {} | ||
|
||
export class KaTeX extends Markup { | ||
properties: KaTeX.Props | ||
|
||
constructor(attrs?: Partial<KaTeX.Attrs>) { | ||
super(attrs) | ||
} | ||
|
||
static initClass(): void { | ||
this.prototype.type = "KaTeX" | ||
this.prototype.default_view = KaTeXView | ||
} | ||
} | ||
KaTeX.initClass() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
Defines a custom MathJax bokeh model to render text using MathJax. | ||
""" | ||
import os | ||
|
||
from bokeh.models import Markup | ||
|
||
from ..util import CUSTOM_MODELS | ||
|
||
|
||
class MathJax(Markup): | ||
""" | ||
A bokeh model that renders text using MathJax. | ||
""" | ||
|
||
__javascript__ = ["https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-MML-AM_CHTML"] | ||
|
||
__implementation__ = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'mathjax.ts') | ||
|
||
|
||
CUSTOM_MODELS['panel.models.mathjax.MathJax'] = MathJax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as p from "core/properties" | ||
import {Markup, MarkupView} from "models/widgets/markup" | ||
|
||
export class MathJaxView extends MarkupView { | ||
model: MathJax | ||
private _hub: any | ||
|
||
initialize(): void { | ||
super.initialize() | ||
this._hub = (window as any).MathJax.Hub; | ||
this._hub.Config({ | ||
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]} | ||
}); | ||
} | ||
|
||
render(): void { | ||
super.render(); | ||
this.markup_el.innerHTML = this.model.text; | ||
this._hub.Queue(["Typeset", this._hub, this.markup_el]); | ||
} | ||
} | ||
|
||
export namespace MathJax { | ||
export type Attrs = p.AttrsOf<Props> | ||
export type Props = Markup.Props & { | ||
text: p.Property<string> | ||
} | ||
} | ||
|
||
export interface MathJax extends MathJax.Attrs {} | ||
|
||
export class MathJax extends Markup { | ||
properties: MathJax.Props | ||
|
||
constructor(attrs?: Partial<MathJax.Attrs>) { | ||
super(attrs) | ||
} | ||
|
||
static initClass(): void { | ||
this.prototype.type = "MathJax" | ||
this.prototype.default_view = MathJaxView | ||
} | ||
} | ||
MathJax.initClass() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.