-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathLatex.hs
63 lines (56 loc) · 1.64 KB
/
Latex.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Latex ( latexExpr ) where
import Expr
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified Data.List as List
type Latex = String
latexAtom :: Atom -> Latex
latexAtom (Var s) = s
latexAtom (App f s) = escape f ++ "(" ++ show s ++ ")"
latexProd :: Prod -> String
latexProd (Prod m) = List.intercalate " " $ map shw $ Map.toList m
where
shw (e,n) = latexAtom e ++ (case n of
1 -> ""
2 -> "^2"
3 -> "^3"
4 -> "^4"
5 -> "^5"
6 -> "^6"
7 -> "^7"
8 -> "^8"
9 -> "^9"
n -> "^{" ++ show n ++ "}")
latexExpr :: Expr -> String
latexExpr (Expr m) =
if Map.null m
then "0"
else List.intercalate " + " . map shw $ Map.toList m
where
shw (p,n) = pre ++ latexProd p
where
pre = if isConstP p
then show n
else case n of
1 -> ""
(-1) -> "-"
_ -> show n
escape :: String -> Latex
escape f = if f `Set.member` latexFunctions then '\\' : f else f
latexFunctions = Set.fromList
[ "sin"
, "cos"
, "tan"
, "asin"
, "acos"
, "atan"
, "sinh"
, "cosh"
, "tanh"
, "asinh"
, "acosh"
, "atanh"
, "exp"
, "sqrt"
, "log"
]