aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHikaru Ibayashi <[email protected]>2023-11-17 18:49:03 -0800
committerGitHub <[email protected]>2023-11-17 18:49:03 -0800
commita3fba6d83e86978d91a913a3113bb3db92195298 (patch)
treef101f818403b525007c17b007982957302851b27 /src
parent267b84e1d987b5065263cbe5899248c04d055678 (diff)
LaTeX reader: fix theorem label parsing (#9198)
Closes #8872. In the current implementation , theorem labels in `theoremEnvironment` are determined by the `LastLabel` in the current state. This approach works well when the `\label{label_name}` is placed at the end of the theorem body. However, when a label is placed at the beginning of the theorem (another common practice) and additional labels follow in the theorem body, `theoremEnvironment` incorrectly picks the last label (e.g., `\label{item2}` in #8872). This patch addresses the issue by extracting the label extraction independently of the `LastLabel` state.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Readers/LaTeX/Math.hs15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX/Math.hs b/src/Text/Pandoc/Readers/LaTeX/Math.hs
index 7b0437109..d3b4a4723 100644
--- a/src/Text/Pandoc/Readers/LaTeX/Math.hs
+++ b/src/Text/Pandoc/Readers/LaTeX/Math.hs
@@ -11,7 +11,8 @@ module Text.Pandoc.Readers.LaTeX.Math
, proof
)
where
-import Data.Maybe (fromMaybe)
+import Data.Maybe (fromMaybe, mapMaybe, listToMaybe)
+import Data.List (foldl')
import Text.Pandoc.Walk (walk)
import Text.Pandoc.Builder as B
import qualified Data.Sequence as Seq
@@ -140,6 +141,16 @@ newtheorem inline = do
M.insert name spec tmap }
return mempty
+extractLabelFromBlock :: Block -> Maybe Text
+extractLabelFromBlock (Para inlines) = extractLabel Nothing inlines
+ where
+ extractLabel = foldl' go
+ go :: Maybe Text -> Inline -> Maybe Text
+ go (Just t) _ = Just t
+ go Nothing (Span (_, _, attrs) _) = lookup "label" attrs
+ go Nothing _ = Nothing
+extractLabelFromBlock _ = Nothing
+
theoremEnvironment :: PandocMonad m
=> LP m Blocks -> LP m Inlines -> Text -> LP m Blocks
theoremEnvironment blocks opt name = do
@@ -150,7 +161,7 @@ theoremEnvironment blocks opt name = do
Just tspec -> do
optTitle <- option mempty $ (\x -> space <> "(" <> x <> ")") <$> opt
bs <- env name blocks
- mblabel <- sLastLabel <$> getState
+ let mblabel = listToMaybe $ mapMaybe extractLabelFromBlock (toList bs)
number <-
if theoremNumber tspec