@@ -46,7 +46,8 @@ import Text.Pandoc.Readers.HTML ( rawHtmlBlock, anyHtmlBlockTag,
4646 htmlBlockElement, htmlComment, unsanitaryURI )
4747import Text.Pandoc.CharacterReferences ( decodeCharacterReferences )
4848import Text.ParserCombinators.Parsec
49−import Control.Monad (when, liftM, unless)
49+import Control.Monad (when, liftM, unless, guard)
50+import Text.TeXMath.Macros (applyMacros, Macro, pMacroDefinition)
5051
5152-- | Read markdown from an input string and return a Pandoc document.
5253readMarkdown :: ParserState -- ^ Parser state, including options for parser
@@ -284,6 +285,7 @@ block = do
284285 , plain
285286 , nullBlock ]
286287 else [ codeBlockDelimited
288+ , macro
287289 , header
288290 , table
289291 , codeBlockIndented
@@ -867,6 +869,29 @@ table = multilineTable False <|> simpleTable True <|>
867869 simpleTable False <|> multilineTable True <|>
868870 gridTable False <|> gridTable True <?> "table"
869871
872+--
873+-- Macros
874+--
875+
876+-- | Parse a \newcommand or \renewcommand macro definition.
877+macro :: GenParser Char ParserState Block
878+macro = getState >>= guard . stateApplyMacros >>
879+ pMacroDefinition >>= addMacro >> return Null
880+
881+-- | Add a macro to the list of macros in state.
882+addMacro :: Macro -> GenParser Char ParserState ()
883+addMacro m = do
884+ updateState $ \st -> st{ stateMacros = m : stateMacros st }
885+
886+-- | Apply current macros to string.
887+applyMacros' :: String -> GenParser Char ParserState String
888+applyMacros' target = do
889+ apply <- liftM stateApplyMacros getState
890+ if apply
891+ then do macros <- liftM stateMacros getState
892+ return $ applyMacros macros target
893+ else return target
894+
870895--
871896-- inline
872897--
@@ -969,8 +994,8 @@ mathChunk = do char '\\'
969994 <|> many1 (noneOf " \t\n\\$")
970995
971996math :: GenParser Char ParserState Inline
972−math = (mathDisplay >>= return . Math DisplayMath)
973− <|> (mathInline >>= return . Math InlineMath)
997+math = (mathDisplay >>= applyMacros' >>= return . Math DisplayMath)
998+ <|> (mathInline >>= applyMacros' >>= return . Math InlineMath)
974999
9751000mathDisplay :: GenParser Char ParserState String
9761001mathDisplay = try $ do