{-# LANGUAGE GeneralizedNewtypeDeriving, ViewPatterns, GADTs, OverloadedStrings #-}
module Text.TeXMath.Writers.TeX (writeTeX, writeTeXWith, addLaTeXEnvironment) where
import Text.TeXMath.Types
import Text.TeXMath.Unicode.ToTeX (getTeXMath)
import Text.TeXMath.Unicode.ToUnicode (fromUnicode)
import qualified Text.TeXMath.Shared as S
import qualified Data.Text as T
import Data.Generics (everywhere, mkT)
import Data.Semigroup ((<>))
import Control.Applicative ((<$>), Applicative)
import Control.Monad (when, unless, foldM_)
import Control.Monad.Reader (MonadReader, runReader, Reader, asks, local)
import Control.Monad.Writer( MonadWriter, WriterT,
execWriterT, tell, censor)
import Text.TeXMath.TeX
writeTeX :: [Exp] -> T.Text
writeTeX :: [Exp] -> Text
writeTeX = Env -> [Exp] -> Text
writeTeXWith Env
defaultEnv
addLaTeXEnvironment :: DisplayType -> T.Text -> T.Text
addLaTeXEnvironment :: DisplayType -> Text -> Text
addLaTeXEnvironment dt :: DisplayType
dt math :: Text
math =
case DisplayType
dt of
DisplayInline -> "\\(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
math Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "\\)"
DisplayBlock -> "\\[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
math Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "\\]"
writeTeXWith :: Env -> [Exp] -> T.Text
writeTeXWith :: Env -> [Exp] -> Text
writeTeXWith env :: Env
env es :: [Exp]
es = Int -> Text -> Text
T.drop 1 (Text -> Text) -> ([TeX] -> Text) -> [TeX] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.init (Text -> Text) -> ([TeX] -> Text) -> [TeX] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TeX -> Text -> Text) -> Text -> TeX -> Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip TeX -> Text -> Text
renderTeX "" (TeX -> Text) -> ([TeX] -> TeX) -> [TeX] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TeX] -> TeX
Grouped ([TeX] -> Text) -> [TeX] -> Text
forall a b. (a -> b) -> a -> b
$
Env -> Math () -> [TeX]
runExpr Env
env (Math () -> [TeX]) -> Math () -> [TeX]
forall a b. (a -> b) -> a -> b
$
(Exp -> Math ()) -> [Exp] -> Math ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Exp -> Math ()
writeExp ([Exp] -> [Exp]
removeOuterGroup [Exp]
es)
runExpr :: Env -> Math () -> [TeX]
runExpr :: Env -> Math () -> [TeX]
runExpr e :: Env
e m :: Math ()
m = (Reader MathState [TeX] -> MathState -> [TeX])
-> MathState -> Reader MathState [TeX] -> [TeX]
forall a b c. (a -> b -> c) -> b -> a -> c
flip Reader MathState [TeX] -> MathState -> [TeX]
forall r a. Reader r a -> r -> a
runReader (Env -> Bool -> MathState
MathState Env
e Bool
False) (Reader MathState [TeX] -> [TeX])
-> Reader MathState [TeX] -> [TeX]
forall a b. (a -> b) -> a -> b
$ WriterT [TeX] (Reader MathState) () -> Reader MathState [TeX]
forall (m :: * -> *) w a. Monad m => WriterT w m a -> m w
execWriterT (Math () -> WriterT [TeX] (Reader MathState) ()
forall a. Math a -> WriterT [TeX] (Reader MathState) a
runTeXMath Math ()
m)
data MathState = MathState{ MathState -> Env
mathEnv :: Env
, MathState -> Bool
mathConvertible :: Bool
} deriving Int -> MathState -> ShowS
[MathState] -> ShowS
MathState -> String
(Int -> MathState -> ShowS)
-> (MathState -> String)
-> ([MathState] -> ShowS)
-> Show MathState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MathState] -> ShowS
$cshowList :: [MathState] -> ShowS
show :: MathState -> String
$cshow :: MathState -> String
showsPrec :: Int -> MathState -> ShowS
$cshowsPrec :: Int -> MathState -> ShowS
Show
setConvertible :: MathState -> MathState
setConvertible :: MathState -> MathState
setConvertible s :: MathState
s = MathState
s{ mathConvertible :: Bool
mathConvertible = Bool
True }
newtype Math a = Math {Math a -> WriterT [TeX] (Reader MathState) a
runTeXMath :: WriterT [TeX] (Reader MathState) a}
deriving (a -> Math b -> Math a
(a -> b) -> Math a -> Math b
(forall a b. (a -> b) -> Math a -> Math b)
-> (forall a b. a -> Math b -> Math a) -> Functor Math
forall a b. a -> Math b -> Math a
forall a b. (a -> b) -> Math a -> Math b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Math b -> Math a
$c<$ :: forall a b. a -> Math b -> Math a
fmap :: (a -> b) -> Math a -> Math b
$cfmap :: forall a b. (a -> b) -> Math a -> Math b
Functor, Functor Math
a -> Math a
Functor Math =>
(forall a. a -> Math a)
-> (forall a b. Math (a -> b) -> Math a -> Math b)
-> (forall a b c. (a -> b -> c) -> Math a -> Math b -> Math c)
-> (forall a b. Math a -> Math b -> Math b)
-> (forall a b. Math a -> Math b -> Math a)
-> Applicative Math
Math a -> Math b -> Math b
Math a -> Math b -> Math a
Math (a -> b) -> Math a -> Math b
(a -> b -> c) -> Math a -> Math b -> Math c
forall a. a -> Math a
forall a b. Math a -> Math b -> Math a
forall a b. Math a -> Math b -> Math b
forall a b. Math (a -> b) -> Math a -> Math b
forall a b c. (a -> b -> c) -> Math a -> Math b -> Math c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: Math a -> Math b -> Math a
$c<* :: forall a b. Math a -> Math b -> Math a
*> :: Math a -> Math b -> Math b
$c*> :: forall a b. Math a -> Math b -> Math b
liftA2 :: (a -> b -> c) -> Math a -> Math b -> Math c
$cliftA2 :: forall a b c. (a -> b -> c) -> Math a -> Math b -> Math c
<*> :: Math (a -> b) -> Math a -> Math b
$c<*> :: forall a b. Math (a -> b) -> Math a -> Math b
pure :: a -> Math a
$cpure :: forall a. a -> Math a
$cp1Applicative :: Functor Math
Applicative, Applicative Math
a -> Math a
Applicative Math =>
(forall a b. Math a -> (a -> Math b) -> Math b)
-> (forall a b. Math a -> Math b -> Math b)
-> (forall a. a -> Math a)
-> Monad Math
Math a -> (a -> Math b) -> Math b
Math a -> Math b -> Math b
forall a. a -> Math a
forall a b. Math a -> Math b -> Math b
forall a b. Math a -> (a -> Math b) -> Math b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> Math a
$creturn :: forall a. a -> Math a
>> :: Math a -> Math b -> Math b
$c>> :: forall a b. Math a -> Math b -> Math b
>>= :: Math a -> (a -> Math b) -> Math b
$c>>= :: forall a b. Math a -> (a -> Math b) -> Math b
$cp1Monad :: Applicative Math
Monad, MonadReader MathState
, MonadWriter [TeX])
getTeXMathM :: T.Text -> Math [TeX]
getTeXMathM :: Text -> Math [TeX]
getTeXMathM s :: Text
s = Text -> Env -> [TeX]
getTeXMath Text
s (Env -> [TeX]) -> Math Env -> Math [TeX]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
tellGroup :: Math () -> Math ()
tellGroup :: Math () -> Math ()
tellGroup = ([TeX] -> [TeX]) -> Math () -> Math ()
forall w (m :: * -> *) a. MonadWriter w m => (w -> w) -> m a -> m a
censor ((TeX -> [TeX] -> [TeX]
forall a. a -> [a] -> [a]
:[]) (TeX -> [TeX]) -> ([TeX] -> TeX) -> [TeX] -> [TeX]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TeX] -> TeX
Grouped)
tellGenFrac :: T.Text -> T.Text -> Math ()
tellGenFrac :: Text -> Text -> Math ()
tellGenFrac open :: Text
open close :: Text
close =
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [ Text -> TeX
ControlSeq "\\genfrac"
, [TeX] -> TeX
Grouped [Text -> TeX
Literal Text
open]
, [TeX] -> TeX
Grouped [Text -> TeX
Literal Text
close]
, [TeX] -> TeX
Grouped [Text -> TeX
Literal "0pt"]
, [TeX] -> TeX
Grouped [] ]
writeBinom :: T.Text -> Exp -> Exp -> Math ()
writeBinom :: Text -> Exp -> Exp -> Math ()
writeBinom cmd :: Text
cmd x :: Exp
x y :: Exp
y = do
Env
env <- (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
if "amsmath" Text -> Env -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Env
env
then do
case Text
cmd of
"\\choose" -> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\binom"]
"\\brack" -> Text -> Text -> Math ()
tellGenFrac "[" "]"
"\\brace" -> Text -> Text -> Math ()
tellGenFrac "\\{" "\\}"
"\\bangle" -> Text -> Text -> Math ()
tellGenFrac "\\langle" "\\rangle"
_ -> String -> Math ()
forall a. HasCallStack => String -> a
error "writeBinom: unknown cmd"
Math () -> Math ()
tellGroup (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ Exp -> Math ()
writeExp Exp
x
Math () -> Math ()
tellGroup (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ Exp -> Math ()
writeExp Exp
y
else Math () -> Math ()
tellGroup (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ do
Exp -> Math ()
writeExp Exp
x
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq Text
cmd]
Exp -> Math ()
writeExp Exp
y
writeExp :: Exp -> Math ()
writeExp :: Exp -> Math ()
writeExp (ENumber s :: Text
s) = [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell ([TeX] -> Math ()) -> Math [TeX] -> Math ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> Math [TeX]
getTeXMathM Text
s
writeExp (EGrouped es :: [Exp]
es) = Math () -> Math ()
tellGroup ((Exp -> Math ()) -> [Exp] -> Math ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Exp -> Math ()
writeExp [Exp]
es)
writeExp (EDelimited "(" ")" [Right (EFraction NoLineFrac x :: Exp
x y :: Exp
y)]) =
Text -> Exp -> Exp -> Math ()
writeBinom "\\choose" Exp
x Exp
y
writeExp (EDelimited "[" "]" [Right (EFraction NoLineFrac x :: Exp
x y :: Exp
y)]) = do
Text -> Exp -> Exp -> Math ()
writeBinom "\\brack" Exp
x Exp
y
writeExp (EDelimited "{" "}" [Right (EFraction NoLineFrac x :: Exp
x y :: Exp
y)]) = do
Text -> Exp -> Exp -> Math ()
writeBinom "\\brace" Exp
x Exp
y
writeExp (EDelimited "\x27E8" "\x27E9" [Right (EFraction NoLineFrac x :: Exp
x y :: Exp
y)]) = do
Text -> Exp -> Exp -> Math ()
writeBinom "\\bangle" Exp
x Exp
y
writeExp (EDelimited open :: Text
open close :: Text
close [Right (EFraction NoLineFrac x :: Exp
x y :: Exp
y)]) = do
Exp -> Math ()
writeExp (Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
open Text
close [Exp -> InEDelimited
forall a b. b -> Either a b
Right ([Alignment] -> [ArrayLine] -> Exp
EArray [Alignment
AlignCenter]
[[[Exp
x]],[[Exp
y]]])])
writeExp (EDelimited open :: Text
open close :: Text
close [Right (EArray aligns :: [Alignment]
aligns rows :: [ArrayLine]
rows)]) = do
Env
env <- (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
case ("amsmath" Text -> Env -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Env
env, Text
open, Text
close) of
(True, "{", "") | [Alignment]
aligns [Alignment] -> [Alignment] -> Bool
forall a. Eq a => a -> a -> Bool
== [Alignment
AlignLeft, Alignment
AlignLeft] ->
Text -> [Alignment] -> [ArrayLine] -> Math ()
table "cases" [] [ArrayLine]
rows
(True, "(", ")") | (Alignment -> Bool) -> [Alignment] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Alignment -> Alignment -> Bool
forall a. Eq a => a -> a -> Bool
== Alignment
AlignCenter) [Alignment]
aligns ->
Text -> [Alignment] -> [ArrayLine] -> Math ()
table "pmatrix" [] [ArrayLine]
rows
(True, "[", "]") | (Alignment -> Bool) -> [Alignment] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Alignment -> Alignment -> Bool
forall a. Eq a => a -> a -> Bool
== Alignment
AlignCenter) [Alignment]
aligns ->
Text -> [Alignment] -> [ArrayLine] -> Math ()
table "bmatrix" [] [ArrayLine]
rows
(True, "{", "}") | (Alignment -> Bool) -> [Alignment] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Alignment -> Alignment -> Bool
forall a. Eq a => a -> a -> Bool
== Alignment
AlignCenter) [Alignment]
aligns ->
Text -> [Alignment] -> [ArrayLine] -> Math ()
table "Bmatrix" [] [ArrayLine]
rows
(True, "\x2223", "\x2223") | (Alignment -> Bool) -> [Alignment] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Alignment -> Alignment -> Bool
forall a. Eq a => a -> a -> Bool
== Alignment
AlignCenter) [Alignment]
aligns ->
Text -> [Alignment] -> [ArrayLine] -> Math ()
table "vmatrix" [] [ArrayLine]
rows
(True, "\x2225", "\x2225") | (Alignment -> Bool) -> [Alignment] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Alignment -> Alignment -> Bool
forall a. Eq a => a -> a -> Bool
== Alignment
AlignCenter) [Alignment]
aligns ->
Text -> [Alignment] -> [ArrayLine] -> Math ()
table "Vmatrix" [] [ArrayLine]
rows
_ -> do
FenceType -> Text -> Math ()
writeDelim FenceType
DLeft Text
open
Exp -> Math ()
writeExp ([Alignment] -> [ArrayLine] -> Exp
EArray [Alignment]
aligns [ArrayLine]
rows)
FenceType -> Text -> Math ()
writeDelim FenceType
DRight Text
close
writeExp (EDelimited open :: Text
open close :: Text
close es :: [InEDelimited]
es) = do
FenceType -> Text -> Math ()
writeDelim FenceType
DLeft Text
open
(InEDelimited -> Math ()) -> [InEDelimited] -> Math ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((Text -> Math ()) -> (Exp -> Math ()) -> InEDelimited -> Math ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FenceType -> Text -> Math ()
writeDelim FenceType
DMiddle) Exp -> Math ()
writeExp) [InEDelimited]
es
FenceType -> Text -> Math ()
writeDelim FenceType
DRight Text
close
writeExp (EIdentifier s :: Text
s) = do
[TeX]
math <- Text -> Math [TeX]
getTeXMathM Text
s
case [TeX]
math of
[] -> () -> Math ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
[t :: TeX
t] -> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [TeX
t]
ts :: [TeX]
ts -> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [[TeX] -> TeX
Grouped [TeX]
ts]
writeExp o :: Exp
o@(EMathOperator s :: Text
s) = do
[TeX]
math <- Text -> Math [TeX]
getTeXMathM Text
s
case Exp -> Maybe TeX
S.getOperator Exp
o of
Just op :: TeX
op -> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [TeX
op]
Nothing -> do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\operatorname"]
(MathState -> Bool) -> Math Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Bool
mathConvertible Math Bool -> (Bool -> Math ()) -> Math ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Math () -> Math ()) -> Math () -> Bool -> Math ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Bool -> Math () -> Math ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '*'])
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [[TeX] -> TeX
Grouped [TeX]
math]
writeExp (ESymbol Ord (Text -> String
T.unpack -> [c :: Char
c]))
| Char
c Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ['\x2061'..'\x2064'] = () -> Math ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
writeExp (ESymbol t :: TeXSymbolType
t s :: Text
s) = do
[TeX]
s' <- Text -> Math [TeX]
getTeXMathM Text
s
Bool -> Math () -> Math ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TeXSymbolType
t TeXSymbolType -> TeXSymbolType -> Bool
forall a. Eq a => a -> a -> Bool
== TeXSymbolType
Bin Bool -> Bool -> Bool
|| TeXSymbolType
t TeXSymbolType -> TeXSymbolType -> Bool
forall a. Eq a => a -> a -> Bool
== TeXSymbolType
Rel) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [TeX
Space]
if Text -> Int
T.length Text
s Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 1 Bool -> Bool -> Bool
&& (TeXSymbolType
t TeXSymbolType -> TeXSymbolType -> Bool
forall a. Eq a => a -> a -> Bool
== TeXSymbolType
Bin Bool -> Bool -> Bool
|| TeXSymbolType
t TeXSymbolType -> TeXSymbolType -> Bool
forall a. Eq a => a -> a -> Bool
== TeXSymbolType
Rel Bool -> Bool -> Bool
|| TeXSymbolType
t TeXSymbolType -> TeXSymbolType -> Bool
forall a. Eq a => a -> a -> Bool
== TeXSymbolType
Op)
then [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq ("\\math" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
T.toLower (TeXSymbolType -> Text
forall a. Show a => a -> Text
tshow TeXSymbolType
t)),
[TeX] -> TeX
Grouped [Text -> TeX
ControlSeq "\\text", [TeX] -> TeX
Grouped [TeX]
s']]
else [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [TeX]
s'
Bool -> Math () -> Math ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TeXSymbolType
t TeXSymbolType -> TeXSymbolType -> Bool
forall a. Eq a => a -> a -> Bool
== TeXSymbolType
Bin Bool -> Bool -> Bool
|| TeXSymbolType
t TeXSymbolType -> TeXSymbolType -> Bool
forall a. Eq a => a -> a -> Bool
== TeXSymbolType
Rel) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [TeX
Space]
writeExp (ESpace width :: Rational
width) = do
Env
env <- (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq (Text -> TeX) -> Text -> TeX
forall a b. (a -> b) -> a -> b
$ Bool -> Rational -> Text
getSpaceCommand ("amsmath" Text -> Env -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Env
env) Rational
width]
writeExp (EFraction fractype :: FractionType
fractype e1 :: Exp
e1 e2 :: Exp
e2) = do
let cmd :: Text
cmd = case FractionType
fractype of
NormalFrac -> "\\frac"
DisplayFrac -> "\\dfrac"
InlineFrac -> "\\tfrac"
NoLineFrac -> "\\binom"
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq Text
cmd]
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e1)
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e2)
writeExp (ESub b :: Exp
b e1 :: Exp
e1) = do
(if Exp -> Bool
isFancy Exp
b then Math () -> Math ()
tellGroup else Math () -> Math ()
forall a. a -> a
id) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ Exp -> Math ()
writeExp Exp
b
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '_']
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e1)
writeExp (ESuper b :: Exp
b e1 :: Exp
e1) = do
(if Exp -> Bool
isFancy Exp
b then Math () -> Math ()
tellGroup else Math () -> Math ()
forall a. a -> a
id) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ Exp -> Math ()
writeExp Exp
b
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '^']
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e1)
writeExp (ESubsup b :: Exp
b e1 :: Exp
e1 e2 :: Exp
e2) = do
(if Exp -> Bool
isFancy Exp
b then Math () -> Math ()
tellGroup else Math () -> Math ()
forall a. a -> a
id) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ Exp -> Math ()
writeExp Exp
b
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '_']
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e1)
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '^']
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e2)
writeExp (EOver convertible :: Bool
convertible b :: Exp
b e1 :: Exp
e1) =
Position -> Bool -> Exp -> Exp -> Math ()
writeScript Position
Over Bool
convertible Exp
b Exp
e1
writeExp (EUnder convertible :: Bool
convertible b :: Exp
b e1 :: Exp
e1) =
Position -> Bool -> Exp -> Exp -> Math ()
writeScript Position
Under Bool
convertible Exp
b Exp
e1
writeExp (EUnderover convertible :: Bool
convertible b :: Exp
b e1 :: Exp
e1@(ESymbol Accent _) e2 :: Exp
e2) =
Exp -> Math ()
writeExp (Bool -> Exp -> Exp -> Exp
EUnder Bool
convertible (Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
b Exp
e2) Exp
e1)
writeExp (EUnderover convertible :: Bool
convertible b :: Exp
b e1 :: Exp
e1 e2 :: Exp
e2@(ESymbol Accent _)) =
Exp -> Math ()
writeExp (Bool -> Exp -> Exp -> Exp
EOver Bool
convertible (Bool -> Exp -> Exp -> Exp
EUnder Bool
False Exp
b Exp
e1) Exp
e2)
writeExp (EUnderover convertible :: Bool
convertible b :: Exp
b e1 :: Exp
e1 e2 :: Exp
e2)
| Exp -> Bool
isOperator Exp
b = do
(if Exp -> Bool
isFancy Exp
b then Math () -> Math ()
tellGroup else Math () -> Math ()
forall a. a -> a
id) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$
(if Bool
convertible then (MathState -> MathState) -> Math () -> Math ()
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local MathState -> MathState
setConvertible else Math () -> Math ()
forall a. a -> a
id) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ Exp -> Math ()
writeExp Exp
b
Bool -> Math () -> Math ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
convertible (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\limits"]
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '_']
Math () -> Math ()
tellGroup (Exp -> Math ()
checkSubstack Exp
e1)
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '^']
Math () -> Math ()
tellGroup (Exp -> Math ()
checkSubstack Exp
e2)
| Bool
otherwise = Exp -> Math ()
writeExp (Bool -> Exp -> Exp -> Exp
EUnder Bool
convertible (Bool -> Exp -> Exp -> Exp
EOver Bool
convertible Exp
b Exp
e2) Exp
e1)
writeExp (ESqrt e :: Exp
e) = do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\sqrt"]
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e)
writeExp (ERoot i :: Exp
i e :: Exp
e) = do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\sqrt"]
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '[']
Exp -> Math ()
writeExp Exp
i
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token ']']
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e)
writeExp (EPhantom e :: Exp
e) = do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\phantom"]
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e)
writeExp (EBoxed e :: Exp
e) = do
Env
env <- (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
if "amsmath" Text -> Env -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Env
env
then do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\boxed"]
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e)
else Exp -> Math ()
writeExp Exp
e
writeExp (EScaled size :: Rational
size e :: Exp
e)
| case Exp
e of
(ESymbol Open _) -> Bool
True
(ESymbol Close _) -> Bool
True
_ -> Bool
False = do
case Rational -> Maybe Text
S.getScalerCommand Rational
size of
Just s :: Text
s -> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq Text
s]
Nothing -> () -> Math ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Exp -> Math ()
writeExp Exp
e
| Bool
otherwise = Exp -> Math ()
writeExp Exp
e
writeExp (EText ttype :: TextType
ttype s :: Text
s) = do
let txtcmd :: TeX -> [TeX]
txtcmd = TextType -> TeX -> [TeX]
getTextCommand TextType
ttype
case (Char -> TeX) -> String -> [TeX]
forall a b. (a -> b) -> [a] -> [b]
map Char -> TeX
escapeLaTeX (Text -> String
T.unpack Text
s) of
[] -> () -> Math ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
xs :: [TeX]
xs -> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell ([TeX] -> Math ()) -> [TeX] -> Math ()
forall a b. (a -> b) -> a -> b
$ TeX -> [TeX]
txtcmd ([TeX] -> TeX
Grouped [TeX]
xs)
writeExp (EStyled ttype :: TextType
ttype es :: [Exp]
es) = do
Text
txtcmd <- ((Env -> TextType -> Text) -> TextType -> Env -> Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip Env -> TextType -> Text
S.getLaTeXTextCommand TextType
ttype) (Env -> Text) -> Math Env -> Math Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq Text
txtcmd]
Math () -> Math ()
tellGroup ((Exp -> Math ()) -> [Exp] -> Math ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Exp -> Math ()
writeExp ([Exp] -> Math ()) -> [Exp] -> Math ()
forall a b. (a -> b) -> a -> b
$ (forall a. Data a => a -> a) -> [Exp] -> [Exp]
(forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere ((Text -> Text) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT (TextType -> Text -> Text
fromUnicode TextType
ttype)) [Exp]
es)
writeExp (EArray [AlignRight, AlignLeft] rows :: [ArrayLine]
rows) = do
Env
env <- (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
if "amsmath" Text -> Env -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Env
env
then Text -> [Alignment] -> [ArrayLine] -> Math ()
table "aligned" [] [ArrayLine]
rows
else Text -> [Alignment] -> [ArrayLine] -> Math ()
table "array" [Alignment
AlignRight, Alignment
AlignLeft] [ArrayLine]
rows
writeExp (EArray aligns :: [Alignment]
aligns rows :: [ArrayLine]
rows) = do
Env
env <- (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
if "amsmath" Text -> Env -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Env
env Bool -> Bool -> Bool
&& (Alignment -> Bool) -> [Alignment] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Alignment -> Alignment -> Bool
forall a. Eq a => a -> a -> Bool
== Alignment
AlignCenter) [Alignment]
aligns
then Text -> [Alignment] -> [ArrayLine] -> Math ()
table "matrix" [] [ArrayLine]
rows
else Text -> [Alignment] -> [ArrayLine] -> Math ()
table "array" [Alignment]
aligns [ArrayLine]
rows
table :: T.Text -> [Alignment] -> [ArrayLine] -> Math ()
table :: Text -> [Alignment] -> [ArrayLine] -> Math ()
table name :: Text
name aligns :: [Alignment]
aligns rows :: [ArrayLine]
rows = do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\begin", [TeX] -> TeX
Grouped [Text -> TeX
Literal Text
name]]
Bool -> Math () -> Math ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Alignment] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Alignment]
aligns) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [[TeX] -> TeX
Grouped [Text -> TeX
Literal Text
columnAligns]]
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token '\n']
(ArrayLine -> Math ()) -> [ArrayLine] -> Math ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ArrayLine -> Math ()
row [ArrayLine]
rows
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\end", [TeX] -> TeX
Grouped [Text -> TeX
Literal Text
name]]
where
columnAligns :: Text
columnAligns = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ (Alignment -> Char) -> [Alignment] -> String
forall a b. (a -> b) -> [a] -> [b]
map Alignment -> Char
alignmentToLetter [Alignment]
aligns
alignmentToLetter :: Alignment -> Char
alignmentToLetter AlignLeft = 'l'
alignmentToLetter AlignCenter = 'c'
alignmentToLetter AlignRight = 'r'
row :: ArrayLine -> Math ()
row :: ArrayLine -> Math ()
row [] = [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [TeX
Space, Text -> TeX
Literal "\\\\", Char -> TeX
Token '\n']
row [c :: [Exp]
c] = [Exp] -> Math ()
cell [Exp]
c Math () -> Math () -> Math ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ArrayLine -> Math ()
row []
row (c :: [Exp]
c:cs :: ArrayLine
cs) = [Exp] -> Math ()
cell [Exp]
c Math () -> Math () -> Math ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [TeX
Space, Char -> TeX
Token '&', TeX
Space] Math () -> Math () -> Math ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ArrayLine -> Math ()
row ArrayLine
cs
cell :: [Exp] -> Math ()
cell :: [Exp] -> Math ()
cell = (Exp -> Math ()) -> [Exp] -> Math ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Exp -> Math ()
writeExp
data FenceType = DLeft | DMiddle | DRight
type Delim = T.Text
writeDelim :: FenceType -> Delim -> Math ()
writeDelim :: FenceType -> Text -> Math ()
writeDelim fence :: FenceType
fence delim :: Text
delim = do
[TeX]
tex <- Text -> Math [TeX]
getTeXMathM Text
delim
Bool
valid <- [TeX] -> [[TeX]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem [TeX]
tex ([[TeX]] -> Bool) -> Math [[TeX]] -> Math Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Math [[TeX]]
delimiters
[TeX]
nullLim <- Text -> Math [TeX]
getTeXMathM "."
let delimCmd :: [TeX]
delimCmd = if Bool
valid then [TeX]
tex else [TeX]
nullLim
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell ([TeX] -> Math ()) -> [TeX] -> Math ()
forall a b. (a -> b) -> a -> b
$ case FenceType
fence of
DLeft -> [Text -> TeX
ControlSeq "\\left"] [TeX] -> [TeX] -> [TeX]
forall a. [a] -> [a] -> [a]
++ [TeX]
delimCmd [TeX] -> [TeX] -> [TeX]
forall a. [a] -> [a] -> [a]
++ [TeX
Space] [TeX] -> [TeX] -> [TeX]
forall a. [a] -> [a] -> [a]
++ if Bool
valid then [] else [TeX]
tex
DMiddle -> case Bool
valid of
True -> [TeX
Space] [TeX] -> [TeX] -> [TeX]
forall a. [a] -> [a] -> [a]
++ [Text -> TeX
ControlSeq "\\middle"] [TeX] -> [TeX] -> [TeX]
forall a. [a] -> [a] -> [a]
++ [TeX]
tex [TeX] -> [TeX] -> [TeX]
forall a. [a] -> [a] -> [a]
++ [TeX
Space]
False -> [TeX]
tex
DRight -> [TeX
Space, Text -> TeX
ControlSeq "\\right"] [TeX] -> [TeX] -> [TeX]
forall a. [a] -> [a] -> [a]
++ [TeX]
delimCmd [TeX] -> [TeX] -> [TeX]
forall a. [a] -> [a] -> [a]
++ if Bool
valid then [] else [TeX]
tex
writeScript :: Position -> Bool -> Exp -> Exp -> Math ()
writeScript :: Position -> Bool -> Exp -> Exp -> Math ()
writeScript pos :: Position
pos convertible :: Bool
convertible b :: Exp
b e1 :: Exp
e1 = do
let diacmd :: Maybe Text
diacmd = case Exp
e1 of
ESymbol stype :: TeXSymbolType
stype a :: Text
a
| TeXSymbolType
stype TeXSymbolType -> [TeXSymbolType] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [TeXSymbolType
Accent, TeXSymbolType
TOver, TeXSymbolType
TUnder]
-> Position -> Text -> Maybe Text
S.getDiacriticalCommand Position
pos Text
a
_ -> Maybe Text
forall a. Maybe a
Nothing
case Maybe Text
diacmd of
Just cmd :: Text
cmd -> do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq Text
cmd]
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
b)
Nothing
| Exp -> Bool
isOperator Exp
b -> do
(if Exp -> Bool
isFancy Exp
b then Math () -> Math ()
tellGroup else Math () -> Math ()
forall a. a -> a
id) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$
(if Bool
convertible then (MathState -> MathState) -> Math () -> Math ()
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local MathState -> MathState
setConvertible else Math () -> Math ()
forall a. a -> a
id) (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ Exp -> Math ()
writeExp Exp
b
Bool -> Math () -> Math ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
convertible (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\limits"]
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Char -> TeX
Token (Char -> TeX) -> Char -> TeX
forall a b. (a -> b) -> a -> b
$ case Position
pos of { Over -> '^'; Under -> '_' }]
Math () -> Math ()
tellGroup (Exp -> Math ()
checkSubstack Exp
e1)
| case Position
pos of {Over -> Bool
True; _ -> Bool
False}
, Exp
e1 Exp -> Exp -> Bool
forall a. Eq a => a -> a -> Bool
== TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Accent "\831" -> do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\overline", Text -> TeX
Literal "{",
Text -> TeX
ControlSeq "\\overline"]
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
b)
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
Literal "}"]
| Bool
otherwise -> do
case Position
pos of
Over -> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\overset"]
Under -> [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\underset"]
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
e1)
Math () -> Math ()
tellGroup (Exp -> Math ()
writeExp Exp
b)
checkSubstack :: Exp -> Math ()
checkSubstack :: Exp -> Math ()
checkSubstack e :: Exp
e@(EArray [AlignCenter] rows :: [ArrayLine]
rows) = do
Env
env <- (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
if "amsmath" Text -> Env -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Env
env
then do
[TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Text -> TeX
ControlSeq "\\substack"]
Math () -> Math ()
tellGroup (Math () -> Math ()) -> Math () -> Math ()
forall a b. (a -> b) -> a -> b
$ (Bool -> ArrayLine -> Math Bool) -> Bool -> [ArrayLine] -> Math ()
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m ()
foldM_ (\first :: Bool
first r :: ArrayLine
r -> do
if Bool
first
then () -> Math ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else [TeX] -> Math ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [TeX
Space, Text -> TeX
Literal "\\\\", TeX
Space]
([Exp] -> Math ()) -> ArrayLine -> Math ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((Exp -> Math ()) -> [Exp] -> Math ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Exp -> Math ()
writeExp ([Exp] -> Math ()) -> ([Exp] -> [Exp]) -> [Exp] -> Math ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Exp] -> [Exp]
removeOuterGroup) ArrayLine
r
Bool -> Math Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False) Bool
True [ArrayLine]
rows
else Exp -> Math ()
writeExp Exp
e
checkSubstack e :: Exp
e = Exp -> Math ()
writeExp Exp
e
getSpaceCommand :: Bool -> Rational -> T.Text
getSpaceCommand :: Bool -> Rational -> Text
getSpaceCommand amsmath :: Bool
amsmath width :: Rational
width =
case Rational -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (Rational
width Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* 18) :: Int of
-3 -> "\\!"
0 -> ""
3 -> "\\,"
4 -> "\\ "
5 -> "\\;"
18 -> "\\quad"
36 -> "\\qquad"
n :: Int
n -> if Bool
amsmath
then "\\mspace{" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "mu}"
else "{\\mskip " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "mu}"
getTextCommand :: TextType -> TeX -> [TeX]
getTextCommand :: TextType -> TeX -> [TeX]
getTextCommand tt :: TextType
tt x :: TeX
x =
case TextType
tt of
TextNormal -> [Text -> TeX
ControlSeq "\\text", TeX
x]
TextItalic -> [Text -> TeX
ControlSeq "\\textit", TeX
x]
TextBold -> [Text -> TeX
ControlSeq "\\textbf", TeX
x]
TextMonospace -> [Text -> TeX
ControlSeq "\\texttt", TeX
x]
TextBoldItalic -> [Text -> TeX
ControlSeq "\\textit",
[TeX] -> TeX
Grouped [Text -> TeX
ControlSeq "\\textbf", TeX
x]]
TextSansSerif -> [Text -> TeX
ControlSeq "\\textsf", TeX
x]
TextSansSerifBold -> [Text -> TeX
ControlSeq "\\textbf",
[TeX] -> TeX
Grouped [Text -> TeX
ControlSeq "\\textsf", TeX
x]]
TextSansSerifItalic -> [Text -> TeX
ControlSeq "\\textit",
[TeX] -> TeX
Grouped [Text -> TeX
ControlSeq "\\textsf", TeX
x]]
TextSansSerifBoldItalic -> [Text -> TeX
ControlSeq "\\textbf",
[TeX] -> TeX
Grouped [Text -> TeX
ControlSeq "\\textit",
[TeX] -> TeX
Grouped [Text -> TeX
ControlSeq "\\textsf", TeX
x]]]
_ -> [Text -> TeX
ControlSeq "\\text", TeX
x]
delimiters :: Math [[TeX]]
delimiters :: Math [[TeX]]
delimiters = do
Env
env <- (MathState -> Env) -> Math Env
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks MathState -> Env
mathEnv
let commands' :: Env
commands' = [ ".", "(", ")", "[", "]", "|", "\x2016", "{", "}"
, "\x2309", "\x2308", "\x2329", "\x232A"
, "\x230B", "\x230A", "\x231C", "\x231D"]
[[TeX]] -> Math [[TeX]]
forall (m :: * -> *) a. Monad m => a -> m a
return ([[TeX]] -> Math [[TeX]]) -> [[TeX]] -> Math [[TeX]]
forall a b. (a -> b) -> a -> b
$ ([TeX] -> Bool) -> [[TeX]] -> [[TeX]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([TeX] -> Bool) -> [TeX] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TeX] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) ((Text -> [TeX]) -> Env -> [[TeX]]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Env -> [TeX]) -> Env -> Text -> [TeX]
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Env -> [TeX]
getTeXMath Env
env) Env
commands')
isFancy :: Exp -> Bool
isFancy :: Exp -> Bool
isFancy (ESub _ _) = Bool
True
isFancy (ESuper _ _) = Bool
True
isFancy (ESubsup _ _ _) = Bool
True
isFancy (EOver _ _ _) = Bool
True
isFancy (EUnder _ _ _) = Bool
True
isFancy (EUnderover _ _ _ _) = Bool
True
isFancy (ESqrt _) = Bool
True
isFancy (ERoot _ _) = Bool
True
isFancy (EPhantom _) = Bool
True
isFancy _ = Bool
False
isOperator :: Exp -> Bool
isOperator :: Exp -> Bool
isOperator (EMathOperator _) = Bool
True
isOperator (ESymbol Op _) = Bool
True
isOperator _ = Bool
False
removeOuterGroup :: [Exp] -> [Exp]
removeOuterGroup :: [Exp] -> [Exp]
removeOuterGroup [EGrouped es :: [Exp]
es] = [Exp]
es
removeOuterGroup es :: [Exp]
es = [Exp]
es
tshow :: Show a => a -> T.Text
tshow :: a -> Text
tshow = String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show