Skip to content

Commit

Permalink
LaTeX writer: fix bug with big footnotes inside emphasis (#9168)
Browse files Browse the repository at this point in the history
Closes #8982.
  • Loading branch information
ibayashi-hikaru committed Dec 10, 2023
1 parent aa95770 commit 3be253f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Text/Pandoc/Writers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,39 @@ import Text.Pandoc.Writers.LaTeX.Util (stringToLaTeX, StringContext(..),
getListingsLanguage, mbBraced)
import Text.Pandoc.Writers.Shared
import qualified Text.Pandoc.Writers.AnnotatedTable as Ann
-- Work around problems with notes inside emphasis (see #8982)

isolateBigNotes :: ([Inline] -> Inline) -> [Inline] -> [Inline]
isolateBigNotes constructor xs =
let (before, after) = break isBigNote xs
in case after of
(noteInline:rest) -> constructor before :
noteInline :
isolateBigNotes constructor rest
[] -> [constructor xs]
where
isBigNote :: Inline -> Bool
isBigNote (Note [Plain _]) = False -- A small note
isBigNote (Note [Para _]) = False -- A small note
isBigNote (Note _) = True -- A big note
isBigNote _ = False -- Not a note

raiseBigNotes :: [Inline] -> [Inline]
raiseBigNotes (Emph inner : xs)
= isolateBigNotes Emph (raiseBigNotes inner) ++ raiseBigNotes xs
raiseBigNotes (Strong inner : xs)
= isolateBigNotes Strong (raiseBigNotes inner) ++ raiseBigNotes xs
raiseBigNotes (Underline inner : xs)
= isolateBigNotes Underline (raiseBigNotes inner) ++ raiseBigNotes xs
raiseBigNotes (Strikeout inner : xs)
= isolateBigNotes Strikeout (raiseBigNotes inner) ++ raiseBigNotes xs
raiseBigNotes (x : xs) = x : raiseBigNotes xs
raiseBigNotes [] = []

-- | Convert Pandoc to LaTeX.
writeLaTeX :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeLaTeX options document =
evalStateT (pandocToLaTeX options document) $
evalStateT (pandocToLaTeX options (walk raiseBigNotes document)) $
startingState options

-- | Convert Pandoc to LaTeX Beamer.
Expand Down
68 changes: 68 additions & 0 deletions test/Tests/Writers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,74 @@ tests = [ testGroup "code blocks"
, "backtick" =:
code "`nu?`" =?> "\\texttt{\\textasciigrave{}nu?\\textasciigrave{}}"
]
, testGroup "inline note"
[ "Big note in emph" =:
emph (str "This sentence"
<> note (para (str "paragraph1")
<> para (str "paragraph2"))
<> str " has footnote.")
=?>
"\\emph{This sentence}\\footnote{paragraph1\n\n paragraph2}"
<> "\\emph{ has footnote.}"
, "Big note in strong" =:
strong (str "This sentence"
<> note (para (str "paragraph1")
<> para (str "paragraph2"))
<> str " has footnote.")
=?>
"\\textbf{This sentence}\\footnote{paragraph1\n\n paragraph2}"
<> "\\textbf{ has footnote.}"

, "Big note in underline" =:
underline (str "This sentence"
<> note (para (str "paragraph1")
<> para (str "paragraph2"))
<> str " has footnote.")
=?>
"\\ul{This sentence}\\footnote{paragraph1\n\n paragraph2}"
<> "\\ul{ has footnote.}"

, "Big note in strikeout" =:
strikeout (str "This sentence"
<> note (para (str "paragraph1")
<> para (str "paragraph2"))
<> str " has footnote.")
=?>
"\\st{This sentence}\\footnote{paragraph1\n\n paragraph2}"
<> "\\st{ has footnote.}"

, "Small note in emph" =:
emph (str "This sentence"
<> note (para (str "paragraph"))
<> str " has footnote.")
=?>
"\\emph{This sentence\\footnote{paragraph} has footnote.}"

, "Big note nested in emph and strong" =:
emph (str "This "
<> strong (str "nested sentence "
<> note (para (str "paragraph1")
<> para (str "paragraph2"))
<> str "has ")
<> str "footnote."
)
=?>
"\\emph{This \\textbf{nested sentence }}\\footnote{paragraph1\n\n"
<> " paragraph2}\\emph{\\textbf{has }footnote.}"

, "Two Big notes in emph" =:
emph (str "This sentence"
<> note (para (str "1-paragraph1")
<> para (str "1-paragraph2"))
<> str " has"
<> note (para (str "2-paragraph1")
<> para (str "2-paragraph2"))
<> str " footnote.")
=?>
"\\emph{This sentence}\\footnote{1-paragraph1\n\n 1-paragraph2}"
<> "\\emph{ has}\\footnote{2-paragraph1\n\n 2-paragraph2}"
<> "\\emph{ footnote.}"
]
, testGroup "writer options"
[ testGroup "top-level division" $
let
Expand Down

0 comments on commit 3be253f

Please sign in to comment.