There are obviously already people using jfricas.
I was asked for a feature to allow greek letters and other stuff.
While it is easily possible to do this with UTF-8 encoding like

delta := "𝛿"::Symbol

and show nicely in jfricas with FormatMathJax enabled.
However, right-clicking on the output and exporting the expression as
TeX, gives back a UTF-8 symbol. If you want to include that expression
in a LaTeX document, then one must enable the right encoding. In my
experience you need something like lualatex or xelatex to work reliably
with 8bit characters.

So it makes sense to support LaTeX symbols in the MathJax output for
jFriCAS. Currently the backslash is translated into {\backslash} and
thus shows as a literal backslash in the output.

After this patch a symbol name that starts with \ is directly handed to
the output unaltered so that it can be interpreted by MathJax.

Of course, that feature can be switched on and off, see below.

(2) -> setFormat!(FormatMathJax)$JFriCASSupport;
(3) -> delta := "\delta"::Symbol; NN := "\mathcal{N}"::Symbol;

(4) -> sin(delta+NN)

--FORMAT:BEG:FormatMathJax
\[
\sin\left({\backslash}mathcal\{N\}+{\backslash}delta\right)
\]
--FORMAT:END:FormatMathJax

(5) -> setPrettySymbols!(true)$FormatMathJax;

(6) -> sin(delta+NN)

--FORMAT:BEG:FormatMathJax
\[
\sin\left(\mathcal{N}+\delta\right)
\]
--FORMAT:END:FormatMathJax

Comments are welcome.

If I do not hear something, I will commit it next Tuesday.

https://github.com/hemmecke/fricas/commit/a584d857a623be6be35bd5641cc1a584b22adcad

Ralf

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fricas-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/fricas-devel/499de1e8-d2a9-db80-e2cb-a090daa7d5f9%40hemmecke.org.
From a584d857a623be6be35bd5641cc1a584b22adcad Mon Sep 17 00:00:00 2001
From: Ralf Hemmecke <r...@hemmecke.org>
Date: Sat, 1 May 2021 13:47:16 +0200
Subject: allow latex symbols in FormatMathJax

---
 src/algebra/fmtmathjax.spad | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/src/algebra/fmtmathjax.spad b/src/algebra/fmtmathjax.spad
index 27d0c099..b53b5c1d 100644
--- a/src/algebra/fmtmathjax.spad
+++ b/src/algebra/fmtmathjax.spad
@@ -1,7 +1,7 @@
 -------------------------------------------------------------------
 ---
 --- FriCAS FormatMathJax
---- Copyright (C) 2020  Ralf Hemmecke <r...@hemmecke.org>
+--- Copyright (C) 2020-2021  Ralf Hemmecke <r...@hemmecke.org>
 ---
 -------------------------------------------------------------------
 -- Redistribution and use in source and binary forms, with or without
@@ -102,8 +102,23 @@ FormatMathJax: Exports == Implementation where
       ++ with name \spad{env}. The string \spad{x} is put right after
       ++ the start of the environment.
       ++ The arguments are separated by the string \spad{sep}.
+    setPrettySymbols!: Boolean -> Boolean
+      ++ setPrettySymbols(true) sets the internal state in such a way
+      ++ that a symbol of the for "\abc", i.e. starting with a
+      ++ backslash and only ASCII letters following it ([a-zA-Z]),
+      ++ will not replace the backslash, but rather hands the symbol
+      ++ as is for interpretation by MathJax.
+      ++ setPrettySymbols(false) switches this feature off.
+      ++ The return value is the state before it was set anew.
 
   Implementation ==> add
+    -- domain variable
+    prettySymbols?: Reference Boolean := ref false
+    setPrettySymbols!(b: Boolean): Boolean ==
+        oldState: Boolean := deref prettySymbols?
+        prettySymbols? := ref b
+        oldState
+
     Rep ==> Record(prolog: BOX, fmt: BOX, epilog: BOX)
     import from Rep
     rep x ==> (x@%) pretend Rep
@@ -179,10 +194,17 @@ In order to keep spaces, we also escape spaces by a backslash.
     formatString(s: S): BOX ==
         -- The backslash must be escaped by a second backslash.
         tex1("\texttt", box duplicateBackslashes s)
-    formatSymbol(s: S): BOX == box texEscapeString s
+
+    formatSymbol(s: S): BOX ==
+        deref prettySymbols? and not empty? s and s.1 = char "\" =>
+            box s
+        box texEscapeString s
+
     formatFunctionSymbol(s: S): BOX ==
-        b := box texEscapeString s
-        if one? width b then b else tex1("\operatorname", b)
+        b := formatSymbol s
+        deref prettySymbols? and not empty? b and lines(b).1.1 = char "\" => b
+        one? width b => b
+        tex1("\operatorname", b)
 
     -- If a is of the form (* x (CONCAT d y)) then replace it
     -- by (INTSEP x (CONCAT d y)).
-- 
2.25.1

Reply via email to