From 44222e0373f47c986833a609271d495d55ff48de Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sat, 13 Jan 2018 12:10:52 -0800
Subject: LaTeX reader: allow macro definitions inside macros.

Previously we went into an infinite loop with

```
\newcommand{\noop}[1]{#1}
\noop{\newcommand{\foo}[1]{#1}}
\foo{hi}
```

See #4253.
---
 src/Text/Pandoc/Readers/LaTeX.hs | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 62d240688..d9b188606 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -442,19 +442,22 @@ doMacros n = do
                                     Just o  ->
                                        (:) <$> option o bracketedToks
                                            <*> count (numargs - 1) getarg
-                       let addTok (Tok _ (Arg i) _) acc | i > 0
-                                                        , i <= numargs =
-                                 foldr addTok acc (args !! (i - 1))
+                       -- first boolean param is true if we're tokenizing
+                       -- an argument (in which case we don't want to
+                       -- expand #1 etc.)
+                       let addTok False (Tok _ (Arg i) _) acc | i > 0
+                                                              , i <= numargs =
+                                 foldr (addTok True) acc (args !! (i - 1))
                            -- add space if needed after control sequence
                            -- see #4007
-                           addTok (Tok _ (CtrlSeq x) txt)
+                           addTok _ (Tok _ (CtrlSeq x) txt)
                                   acc@(Tok _ Word _ : _)
                              | not (T.null txt) &&
                                (isLetter (T.last txt)) =
                                Tok spos (CtrlSeq x) (txt <> " ") : acc
-                           addTok t acc = setpos spos t : acc
+                           addTok _ t acc = setpos spos t : acc
                        ts' <- getInput
-                       setInput $ foldr addTok ts' newtoks
+                       setInput $ foldr (addTok False) ts' newtoks
                        case expansionPoint of
                             ExpandWhenUsed ->
                               if n > 20  -- detect macro expansion loops
-- 
cgit v1.2.3