From b55ba6665367b6f39e09f5284da7d3da0235cb15 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 27 Sep 2026 00:41:53 +0000 Subject: [PATCH] The Terminal renders an expression's LaTeX in the browser `render x` writes a page with the expression's LaTeX to the temporary directory, loads MathJax from the CDN the notebook extension uses, and opens the page the way a plot is shown. `latex x` already gave the LaTeX as text, and the page shows the source under the typeset form. Where no browser can be started, `render` prints the page's path instead. Closes #549. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../AngouriMath.Terminal.Lib/PreRunCode.fs | 27 +++++++++++++++++++ Sources/Terminal/readme.md | 13 +++++++++ Sources/Tests/TerminalUnitTests/Tests.fs | 17 ++++++++++++ 3 files changed, 57 insertions(+) diff --git a/Sources/Terminal/AngouriMath.Terminal.Lib/PreRunCode.fs b/Sources/Terminal/AngouriMath.Terminal.Lib/PreRunCode.fs index 414b92120..21693fffc 100644 --- a/Sources/Terminal/AngouriMath.Terminal.Lib/PreRunCode.fs +++ b/Sources/Terminal/AngouriMath.Terminal.Lib/PreRunCode.fs @@ -48,6 +48,33 @@ let help () = System.Diagnostics.Process.Start psi $""Sending you to {url}"" +/// The page `render` opens: the expression's LaTeX -- what `latex` gives -- typeset by MathJax, +/// with the LaTeX itself beneath it, written to the temporary directory. Joined with +/// String.concat, since `+` builds an expression here. +/// https://github.com/asc-community/AngouriMath/issues/549 +let renderedPage (x : obj) = + let source = System.Net.WebUtility.HtmlEncode (latex x) + let page = System.IO.Path.Combine (System.IO.Path.GetTempPath (), $""angourimath-{System.Guid.NewGuid ()}.html"") + let html = + String.concat """" [ + ""AngouriMath"" + """" + ""

$$""; source; ""$$

""; source; ""
"" ] + System.IO.File.WriteAllText (page, html) + page + +/// The expression typeset in the browser, as Plot's charts are shown; `latex` gives its LaTeX +/// as text. https://github.com/asc-community/AngouriMath/issues/549 +let render (x : obj) = + let page = renderedPage x + let psi = System.Diagnostics.ProcessStartInfo page + psi.UseShellExecute <- true + try + System.Diagnostics.Process.Start psi |> ignore + printfn $""Showing {page} in the browser"" + with _ -> + printfn $""Wrote {page}, and no browser could be started to show it"" + /// Arithmetic here builds expressions, so `3 / 2` is a rational rather than 1 and `x + 1` is /// an expression rather than an error. The cost is that ordinary F# whose arithmetic must stay /// primitive does not typecheck: `let rec fib n = if n < 2 then n else fib (n-1) + fib (n-2)` diff --git a/Sources/Terminal/readme.md b/Sources/Terminal/readme.md index 989b85331..61a7ec17d 100644 --- a/Sources/Terminal/readme.md +++ b/Sources/Terminal/readme.md @@ -49,3 +49,16 @@ them oldest first, and `back n` counts from the end. A cell with no value — a `let` — adds nothing, and a question about the history is itself a cell with a value, so it is remembered too. + +## LaTeX + +`latex` gives an expression's LaTeX, and `render` shows it typeset in the browser, the way a plot +is shown: a page in the temporary directory, rendered by MathJax, with the LaTeX beneath it. + +``` +[...] latex (x / 2) +\frac{x}{2} + +[...] render (x / 2) +Showing /tmp/angourimath-….html in the browser +``` diff --git a/Sources/Tests/TerminalUnitTests/Tests.fs b/Sources/Tests/TerminalUnitTests/Tests.fs index c63e5f877..3a3e1ffb8 100644 --- a/Sources/Tests/TerminalUnitTests/Tests.fs +++ b/Sources/Tests/TerminalUnitTests/Tests.fs @@ -70,6 +70,23 @@ let ``The values of earlier cells are kept in run`` () = Assert.Equal(LatexSuccess ("11", "11"), execute kernel "back 5") Assert.Equal(PlainTextSuccess "6", execute kernel "run.Count") +// `render` opens a page with the expression typeset; the page is the LaTeX `latex` gives, and +// escaped for HTML where the LaTeX has a character HTML reads. +// https://github.com/asc-community/AngouriMath/issues/549 +[] +let ``The rendered page holds the expression's LaTeX`` () = + let kernel = ``Create kernel or fail`` () + enableAngouriMath kernel |> ignore + let page code = + match execute kernel $"System.IO.File.ReadAllText (renderedPage ({code}))" with + | PlainTextSuccess page -> page + | other -> failwith $"expected the page, and the prompt answered {other}" + Assert.Equal(PlainTextSuccess @"\frac{x}{2}", execute kernel "latex (x / 2)") + let fraction = page "x / 2" + Assert.Contains(@"$$\frac{x}{2}$$", fraction) + Assert.Contains("mathjax@3", fraction) + Assert.Contains("y < 3", page "\"y < 3\"") + [] let ``Test 1`` () = executeNewWithAm "1 + 1"