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.
This commit is contained in:
John MacFarlane 2011-12-31 11:40:47 -08:00
parent c264dc4f5b
commit 3cf60c7306
5 changed files with 56 additions and 16 deletions

View file

@ -363,18 +363,18 @@ customCodeBlock = try $ do
-- | The 'math' directive (from Sphinx) for display math.
mathBlock :: GenParser Char st Block
mathBlock = mathBlockMultiline <|> mathBlockOneLine
mathBlock = try $ do
string ".. math::"
mathBlockMultiline <|> mathBlockOneLine
mathBlockOneLine :: GenParser Char st Block
mathBlockOneLine = try $ do
string ".. math:"
result <- manyTill anyChar newline
blanklines
return $ Para [Math DisplayMath $ removeLeadingTrailingSpace result]
mathBlockMultiline :: GenParser Char st Block
mathBlockMultiline = try $ do
string ".. math::"
blanklines
result <- indentedBlock
-- a single block can contain multiple equations, which need to go
@ -384,7 +384,8 @@ mathBlockMultiline = try $ do
let startsWithColon (':':_) = True
startsWithColon _ = False
let lns' = dropWhile startsWithColon lns
let eqs = map unwords $ filter (not . null) $ splitBy null lns'
let eqs = map (removeLeadingTrailingSpace . unlines)
$ filter (not . null) $ splitBy null lns'
return $ Para $ map (Math DisplayMath) eqs
lhsCodeBlock :: GenParser Char ParserState Block

View file

@ -286,9 +286,11 @@ inlineToRST (Str str) = return $ text $ escapeString str
inlineToRST (Math t str) = do
modify $ \st -> st{ stHasMath = True }
return $ if t == InlineMath
then ":math:`" <> text str <> "`\\ "
else blankline $$ ".. math::" $$ blankline $$
nest 3 (text str) $$ blankline
then ":math:`" <> text str <> "`" <> beforeNonBlank "\\ "
else if '\n' `elem` str
then blankline $$ ".. math::" $$
blankline $$ nest 3 (text str) $$ blankline
else blankline $$ (".. math:: " <> text str) $$ blankline
inlineToRST (RawInline _ _) = return empty
inlineToRST (LineBreak) = return cr -- there's no line break in RST
inlineToRST Space = return space

View file

@ -305,4 +305,11 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
,Para [Note [Para [Str "Note",Space,Str "with",Space,Str "continuation",Space,Str "line",Str "."]]]
,Para [Note [Para [Str "Note",Space,Str "with"],Para [Str "continuation",Space,Str "block",Str "."]]]
,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 "."]]]
,Para [Str "Not",Space,Str "in",Space,Str "note",Str "."]]
,Para [Str "Not",Space,Str "in",Space,Str "note",Str "."]
,Header 1 [Str "Math"]
,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 ":"]
,Para [Math DisplayMath "E=mc^2"]
,Para [Math DisplayMath "E = mc^2"]
,Para [Math DisplayMath "E = mc^2",Math DisplayMath "\\alpha = \\beta"]
,Para [Math DisplayMath "E &= mc^2\\\\\nF &= \\pi E",Math DisplayMath "F &= \\gamma \\alpha^2"]
,Para [Str "All",Space,Str "done",Str "."]]

View file

@ -536,3 +536,32 @@ Footnotes
Not in note.
Math
====
Some inline math :math:`E=mc^2`\ . Now some
display math:
.. math:: E=mc^2
.. math::
E = mc^2
.. math::
E = mc^2
\alpha = \beta
.. math::
:label hithere
:nowrap
E &= mc^2\\
F &= \pi E
F &= \gamma \alpha^2
All done.

View file

@ -598,15 +598,16 @@ LaTeX
=====
-
- :math:`$2+2=4$`
- :math:`$x \in y$`
- :math:`$\alpha \wedge \omega$`
- :math:`$223$`
- :math:`$p$`-Tree
- :math:`2+2=4`
- :math:`x \in y`
- :math:`\alpha \wedge \omega`
- :math:`223`
- :math:`p`\ -Tree
- Heres some display math:
:math:`$$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$$`
- Heres one that has a line break in it:
:math:`$\alpha + \omega \times x^2$`.
.. math:: \frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}
- Heres one that has a line break in it: :math:`\alpha + \omega \times x^2`\ .
These shouldnt be math: