From 5219599a775b5765d39ddceae4d57223c8eacc41 Mon Sep 17 00:00:00 2001
From: hubertp-lshift <hubertp@lshift.de>
Date: Sat, 26 Nov 2016 21:47:51 +0100
Subject: [PATCH] [Tex] Remove invalid inlines in sections (#3218)

Latex doesn't like when hypertargets or images are
put in the options list of the section. They are not
lost since they were actually duplicated and present
also in the second argument list.

Note on the implementation:
I had to inline the definiton of 'foldMap' since it is
not implemented in every version of Haskell that Pandoc
supports.
---
 src/Text/Pandoc/Writers/LaTeX.hs | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index b75f56cef..3657f3464 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -743,14 +743,16 @@ sectionHeader :: Bool    -- True for unnumbered
 sectionHeader unnumbered ident level lst = do
   txt <- inlineListToLaTeX lst
   plain <- stringToLaTeX TextString $ concatMap stringify lst
-  let noNote (Note _) = Str ""
-      noNote x        = x
-  let lstNoNotes = walk noNote lst
+  let removeInvalidInline (Note _)      = []
+      removeInvalidInline (Span (id', _, _) _) | not (null id') = []
+      removeInvalidInline (Image _ _ _) = []
+      removeInvalidInline x             = [x]
+  let lstNoNotes = foldr (mappend . (\x -> walkM removeInvalidInline x)) mempty lst
   txtNoNotes <- inlineListToLaTeX lstNoNotes
   -- footnotes in sections don't work (except for starred variants)
   -- unless you specify an optional argument:
   -- \section[mysec]{mysec\footnote{blah}}
-  optional <- if unnumbered || lstNoNotes == lst
+  optional <- if unnumbered || lstNoNotes == lst || lstNoNotes == []
                  then return empty
                  else do
                    return $ brackets txtNoNotes