Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding functionality to render Latex as SVG. #61

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ProofWidgets.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ProofWidgets.Component.Basic
import ProofWidgets.Component.FilterDetails
import ProofWidgets.Component.HtmlDisplay
import ProofWidgets.Component.InteractiveSvg
import ProofWidgets.Component.Latex
import ProofWidgets.Component.MakeEditLink
import ProofWidgets.Component.OfRpcMethod
import ProofWidgets.Component.Panel.Basic
Expand Down
17 changes: 17 additions & 0 deletions ProofWidgets/Component/Latex.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Lean.Server.Rpc.Basic

import ProofWidgets.Component.Basic

namespace ProofWidgets

open Lean Server

structure LatexProps where
content : String
deriving Server.RpcEncodable

@[widget_module]
def Latex : Component LatexProps where
javascript := include_str ".." / ".." / ".lake" / "build" / "js" / "latexToSvg.js"

end ProofWidgets
29 changes: 29 additions & 0 deletions ProofWidgets/Demos/LatexExpr.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import ProofWidgets.Component.Panel.GoalTypePanel
import ProofWidgets.Component.Panel.SelectionPanel -- Needed for GoalTypePanel
import ProofWidgets.Component.HtmlDisplay
import ProofWidgets.Component.Latex
import ProofWidgets.Demos.Macro

open ProofWidgets Jsx Lean

@[expr_presenter]
def latex_presenter : ExprPresenter where
userName := "Latex"
layoutKind := .inline
present e :=
return <span>
<Latex content={(← Lean.Meta.ppExpr e).pretty} />
</span>

example : 2 + 2 = 4 ∧ 3 + 3 = 6 := by
with_panel_widgets [GoalTypePanel]
-- Place cursor here.
constructor
rfl
rfl

macro "#latex " src:term : command =>
Lean.TSyntax.mkInfoCanonical <$>
`(#html <Latex content={$src} />)

#latex "\\mbox{This is a latex string: }\\forall x \\in \\mathbb{N}, x\\ge 0,\\, \\mbox{ rendered as SVG.}"
30 changes: 30 additions & 0 deletions widget/src/exprPresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@ import { RpcContext, RpcSessionAtPos, RpcPtr, Name, DocumentPosition, mapRpcErro
import HtmlDisplay, { Html } from './htmlDisplay'
import InteractiveExpr from './interactiveExpr'

import { mathjax } from 'mathjax-full/js/mathjax'
import { TeX } from 'mathjax-full/js/input/tex'
import { SVG } from 'mathjax-full/js/output/svg'
import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages'
import { liteAdaptor } from 'mathjax-full/js/adaptors/liteAdaptor'
import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html'

const adaptor = liteAdaptor()
RegisterHTMLHandler(adaptor)

const mathjax_document = mathjax.document('', {
InputJax: new TeX({ packages: AllPackages }),
OutputJax: new SVG({ fontCache: 'local' })
})

const mathjax_options = {
em: 16,
ex: 8,
containerWidth: 1280
}

function get_mathjax_svg(math: string): string {
const node = mathjax_document.convert(math, mathjax_options)
return adaptor.outerHTML(node)
}

export function RenderLatexSvg({content}: {content: string}): JSX.Element {
return <div dangerouslySetInnerHTML={{ __html: get_mathjax_svg(content) }} />
}

type ExprWithCtx = RpcPtr<'ProofWidgets.ExprWithCtx'>

interface ExprPresentationData {
Expand Down
5 changes: 5 additions & 0 deletions widget/src/latexToSvg.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RenderLatexSvg } from "./exprPresentation";

export default function ({content}: {content: string}) {
return <RenderLatexSvg content={content} />
}