add mathChunks/latexCfgChunksTrans

This commit is contained in:
Xie Ruifeng 2020-11-08 18:25:50 +08:00 committed by David Himmelstrup
commit 828b033504

View file

@ -17,6 +17,8 @@ module Reanimate.LaTeX
latexWithHeaders, latexWithHeaders,
latexChunks, latexChunks,
latexCfgChunks, latexCfgChunks,
latexCfgChunksTrans,
mathChunks,
xelatex, xelatex,
xelatexWithHeaders, xelatexWithHeaders,
ctex, ctex,
@ -69,6 +71,9 @@ data TexConfig = TexConfig
} }
deriving (Generic, Hashable, Read, Show, Eq, Ord) deriving (Generic, Hashable, Read, Show, Eq, Ord)
defaultTexConfig :: TexConfig
defaultTexConfig = TexConfig LaTeX [] []
-- | Render TeX script using a given configuration. -- | Render TeX script using a given configuration.
latexCfg :: TexConfig -> T.Text -> SVG latexCfg :: TexConfig -> T.Text -> SVG
latexCfg (TexConfig engine headers postscript) = latexCfg (TexConfig engine headers postscript) =
@ -113,23 +118,33 @@ someTexWithHeaders engine exec dvi args postscript headers tex =
script = mkTexScript exec args headers (T.unlines (postscript ++ [tex])) script = mkTexScript exec args headers (T.unlines (postscript ++ [tex]))
-- | Invoke latex using a given configuration and separate results. -- | Invoke latex using a given configuration and separate results.
latexCfgChunks :: Traversable t => TexConfig -> t T.Text -> t Tree -- Apply the transformation to the LaTeX segments.
latexCfgChunks _cfg chunks | pNoExternals = fmap mkText chunks -- See also 'mathChunks', the transformation is @(\s -> "$" <> s <> "$")@.
latexCfgChunks cfg chunks = worker $ svgGlyphs $ tex $ fold chunks latexCfgChunksTrans :: Traversable t => TexConfig -> (T.Text -> T.Text) -> t T.Text -> t Tree
latexCfgChunksTrans _cfg f chunks | pNoExternals = fmap (mkText . f) chunks
latexCfgChunksTrans cfg f chunks = worker $ svgGlyphs $ tex $ f $ fold chunks
where where
tex = latexCfg cfg tex = latexCfg cfg
merge lst = mkGroup [fmt svg | (fmt, _, svg) <- lst] merge lst = mkGroup [fmt svg | (fmt, _, svg) <- lst]
checkResult (r, []) = r checkResult (r, []) = r
checkResult (_, _) = error "latex chunk mismatch" checkResult (_, _) = error "latex chunk mismatch"
worker = checkResult . runState (mapM (state . workerSingle) chunks) worker = checkResult . runState (mapM (state . workerSingle) (fmap f chunks))
workerSingle x everything = workerSingle x everything =
let width = length $ svgGlyphs (tex x) let width = length $ svgGlyphs (tex x)
(first, rest) = splitAt width everything (first, rest) = splitAt width everything
in (merge first, rest) in (merge first, rest)
-- | Render math formula and separate results.
mathChunks :: Traversable t => t T.Text -> t Tree
mathChunks = latexCfgChunksTrans defaultTexConfig (\s -> "$" <> s <> "$")
-- | Invoke latex using a given configuration and separate results.
latexCfgChunks :: Traversable t => TexConfig -> t T.Text -> t Tree
latexCfgChunks cfg = latexCfgChunksTrans cfg id
-- | Invoke latex and separate results. -- | Invoke latex and separate results.
latexChunks :: Traversable t => t T.Text -> t Tree latexChunks :: Traversable t => t T.Text -> t Tree
latexChunks = latexCfgChunks (TexConfig LaTeX [] []) latexChunks = latexCfgChunksTrans defaultTexConfig id
-- | Invoke xelatex and import the result as an SVG object. SVG objects are -- | Invoke xelatex and import the result as an SVG object. SVG objects are
-- cached to improve performance. Xelatex has support for non-western scripts. -- cached to improve performance. Xelatex has support for non-western scripts.