From 4c8517bd1cdc0b20c9dde2412be22a1775dd6d81 Mon Sep 17 00:00:00 2001 From: Sam Ritchie Date: Fri, 20 Oct 2023 10:47:35 -0600 Subject: [PATCH] Merge --- src/emmy/expression/render.cljc | 50 +++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/emmy/expression/render.cljc b/src/emmy/expression/render.cljc index c44011be..a2e026bc 100644 --- a/src/emmy/expression/render.cljc +++ b/src/emmy/expression/render.cljc @@ -3,8 +3,7 @@ (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] @@ -12,9 +11,7 @@ [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`." @@ -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 @@ -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."