LaTeX#

Open this notebook in Jupyterlite | Download this notebook from GitHub (right-click to download).


import panel as pn

pn.extension('katex', 'mathjax')

The LaTeX pane allows rendering LaTeX equations as HTML. It uses either MathJax or KaTeX depending on the defined renderer. By default it will use the renderer loaded in the extension (e.g. pn.extension('katex')), defaulting to KaTeX.

Parameters:#

For details on other options for customizing the component see the layout and styling how-to guides.

  • object (str or object): A string containing LaTeX code, an object with a _repr_latex_ method, or a SymPy expression

  • renderer (object): The current value; must be one of the option values

  • styles (dict): Dictionary specifying CSS styles


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.:

latex = pn.pane.LaTeX(
    r'The LaTeX pane supports two delimiters: $LaTeX$ and \(LaTeX\)', styles={'font-size': '18pt'}
)

latex

Its important to prefix your LaTeX strings with an r to make the string a raw string and not escape the \ character.

pn.Column(
    pn.pane.LaTeX("$\frac{1}{n}$"),
    pn.pane.LaTeX(r"$\frac{1}{n}$")
)

The LaTeX pane can be updated like other panes:

latex.object = r'$\sum_{j}{\sum_{i}{a*w_{j, i}}}$'

If both renderers have been loaded we can override the default renderer:

pn.pane.LaTeX(r'$\sum_{j}{\sum_{i}{a*w_{j, i}}}$', renderer='mathjax', styles={'font-size': '18pt'})

And can also be composed like any other pane:

maxwell = pn.pane.LaTeX(r"""
$\begin{aligned}
  \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
  \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
  \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
  \nabla \cdot \vec{\mathbf{B}} & = 0
\end{aligned}
$""", styles={'font-size': '24pt'})

cauchy_schwarz = pn.pane.LaTeX(object=r"""
$\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)$
""", styles={'font-size': '24pt'})

cross_product = pn.pane.LaTeX(object=r"""
$\mathbf{V}_1 \times \mathbf{V}_2 =  \begin{vmatrix}
\mathbf{i} & \mathbf{j} & \mathbf{k} \\
\frac{\partial X}{\partial u} &  \frac{\partial Y}{\partial u} & 0 \\
\frac{\partial X}{\partial v} &  \frac{\partial Y}{\partial v} & 0
\end{vmatrix}
$""", styles={'font-size': '24pt'})

spacer = pn.Spacer(width=50)

pn.Column(
    pn.pane.Markdown('# The LaTeX Pane'),
    pn.Row(maxwell, spacer, cross_product, spacer, cauchy_schwarz)
)

Controls#

The LaTeX pane exposes a number of options which can be changed from both Python and Javascript. Try out the effect of these parameters interactively:

pn.Row(latex.controls(jslink=True), latex)

Open this notebook in Jupyterlite | Download this notebook from GitHub (right-click to download).