@@ -39,12 +39,15 @@ import Text.Pandoc.Parsing hiding ( F, unF, askF, asksF, runF
3939 , newline, orderedListMarker
4040 , parseFromString
4141 , updateLastStrPos )
42+import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
4243import Text.Pandoc.Shared (compactify', compactify'DL)
44+import Text.Parsec.Pos (updatePosString)
45+import Text.TeXMath (texMathToPandoc, DisplayType(..))
4346
4447import Control.Applicative ( Applicative, pure
4548 , (<$>), (<$), (<*>), (<*), (*>), (<**>) )
4649import Control.Arrow (first)
47−import Control.Monad (foldM, guard, liftM, liftM2, mzero, when)
50+import Control.Monad (foldM, guard, liftM, liftM2, mplus, mzero, when)
4851import Control.Monad.Reader (Reader, runReader, ask, asks)
4952import Data.Char (isAlphaNum, toLower)
5053import Data.Default
@@ -886,6 +889,7 @@ inline =
886889 , verbatim
887890 , subscript
888891 , superscript
892+ , inlineLaTeX
889893 , symbol
890894 ] <* (guard =<< newlinesCountWithinLimits)
891895 <?> "inline"
@@ -1351,3 +1355,29 @@ simpleSubOrSuperString = try $
13511355 , mappend <$> option [] ((:[]) <$> oneOf "+-")
13521356 <*> many1 alphaNum
13531357 ]
1358+
1359+inlineLaTeX :: OrgParser (F Inlines)
1360+inlineLaTeX = try $ do
1361+ cmd <- inlineLaTeXCommand
1362+ maybe mzero returnF $ parseAsMath cmd `mplus` parseAsInlineLaTeX cmd
1363+ where
1364+ parseAsMath :: String -> Maybe Inlines
1365+ parseAsMath cs = maybeRight $ B.fromList <$> texMathToPandoc DisplayInline cs
1366+
1367+ parseAsInlineLaTeX :: String -> Maybe Inlines
1368+ parseAsInlineLaTeX cs = maybeRight $ runParser inlineCommand state "" cs
1369+
1370+ state :: ParserState
1371+ state = def{ stateOptions = def{ readerParseRaw = True }}
1372+
1373+maybeRight :: Either a b -> Maybe b
1374+maybeRight = either (const Nothing) Just
1375+
1376+inlineLaTeXCommand :: OrgParser String
1377+inlineLaTeXCommand = try $ do
1378+ rest <- getInput
1379+ pos <- getPosition
1380+ case runParser rawLaTeXInline def "source" rest of
1381+ Right (RawInline _ cs) -> cs <$ (setInput $ drop (length cs) rest)
1382+ <* (setPosition $ updatePosString pos cs)
1383+ _ -> mzero