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.
This commit is contained in:
John MacFarlane 2017-12-22 17:59:47 -08:00
parent 4a07977715
commit 28b736bf95
6 changed files with 55 additions and 15 deletions

View file

@ -3195,18 +3195,22 @@ LaTeX macros
#### Extension: `latex_macros` ####
For output formats other than LaTeX, pandoc will parse LaTeX `\newcommand` and
`\renewcommand` definitions and apply the resulting macros to all LaTeX
math. So, for example, the following will work in all output formats,
not just LaTeX:
For output formats other than LaTeX, pandoc will parse LaTeX
macro definitions and apply the resulting macros to all LaTeX
math and raw LaTeX. So, for example, the following will work in
all output formats, not just LaTeX:
\newcommand{\tuple}[1]{\langle #1 \rangle}
$\tuple{a, b, c}$
In LaTeX output, the `\newcommand` definition will simply be passed
unchanged to the output.
In LaTeX output, the macro definitions will not be passed
through as raw LaTeX.
When `latex_macros` is disabled, the macro definitions will
be passed through as raw LaTeX, and the raw LaTeX and math will
not have macros applied. This is usually a better approach when
you are targeting LaTeX or PDF.
Links
-----

View file

@ -272,8 +272,10 @@ rawLaTeXBlock = do
lookAhead (try (char '\\' >> letter))
-- we don't want to apply newly defined latex macros to their own
-- definitions:
(snd <$> rawLaTeXParser macroDef) <|>
((snd <$> rawLaTeXParser (environment <|> blockCommand)) >>= applyMacros)
(do (_, raw) <- rawLaTeXParser macroDef
(guardDisabled Ext_latex_macros >> return raw) <|> return "")
<|> (do (_, raw) <- rawLaTeXParser (environment <|> blockCommand)
applyMacros raw)
rawLaTeXInline :: (PandocMonad m, HasMacros s, HasReaderOptions s)
=> ParserT String s m String

View file

@ -1120,13 +1120,17 @@ rawVerbatimBlock = htmlInBalanced isVerbTag
rawTeXBlock :: PandocMonad m => MarkdownParser m (F Blocks)
rawTeXBlock = do
guardEnabled Ext_raw_tex
result <- (B.rawBlock "context" . trimr . concat <$>
result <- (B.rawBlock "context" . trim . concat <$>
many1 ((++) <$> (rawConTeXtEnvironment <|> conTeXtCommand)
<*> (blanklines <|> many spaceChar)))
<|> (B.rawBlock "latex" . trimr . concat <$>
<|> (B.rawBlock "latex" . trim . concat <$>
many1 ((++) <$> rawLaTeXBlock
<*> (blanklines <|> many spaceChar)))
return $ return result
return $ case B.toList result of
[RawBlock _ cs]
| all (`elem` [' ','\t','\n']) cs -> return mempty
-- don't create a raw block for suppressed macro defs
_ -> return result
conTeXtCommand :: PandocMonad m => MarkdownParser m String
conTeXtCommand = oneOfStrings ["\\placeformula"]

View file

@ -3,6 +3,5 @@
\newcommand{\gen}{a\ Gen\ b}
abc
^D
[RawBlock (Format "latex") "\\newcommand{\\gen}{a\\ Gen\\ b}"
,Para [Str "abc"]]
[Para [Str "abc"]]
```

View file

@ -3,7 +3,6 @@
\newcommand{\my}{\phi}
$\my+\my$
^D
\newcommand{\my}{\phi}
$\phi+\phi$
```
@ -66,3 +65,36 @@ x &= y\\\end{aligned}\]
\emph{hi--ok}
```
```
% pandoc -f markdown+latex_macros -t markdown
\newcommand{\my}{\phi}
\begin{equation}
\my+\my
\end{equation}
^D
\begin{equation}
\phi+\phi
\end{equation}
```
```
% pandoc -f markdown-latex_macros -t markdown
\newcommand{\my}{\phi}
\begin{equation}
\my+\my
\end{equation}
^D
\newcommand{\my}{\phi}
\begin{equation}
\my+\my
\end{equation}
```
```
% pandoc -f markdown+latex_macros -t markdown
\newcommand{\my}{\emph{a}}
\my
^D
\emph{a}
```

View file

@ -54,7 +54,6 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "Author",S
,OrderedList (3,Example,TwoParens)
[[Plain [Str "Third",Space,Str "example."]]]
,Header 2 ("macros",[],[]) [Str "Macros"]
,RawBlock (Format "latex") "\\newcommand{\\tuple}[1]{\\langle #1 \\rangle}"
,Para [Math InlineMath "\\langle x,y \\rangle"]
,Header 2 ("case-insensitive-references",[],[]) [Str "Case-insensitive",Space,Str "references"]
,Para [Link ("",[],[]) [Str "Fum"] ("/fum","")]