aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2025-04-25 22:39:14 -0700
committerJohn MacFarlane <[email protected]>2025-04-25 22:39:56 -0700
commitfb5499d7af1654bdcb93247950d72baffa562d98 (patch)
tree849e46cad628b8dfe9c21cd8266ed1c134858e05
parenta32a9ff7e9683220828e5c86eeb7a6cdde462581 (diff)
Typst writer: add equation label if math contains `\label{..}`.
Closes #10805.
-rw-r--r--src/Text/Pandoc/Writers/Typst.hs16
-rw-r--r--test/command/10805.md9
2 files changed, 22 insertions, 3 deletions
diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs
index 767772521..d18b0fde3 100644
--- a/src/Text/Pandoc/Writers/Typst.hs
+++ b/src/Text/Pandoc/Writers/Typst.hs
@@ -384,9 +384,12 @@ inlineToTypst inline =
case res of
Left il -> inlineToTypst il
Right r ->
- case mathType of
- InlineMath -> return $ "$" <> literal r <> "$"
- DisplayMath -> return $ "$ " <> literal r <> " $"
+ (case extractLabel str of -- #10805
+ Nothing -> id
+ Just lab -> (<> ("<" <> literal lab <> ">"))) <$>
+ case mathType of
+ InlineMath -> return $ "$" <> literal r <> "$"
+ DisplayMath -> return $ "$ " <> literal r <> " $"
Code (_,cls,_) code -> return $
case cls of
(lang:_) -> "#raw(lang:" <> doubleQuoted lang <>
@@ -605,3 +608,10 @@ doubleQuoted = doubleQuotes . literal . escape
endCode :: Doc Text
endCode = beforeNonBlank ";"
+
+extractLabel :: Text -> Maybe Text
+extractLabel = go . T.unpack
+ where
+ go [] = Nothing
+ go ('\\':'l':'a':'b':'e':'l':'{':xs) = Just (T.pack (takeWhile (/='}') xs))
+ go (_:xs) = go xs
diff --git a/test/command/10805.md b/test/command/10805.md
new file mode 100644
index 000000000..e3d02bc51
--- /dev/null
+++ b/test/command/10805.md
@@ -0,0 +1,9 @@
+```
+% pandoc -t typst -f latex
+\begin{equation}
+ \label{eq:U}
+ U = A
+\end{equation}
+^D
+$ U = A $<eq:U>
+```