Using the same solution as in the LaTeX reader:
equation -> displaymath
align -> displaymath \begin{aligned} ... \end{aligned}
etc..
This commit is contained in:
Yuchen Pei 2017-07-12 11:19:49 -04:00 committed by John MacFarlane
parent de117fbd9e
commit 8b502dd50f
3 changed files with 44 additions and 10 deletions

View file

@ -75,7 +75,8 @@ import qualified Text.Pandoc.Builder
as B (headerWith, str, space, strong, emph, strikeout, code, link, image,
spanWith, para, horizontalRule, blockQuote, bulletList, plain,
orderedList, simpleTable, softbreak, codeBlockWith, imageWith, divWith,
setMeta, definitionList, superscript, subscript)
setMeta, definitionList, superscript, subscript, displayMath,
math)
import Text.Pandoc.Class (PandocMonad(..))
import Text.Pandoc.Definition (Pandoc(..), Inline(Space),
Block(BulletList, OrderedList), Attr, nullMeta, Meta, ListNumberStyle(..),
@ -265,13 +266,32 @@ displayMath :: PandocMonad m => VwParser m Blocks
displayMath = try $ do
many spaceChar >> string "{{$"
mathTag <- option "" mathTagParser
many space
contents <- manyTill anyChar (try (char '\n' >> many spaceChar >> string "}}$"
>> many spaceChar >> newline))
let contentsWithTags
| mathTag == "" = "\\[" ++ contents ++ "\n\\]"
| otherwise = "\\begin{" ++ mathTag ++ "}" ++ contents
| mathTag == "" = contents
| otherwise = "\\begin{" ++ mathTag ++ "}\n" ++ contents
++ "\n\\end{" ++ mathTag ++ "}"
return $ B.plain $ B.str contentsWithTags
return $ B.para $ B.displayMath contentsWithTags
mathTagLaTeX :: String -> String
mathTagLaTeX s = case s of
"equation" -> ""
"equation*" -> ""
"gather" -> "gathered"
"gather*" -> "gathered"
"multline" -> "gathered"
"multline*" -> "gathered"
"eqnarray" -> "aligned"
"eqnarray*" -> "aligned"
"align" -> "aligned"
"align*" -> "aligned"
"alignat" -> "aligned"
"alignat*" -> "aligned"
_ -> s
mixedList :: PandocMonad m => VwParser m Blocks
mixedList = try $ do
@ -598,7 +618,7 @@ inlineMath :: PandocMonad m => VwParser m Inlines
inlineMath = try $ do
char '$'
contents <- many1Till (noneOf "\n") (char '$')
return $ B.str $ "\\(" ++ contents ++ "\\)"
return $ B.math contents
tag :: PandocMonad m => VwParser m Inlines
tag = try $ do
@ -650,4 +670,4 @@ mathTagParser = do
s <- try $ lookAhead (char '%' >> (manyTill (noneOf spaceChars)
(try $ char '%' >> many (noneOf $ '%':spaceChars) >> space)))
char '%' >> string s >> char '%'
return s
return $ mathTagLaTeX s

View file

@ -81,7 +81,7 @@ Pandoc (Meta {unMeta = fromList [("date",MetaInlines [Str "2017-05-01"]),("title
,Header 2 ("lists",[],[]) [Str "lists"]
,OrderedList (1,DefaultStyle,DefaultDelim)
[[Plain [Str "ordered",Space,Str "list",Space,Str "item",Space,Str "1,",Space,Str "and",Space,Str "here",Space,Str "is",Space,Str "some",Space,Str "math",Space,Str "belonging",Space,Str "to",Space,Str "list",Space,Str "item",Space,Str "1"]
,Plain [Str "\\[\n a^2 + b^2 = c^2\n\\]"]
,Para [Math DisplayMath "a^2 + b^2 = c^2"]
,Plain [Str "and",Space,Str "some",Space,Str "preformatted",Space,Str "and",Space,Str "tables",Space,Str "belonging",Space,Str "to",Space,Str "item",Space,Str "1",Space,Str "as",Space,Str "well"]
,CodeBlock ("",[],[]) "I'm part of item 1."
,Table [] [AlignDefault,AlignDefault] [0.0,0.0]
@ -184,9 +184,13 @@ Pandoc (Meta {unMeta = fromList [("date",MetaInlines [Str "2017-05-01"]),("title
,[Plain [Str "b"]]]]]]]
,[Plain [Span ("",["done4"],[]) [],Str "task",Space,Str "2"]]]
,Header 2 ("math",[],[]) [Str "math"]
,Para [Str "\\( \\sum_i a_i^2 = 1 \\)"]
,Plain [Str "\\[\n\\sum_i a_i^2\n=\n1\n\\]"]
,Plain [Str "\\begin{align}\n\\sum_i a_i^2 &= 1 + 1 \\\\\n&= 2.\n\\end{align}"]
,Para [Math InlineMath " \\sum_i a_i^2 = 1 "]
,Para [Math DisplayMath "\\sum_i a_i^2\n=\n1"]
,Para [Math DisplayMath "\\begin{aligned}\n\\sum_i a_i^2 &= 1 + 1 \\\\\n&= 2.\n\\end{aligned}"]
,Para [Str "edge",Space,Str "case",Space,Str "(the",Space,Code ("",[],[]) "c^2 + ",Space,Str "after",Space,Str "the",Space,Str "multline",Space,Str "tag",Space,Str "is",Space,Str "in",Space,Str "the",Space,Str "equation):"]
,Para [Math DisplayMath "\\begin{gathered}\nc^2 + \na^2 + b^2\n\\end{gathered}"]
,Para [Str "edge",Space,Str "case",Space,Str "(the",Space,Str "tag",Space,Str "is",Space,Code ("",[],[]) "hello%bye",Str ")"]
,Para [Math DisplayMath "\\begin{hello%bye}\n\\int_a^b f(x) dx\n\\end{hello%bye}"]
,Para [Str "Just",Space,Str "two",Space,Str "dollar",Space,Str "signs:",Space,Str "$$"]
,Para [Str "[not",Space,Str "math]",Space,Str "You",Space,Str "have",Space,Str "$1",SoftBreak,Str "and",Space,Str "I",Space,Str "have",Space,Str "$1."]
,Header 2 ("tags",[],[]) [Str "tags"]

View file

@ -297,6 +297,16 @@ $ \sum_i a_i^2 = 1 $
&= 2.
}}$
edge case (the `c^2 + ` after the multline tag is in the equation):
{{$%multline%c^2 +
a^2 + b^2
}}$
edge case (the tag is `hello%bye`)
{{$%hello%bye%
\int_a^b f(x) dx
}}$
Just two dollar signs: $$
[not math] You have $1