Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie committed Oct 20, 2023
1 parent bfa3ffe commit 4c8517b
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/emmy/expression/render.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
(ns emmy.expression.render
"Functions and utilities for rendering symbolic expressions to various backends
like LaTeX, infix or Javascript."
(:require [clojure.pprint :as pp]
[clojure.set :as set]
(:require [clojure.set :as set]
[clojure.string :as s]
[clojure.zip :as z]
[emmy.generic :as g]
[emmy.pattern.rule :as R :refer [=>]]
[emmy.ratio :as r]
[emmy.simplify.rules :refer [negative-number?]]
[emmy.util :as u]
[emmy.value :as v])
#?(:clj
(:import (clojure.lang IDeref))))
[emmy.value :as v]))

(def ^:private rewrite-trig-powers
"Historical preference is to write `sin^2(x)` rather than `(sin(x))^2`."
Expand Down Expand Up @@ -586,7 +583,32 @@
(brace s))
v))))))))))

(defn ->TeX-str
(deftype TeXWrapper [s]
Object
(toString [_] s)
#?(:clj
(equals [_ b]
(and (instance? TeXWrapper b)
(= s (.-s ^TeXWrapper b)))))

#?@(:cljs
[IEquiv
(-equiv [_ b]
(and (instance? TeXWrapper b)
(= s (.-s ^TeXWrapper b))))

IPrintWithWriter
(-pr-writer [_ writer _]
(write-all writer s))]))

(defn ^:no-doc tex? [x]
(instance? TeXWrapper x))

#?(:clj
(defmethod print-method TeXWrapper [^TeXWrapper f ^java.io.Writer w]
(.write w (.-s f))))

(defn ->TeX
"Convert the given expression to TeX format, as a string.
If you set the `:equation` keyword argument to a truthy value, the result will
Expand Down Expand Up @@ -616,21 +638,7 @@
(str "\\begin{equation}\n"
label tex-string
"\n\\end{equation}"))
tex-string)))

(deftype TexWrapper [s v]
Object
(toString [_] s)

IDeref
(#?(:cljs -deref :clj deref) [_] v))

(defn ->TeX [v & opts]
(TexWrapper. (apply ->TeX-str v opts) v))

#?(:clj
(defmethod print-method TexWrapper [^TexWrapper s ^java.io.Writer w]
(.write w (str "\"" (.toString s) "\""))))
(->TeXWrapper tex-string))))

(def ->JavaScript
"Converts an S-expression to JavaScript."
Expand Down

0 comments on commit 4c8517b

Please sign in to comment.