
I’m trying to replicate the bokeh latex example mentioned at Creating LaTeX labels — Bokeh 2.4.2 Documentation in jupyter notebook for LabelSet. I could see the katex.min.js being loaded from web console. However when the LatexLabel renders, it states katex not defined . Katex JS doc says, it should be available globally once js is loaded.
Versions: bokeh - 1.4.0, katex - 0.11, python - 3.7
import numpy as np
from bokeh.models import LabelSet
from bokeh.plotting import figure
from bokeh.io import output_notebook, show, curdoc
from bokeh.util.compiler import TypeScript
TS_CODE = """
import * as p from "core/properties"
import {LabelSet, LabelSetView} from "models/annotations/label_set"
declare const katex: any
export class LatexLabelSetView extends LabelSetView {
model: LatexLabelSet
render(): void {
const {ctx} = this.plot_view.canvas_view
const [sx, sy] = this._map_data()
for (let i = 0; i < this._text.length; i++) {
try {
this._v_css_text(ctx, i, this._text[i], sx[i] + this._x_offset[i], sy[i] - this._y_offset[i], this._angle[i])
console.log(this._text[i])
console.log(katex)
katex.render(this._text[i], this.el, {displayMode: true})
}
catch(e) {
console.log( 'Error: ' + e);
}
}
}
}
export namespace LatexLabelSet {
export type Attrs = p.AttrsOf<Props>
export type Props = LabelSet.Props
}
export interface LatexLabelSet extends LatexLabelSet.Attrs {}
export class LatexLabelSet extends LabelSet {
properties: LatexLabelSet.Props
constructor(attrs?: Partial<LatexLabelSet.Attrs>) {
super(attrs)
}
static init_LatexLabelSet() {
this.prototype.default_view = LatexLabelSetView
}
}
LatexLabelSet.init_LatexLabelSet()
"""
class LatexLabelSet(LabelSet):
__javascript__ = ["https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js"]
__css__ = ["https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css"]
__implementation__ = TypeScript(TS_CODE)
x = np.arange(0.0, 1.0 + 0.01, 0.01)
y = np.cos(2 * 2 * np.pi * x) + 2
p = figure(title="LaTex Demonstration", plot_width=500, plot_height=500)
p.line(x, y)
latex = LatexLabelSet(text=["f = \sum_{n=1}^\infty\\frac{-e^{i\pi}}{2^n}!"],
x=40, y=420, x_units='screen', y_units='screen',
render_mode='css', text_font_size='16pt',
background_fill_alpha=0)
p.add_layout(latex)
output_notebook()
show(p)


