From 58447bba98cb162b21c30755e0e237f890160a1e Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Tue, 15 May 2018 09:15:45 -0700
Subject: [PATCH] rawLaTeXBlock: don't expand macros in macro definitions!

Closes #4653.  Note that this only affected LaTeX in markdown.
Added regression test.
---
 src/Text/Pandoc/Readers/LaTeX.hs | 22 +++++++++++-----------
 test/command/4653.md             |  8 ++++++++
 2 files changed, 19 insertions(+), 11 deletions(-)
 create mode 100644 test/command/4653.md

diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 39dffde76..f2c0d1fbb 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -242,8 +242,8 @@ withVerbatimMode parser = do
   return result
 
 rawLaTeXParser :: (PandocMonad m, HasMacros s, HasReaderOptions s)
-               => LP m a -> LP m a -> ParserT String s m (a, String)
-rawLaTeXParser parser valParser = do
+               => Bool -> LP m a -> LP m a -> ParserT String s m (a, String)
+rawLaTeXParser retokenize parser valParser = do
   inp <- getInput
   let toks = tokenize "source" $ T.pack inp
   pstate <- getState
@@ -254,10 +254,11 @@ rawLaTeXParser parser valParser = do
   case res' of
        Left _    -> mzero
        Right toks' -> do
-         res <- lift $ runParserT (do doMacros 0
-                                      -- retokenize, applying macros
-                                      ts <- many (satisfyTok (const True))
-                                      setInput ts
+         res <- lift $ runParserT (do when retokenize $ do
+                                        -- retokenize, applying macros
+                                        doMacros 0
+                                        ts <- many (satisfyTok (const True))
+                                        setInput ts
                                       rawparser)
                         lstate' "chunk" toks'
          case res of
@@ -284,20 +285,19 @@ rawLaTeXBlock :: (PandocMonad m, HasMacros s, HasReaderOptions s)
               => ParserT String s m String
 rawLaTeXBlock = do
   lookAhead (try (char '\\' >> letter))
-  -- we don't want to apply newly defined latex macros to their own
-  -- definitions:
-  snd <$> rawLaTeXParser (environment <|> macroDef <|> blockCommand) blocks
+  snd <$> (rawLaTeXParser False macroDef blocks
+      <|> rawLaTeXParser True(environment <|> macroDef <|> blockCommand) blocks)
 
 rawLaTeXInline :: (PandocMonad m, HasMacros s, HasReaderOptions s)
                => ParserT String s m String
 rawLaTeXInline = do
   lookAhead (try (char '\\' >> letter))
-  snd <$> rawLaTeXParser (inlineEnvironment <|> inlineCommand') inlines
+  snd <$> rawLaTeXParser True (inlineEnvironment <|> inlineCommand') inlines
 
 inlineCommand :: PandocMonad m => ParserT String ParserState m Inlines
 inlineCommand = do
   lookAhead (try (char '\\' >> letter))
-  fst <$> rawLaTeXParser (inlineEnvironment <|> inlineCommand') inlines
+  fst <$> rawLaTeXParser True (inlineEnvironment <|> inlineCommand') inlines
 
 tokenize :: SourceName -> Text -> [Tok]
 tokenize sourcename = totoks (initialPos sourcename)
diff --git a/test/command/4653.md b/test/command/4653.md
new file mode 100644
index 000000000..24a706e89
--- /dev/null
+++ b/test/command/4653.md
@@ -0,0 +1,8 @@
+```
+% pandoc -t latex
+\let\tex\TeX
+\renewcommand{\TeX}{\tex\xspace}
+^D
+\let\tex\TeX
+\renewcommand{\TeX}{\tex\xspace}
+```