git.iko.soy/typeable/pandoc
on master help sign in
`latex_macros` extension changes.

Don't pass through macro definitions themselves when `latex_macros` is set. The macros have already been applied. If `latex_macros` is enabled, then `rawLaTeXBlock` in Text.Pandoc.Readers.LaTeX will succeed in parsing a macro definition, and will update pandoc's internal macro map accordingly, but the empty string will be returned. Together with earlier changes, this closes #4179.

commit28b736bf957da0df79ffb211fc5e7ec4ff713c4b authorJohn MacFarlane <jgm@berkeley.edu> dateFri 22 Dec 2017, 17:59 (9 years ago) parent4a07977
6 files changed+55−15
MANUAL.txt+10−6 src/Text/Pandoc/Readers/LaTeX.hs+4−2 src/Text/Pandoc/Readers/Markdown.hs+7−3 test/command/4159.md+1−2 test/command/macros.md+33−1 test/markdown-reader-more.native−1
MANUAL.txt+10−6
@@ -3195,18 +3195,22 @@ LaTeX macros
31953195
31963196#### Extension: `latex_macros` ####
31973197
3198−For output formats other than LaTeX, pandoc will parse LaTeX `\newcommand` and
3199−`\renewcommand` definitions and apply the resulting macros to all LaTeX
3200−math. So, for example, the following will work in all output formats,
3201−not just LaTeX:
3198+For output formats other than LaTeX, pandoc will parse LaTeX
3199+macro definitions and apply the resulting macros to all LaTeX
3200+math and raw LaTeX. So, for example, the following will work in
3201+all output formats, not just LaTeX:
32023202
32033203 \newcommand{\tuple}[1]{\langle #1 \rangle}
32043204
32053205 $\tuple{a, b, c}$
32063206
3207−In LaTeX output, the `\newcommand` definition will simply be passed
3208−unchanged to the output.
3207+In LaTeX output, the macro definitions will not be passed
3208+through as raw LaTeX.
32093209
3210+When `latex_macros` is disabled, the macro definitions will
3211+be passed through as raw LaTeX, and the raw LaTeX and math will
3212+not have macros applied. This is usually a better approach when
3213+you are targeting LaTeX or PDF.
32103214
32113215Links
32123216-----
src/Text/Pandoc/Readers/LaTeX.hs+4−2
@@ -272,8 +272,10 @@ rawLaTeXBlock = do
272272 lookAhead (try (char '\\' >> letter))
273273 -- we don't want to apply newly defined latex macros to their own
274274 -- definitions:
275− (snd <$> rawLaTeXParser macroDef) <|>
276− ((snd <$> rawLaTeXParser (environment <|> blockCommand)) >>= applyMacros)
275+ (do (_, raw) <- rawLaTeXParser macroDef
276+ (guardDisabled Ext_latex_macros >> return raw) <|> return "")
277+ <|> (do (_, raw) <- rawLaTeXParser (environment <|> blockCommand)
278+ applyMacros raw)
277279
278280rawLaTeXInline :: (PandocMonad m, HasMacros s, HasReaderOptions s)
279281 => ParserT String s m String
src/Text/Pandoc/Readers/Markdown.hs+7−3
@@ -1120,13 +1120,17 @@ rawVerbatimBlock = htmlInBalanced isVerbTag
11201120rawTeXBlock :: PandocMonad m => MarkdownParser m (F Blocks)
11211121rawTeXBlock = do
11221122 guardEnabled Ext_raw_tex
1123− result <- (B.rawBlock "context" . trimr . concat <$>
1123+ result <- (B.rawBlock "context" . trim . concat <$>
11241124 many1 ((++) <$> (rawConTeXtEnvironment <|> conTeXtCommand)
11251125 <*> (blanklines <|> many spaceChar)))
1126− <|> (B.rawBlock "latex" . trimr . concat <$>
1126+ <|> (B.rawBlock "latex" . trim . concat <$>
11271127 many1 ((++) <$> rawLaTeXBlock
11281128 <*> (blanklines <|> many spaceChar)))
1129− return $ return result
1129+ return $ case B.toList result of
1130+ [RawBlock _ cs]
1131+ | all (`elem` [' ','\t','\n']) cs -> return mempty
1132+ -- don't create a raw block for suppressed macro defs
1133+ _ -> return result
11301134
11311135conTeXtCommand :: PandocMonad m => MarkdownParser m String
11321136conTeXtCommand = oneOfStrings ["\\placeformula"]
test/command/4159.md+1−2
@@ -3,6 +3,5 @@
33\newcommand{\gen}{a\ Gen\ b}
44abc
55^D
6−[RawBlock (Format "latex") "\\newcommand{\\gen}{a\\ Gen\\ b}"
7−,Para [Str "abc"]]
6+[Para [Str "abc"]]
87```
test/command/macros.md+33−1
@@ -3,7 +3,6 @@
33\newcommand{\my}{\phi}
44$\my+\my$
55^D
6−\newcommand{\my}{\phi}
76$\phi+\phi$
87```
98
@@ -66,3 +65,36 @@ x &= y\\\end{aligned}\]
6665
6766\emph{hi--ok}
6867```
68+
69+```
70+% pandoc -f markdown+latex_macros -t markdown
71+\newcommand{\my}{\phi}
72+\begin{equation}
73+\my+\my
74+\end{equation}
75+^D
76+\begin{equation}
77+\phi+\phi
78+\end{equation}
79+```
80+
81+```
82+% pandoc -f markdown-latex_macros -t markdown
83+\newcommand{\my}{\phi}
84+\begin{equation}
85+\my+\my
86+\end{equation}
87+^D
88+\newcommand{\my}{\phi}
89+\begin{equation}
90+\my+\my
91+\end{equation}
92+```
93+
94+```
95+% pandoc -f markdown+latex_macros -t markdown
96+\newcommand{\my}{\emph{a}}
97+\my
98+^D
99+\emph{a}
100+```
test/markdown-reader-more.native−1
@@ -54,7 +54,6 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "Author",S
5454,OrderedList (3,Example,TwoParens)
5555 [[Plain [Str "Third",Space,Str "example."]]]
5656,Header 2 ("macros",[],[]) [Str "Macros"]
57−,RawBlock (Format "latex") "\\newcommand{\\tuple}[1]{\\langle #1 \\rangle}"
5857,Para [Math InlineMath "\\langle x,y \\rangle"]
5958,Header 2 ("case-insensitive-references",[],[]) [Str "Case-insensitive",Space,Str "references"]
6059,Para [Link ("",[],[]) [Str "Fum"] ("/fum","")]