From e3a263df4669450049c49043412f791261e049a4 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 11 Aug 2021 16:14:34 -0700 Subject: Fix scope for LaTeX macros. They should by default scope over the group in which they are defined (except `\gdef` and `\xdef`, which are global). In addition, environments must be treated as groups. We handle this by making sMacros in the LaTeX parser state a STACK of macro tables. Opening a group adds a table to the stack, closing one removes one. Only the top of the stack is queried. This commit adds a parameter for scope to the Macro constructor (not exported). Closes #7494. --- test/command/7494.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/command/7494.md (limited to 'test/command/7494.md') diff --git a/test/command/7494.md b/test/command/7494.md new file mode 100644 index 000000000..d8a2ff7a5 --- /dev/null +++ b/test/command/7494.md @@ -0,0 +1,50 @@ +``` +% pandoc -f latex -t plain +\def\foo{BAR} +{\foo +\def\foo{BAZ} +\foo +} +\foo +^D +BAR BAZ BAR +``` + +``` +% pandoc -f latex -t plain +\def\foo{BAR} +{\foo +\gdef\foo{BAZ} +\foo +} +\foo +^D +BAR BAZ BAZ +``` + +``` +% pandoc -f latex -t plain +\newcommand{\aaa}{BBB} +{ +\renewcommand{\aaa}{AAA} +\aaa +} +\aaa +^D +AAA BBB +``` + +``` +% pandoc -f latex -t markdown +\newcommand{\aaa}{BBB} +\begin{quote} +\renewcommand{\aaa}{AAA} +\aaa +\end{quote} +\aaa +^D +> AAA + +BBB +``` + -- cgit v1.2.3 From dd1a956a8aa7d0cadd37db1650698eee9274beee Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 11 Aug 2021 16:28:53 -0700 Subject: LaTeX reader: Support `\global` before `\def`, `\let`, etc. See #7494. --- src/Text/Pandoc/Readers/LaTeX/Macro.hs | 12 ++++++++++-- test/command/7494.md | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'test/command/7494.md') diff --git a/src/Text/Pandoc/Readers/LaTeX/Macro.hs b/src/Text/Pandoc/Readers/LaTeX/Macro.hs index 5709cbb8c..bd7979ef3 100644 --- a/src/Text/Pandoc/Readers/LaTeX/Macro.hs +++ b/src/Text/Pandoc/Readers/LaTeX/Macro.hs @@ -25,8 +25,8 @@ macroDef constructor = do guardDisabled Ext_latex_macros) <|> return mempty where commandDef = do - nameMacroPairs <- newcommand <|> letmacro <|> - edefmacro <|> defmacro <|> newif + nameMacroPairs <- newcommand <|> + checkGlobal (letmacro <|> edefmacro <|> defmacro <|> newif) guardDisabled Ext_latex_macros <|> mapM_ insertMacro nameMacroPairs environmentDef = do @@ -77,6 +77,14 @@ letmacro = do Macro GroupScope ExpandWhenDefined [] Nothing [target])] _ -> pure [(name, Macro GroupScope ExpandWhenDefined [] Nothing [target])] +checkGlobal :: PandocMonad m => LP m [(Text, Macro)] -> LP m [(Text, Macro)] +checkGlobal p = + (do controlSeq "global" + ms <- p + return $ map (\(n, Macro _ expand arg optarg contents) -> + (n, Macro GlobalScope expand arg optarg contents)) ms) + <|> p + edefmacro :: PandocMonad m => LP m [(Text, Macro)] edefmacro = do scope <- (GroupScope <$ controlSeq "edef") diff --git a/test/command/7494.md b/test/command/7494.md index d8a2ff7a5..364bcb7e1 100644 --- a/test/command/7494.md +++ b/test/command/7494.md @@ -22,6 +22,18 @@ BAR BAZ BAR BAR BAZ BAZ ``` +``` +% pandoc -f latex -t plain +\def\foo{BAR} +{\foo +\global\def\foo{BAZ} +\foo +} +\foo +^D +BAR BAZ BAZ +``` + ``` % pandoc -f latex -t plain \newcommand{\aaa}{BBB} -- cgit v1.2.3