git.iko.soy/typeable/pandoc
on master help sign in
Support for math in RST reader and writer.

Inline math uses the :math:`...` construct. Display math uses .. math:: ... or if multilin .. math:: ... These seem to be supported now by rst2latex.py.

commit3cf60c73061f247b531da4b3c18664c6134bee53 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 31 Dec 2011, 11:40 (15 years ago) parentc264dc4
5 files changed+56−16
src/Text/Pandoc/Readers/RST.hs+5−4 src/Text/Pandoc/Writers/RST.hs+5−3 tests/rst-reader.native+8−1 tests/rst-reader.rst+29 tests/writer.rst+9−8
src/Text/Pandoc/Readers/RST.hs+5−4
@@ -363,18 +363,18 @@ customCodeBlock = try $ do
363363
364364-- | The 'math' directive (from Sphinx) for display math.
365365mathBlock :: GenParser Char st Block
366−mathBlock = mathBlockMultiline <|> mathBlockOneLine
366+mathBlock = try $ do
367+ string ".. math::"
368+ mathBlockMultiline <|> mathBlockOneLine
367369
368370mathBlockOneLine :: GenParser Char st Block
369371mathBlockOneLine = try $ do
370− string ".. math:"
371372 result <- manyTill anyChar newline
372373 blanklines
373374 return $ Para [Math DisplayMath $ removeLeadingTrailingSpace result]
374375
375376mathBlockMultiline :: GenParser Char st Block
376377mathBlockMultiline = try $ do
377− string ".. math::"
378378 blanklines
379379 result <- indentedBlock
380380 -- a single block can contain multiple equations, which need to go
@@ -384,7 +384,8 @@ mathBlockMultiline = try $ do
384384 let startsWithColon (':':_) = True
385385 startsWithColon _ = False
386386 let lns' = dropWhile startsWithColon lns
387− let eqs = map unwords $ filter (not . null) $ splitBy null lns'
387+ let eqs = map (removeLeadingTrailingSpace . unlines)
388+ $ filter (not . null) $ splitBy null lns'
388389 return $ Para $ map (Math DisplayMath) eqs
389390
390391lhsCodeBlock :: GenParser Char ParserState Block
src/Text/Pandoc/Writers/RST.hs+5−3
@@ -286,9 +286,11 @@ inlineToRST (Str str) = return $ text $ escapeString str
286286inlineToRST (Math t str) = do
287287 modify $ \st -> st{ stHasMath = True }
288288 return $ if t == InlineMath
289− then ":math:`" <> text str <> "`\\ "
290− else blankline $$ ".. math::" $$ blankline $$
291− nest 3 (text str) $$ blankline
289+ then ":math:`" <> text str <> "`" <> beforeNonBlank "\\ "
290+ else if '\n' `elem` str
291+ then blankline $$ ".. math::" $$
292+ blankline $$ nest 3 (text str) $$ blankline
293+ else blankline $$ (".. math:: " <> text str) $$ blankline
292294inlineToRST (RawInline _ _) = return empty
293295inlineToRST (LineBreak) = return cr -- there's no line break in RST
294296inlineToRST Space = return space
tests/rst-reader.native+8−1
@@ -305,4 +305,11 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
305305,Para [Note [Para [Str "Note",Space,Str "with",Space,Str "continuation",Space,Str "line",Str "."]]]
306306,Para [Note [Para [Str "Note",Space,Str "with"],Para [Str "continuation",Space,Str "block",Str "."]]]
307307,Para [Note [Para [Str "Note",Space,Str "with",Space,Str "continuation",Space,Str "line"],Para [Str "and",Space,Str "a",Space,Str "second",Space,Str "para",Str "."]]]
308−,Para [Str "Not",Space,Str "in",Space,Str "note",Str "."]]
308+,Para [Str "Not",Space,Str "in",Space,Str "note",Str "."]
309+,Header 1 [Str "Math"]
310+,Para [Str "Some",Space,Str "inline",Space,Str "math",Space,Math InlineMath "E=mc^2",Str ".",Space,Str "Now",Space,Str "some",Space,Str "display",Space,Str "math",Str ":"]
311+,Para [Math DisplayMath "E=mc^2"]
312+,Para [Math DisplayMath "E = mc^2"]
313+,Para [Math DisplayMath "E = mc^2",Math DisplayMath "\\alpha = \\beta"]
314+,Para [Math DisplayMath "E &= mc^2\\\\\nF &= \\pi E",Math DisplayMath "F &= \\gamma \\alpha^2"]
315+,Para [Str "All",Space,Str "done",Str "."]]
tests/rst-reader.rst+29
@@ -536,3 +536,32 @@ Footnotes
536536
537537Not in note.
538538
539+Math
540+====
541+
542+Some inline math :math:`E=mc^2`\ . Now some
543+display math:
544+
545+.. math:: E=mc^2
546+
547+.. math::
548+
549+ E = mc^2
550+
551+.. math::
552+
553+ E = mc^2
554+
555+ \alpha = \beta
556+
557+.. math::
558+ :label hithere
559+ :nowrap
560+
561+ E &= mc^2\\
562+ F &= \pi E
563+
564+ F &= \gamma \alpha^2
565+
566+All done.
567+
tests/writer.rst+9−8
@@ -598,15 +598,16 @@ LaTeX
598598=====
599599
600600-
601−- :math:`$2+2=4$`
602−- :math:`$x \in y$`
603−- :math:`$\alpha \wedge \omega$`
604−- :math:`$223$`
605−- :math:`$p$`-Tree
601+- :math:`2+2=4`
602+- :math:`x \in y`
603+- :math:`\alpha \wedge \omega`
604+- :math:`223`
605+- :math:`p`\ -Tree
606606- Here’s some display math:
607− :math:`$$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$$`
608−- Here’s one that has a line break in it:
609− :math:`$\alpha + \omega \times x^2$`.
607+
608+ .. math:: \frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}
609+
610+- Here’s one that has a line break in it: :math:`\alpha + \omega \times x^2`\ .
610611
611612These shouldn’t be math:
612613