diff options
| author | John MacFarlane <jgm@berkeley.edu> | 2023-08-26 16:15:20 -0700 |
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2023-08-26 16:19:01 -0700 |
| commit | f7035d080c617cda3804a7d0932da8283a5c681c (patch) | |
| tree | 996d8a89d682dc15798ae1a1ab6299ea90a17e66 | |
| parent | 99340446685eed812895c7fb7028979dc6975ab3 (diff) | |
LaTeX writer link/target improvements.
+ Use `\phantomsection` and `\label` instead of `\hypertarget`.
+ Use `\hyperref` for LaTeX internal links, `\hyperlink` for
beamer (since `\hyperref` doesn't seem to work).
Closes #9022.
| -rw-r--r-- | src/Text/Pandoc/Writers/LaTeX.hs | 54 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/LaTeX/Util.hs | 14 | ||||
| -rw-r--r-- | test/command/1710.md | 2 | ||||
| -rw-r--r-- | test/command/2874.md | 2 | ||||
| -rw-r--r-- | test/command/4016.md | 6 | ||||
| -rw-r--r-- | test/command/4690.md | 2 | ||||
| -rw-r--r-- | test/command/6030.md | 4 | ||||
| -rw-r--r-- | test/command/6107.md | 4 | ||||
| -rw-r--r-- | test/command/6360.md | 2 | ||||
| -rw-r--r-- | test/command/7278.md | 8 | ||||
| -rw-r--r-- | test/command/7857.md | 2 |
11 files changed, 43 insertions, 57 deletions
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index 3585e1544..d12890bd5 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -281,11 +281,9 @@ blockToLaTeX (Div attr@(identifier,"block":dclasses,_) | "example" `elem` dclasses = "exampleblock" | "alert" `elem` dclasses = "alertblock" | otherwise = "block" - ref <- toLabel identifier - let anchor = if T.null identifier - then empty - else cr <> "\\protect\\hypertarget" <> - braces (literal ref) <> braces empty + anchor <- if T.null identifier + then pure empty + else (cr <>) <$> hypertarget identifier title' <- inlineListToLaTeX ils contents <- blockListToLaTeX bs wrapDiv attr $ ("\\begin" <> braces blockname <> braces title' <> anchor) $$ @@ -318,11 +316,9 @@ blockToLaTeX (Div (identifier,"slide":dclasses,dkvs) slideTitle <- if ils == [Str "\0"] -- marker for hrule then return empty else braces <$> inlineListToLaTeX ils - ref <- toLabel identifier - let slideAnchor = if T.null identifier - then empty - else cr <> "\\protect\\hypertarget" <> - braces (literal ref) <> braces empty + slideAnchor <- if T.null identifier + then pure empty + else (cr <>) <$> hypertarget identifier contents <- blockListToLaTeX bs >>= wrapDiv (identifier,classes,kvs) return $ ("\\begin{frame}" <> options <> slideTitle <> slideAnchor) $$ contents $$ "\\end{frame}" @@ -354,16 +350,8 @@ blockToLaTeX (Div (identifier,classes,kvs) bs) = do $+$ "\\end{CSLReferences}" else blockListToLaTeX bs modify $ \st -> st{ stIncremental = oldIncremental } - linkAnchor' <- hypertarget True identifier empty - -- see #2704 for the motivation for adding \leavevmode - -- and #7078 for \vadjust pre - let linkAnchor = - case bs of - Para _ : _ - | not (isEmpty linkAnchor') - -> "\\leavevmode\\vadjust pre{" <> linkAnchor' <> "}%" - _ -> linkAnchor' - wrapNotes txt = if beamer && "notes" `elem` classes + linkAnchor <- hypertarget identifier + let wrapNotes txt = if beamer && "notes" `elem` classes then "\\note" <> braces txt -- speaker notes else linkAnchor $$ txt wrapNotes <$> wrapDiv (identifier,classes,kvs) result @@ -396,12 +384,10 @@ blockToLaTeX (BlockQuote lst) = do return $ "\\begin{quote}" $$ contents $$ "\\end{quote}" blockToLaTeX (CodeBlock (identifier,classes,keyvalAttr) str) = do opts <- gets stOptions - lab <- labelFor identifier inNote <- stInNote <$> get - linkAnchor' <- hypertarget True identifier lab - let linkAnchor = if isEmpty linkAnchor' - then empty - else linkAnchor' <> "%" + linkAnchor <- if T.null identifier + then pure empty + else ((<> cr) . (<> "%")) <$> hypertarget identifier let lhsCodeBlock = do modify $ \s -> s{ stLHS = True } return $ flush (linkAnchor $$ "\\begin{code}" $$ literal str $$ @@ -575,8 +561,8 @@ blockToLaTeX (Figure (ident, _, _) captnode body) = do [b] -> blockToLaTeX b bs -> mconcat . intersperse (cr <> "\\hfill") <$> mapM (toSubfigure (length bs)) bs - innards <- hypertarget True ident $ - "\\centering" $$ contents $$ caption <> cr + target <- hypertarget ident + let innards = target $$ "\\centering" $$ contents $$ caption <> cr modify $ \st -> st{ stInFigure = isSubfigure , stSubfigure = stSubfigure st || isSubfigure @@ -777,7 +763,7 @@ inlineToLaTeX (Span ("",["mark"],[]) lst) = do modify $ \st -> st{ stStrikeout = True } -- this gives us the soul package inCmd "hl" <$> inlineListToLaTeX lst inlineToLaTeX (Span (id',classes,kvs) ils) = do - linkAnchor <- hypertarget False id' empty + linkAnchor <- hypertarget id' lang <- toLang $ lookup "lang" kvs let classToCmd "csl-no-emph" = Just "textup" classToCmd "csl-no-strong" = Just "textnormal" @@ -806,7 +792,7 @@ inlineToLaTeX (Span (id',classes,kvs) ils) = do else id) $ (if T.null id' then empty - else "\\protect" <> linkAnchor) <> + else linkAnchor) <> (if null cmds then braces contents else foldr inCmd contents cmds) @@ -959,7 +945,11 @@ inlineToLaTeX (Link (id',_,_) txt (src,_)) = Just ('#', ident) -> do contents <- inlineListToLaTeX txt lab <- toLabel ident - return $ text "\\hyperref" <> brackets (literal lab) <> braces contents + beamer <- gets stBeamer + return $ + if beamer + then text "\\hyperlink" <> braces (literal lab) <> braces contents + else text "\\hyperref" <> brackets (literal lab) <> braces contents _ -> case txt of [Str x] | unEscapeString (T.unpack x) == unEscapeString (T.unpack src) -> -- autolink do modify $ \s -> s{ stUrl = True } @@ -979,8 +969,8 @@ inlineToLaTeX (Link (id',_,_) txt (src,_)) = >>= (if T.null id' then return else \x -> do - linkAnchor <- hypertarget False id' x - return ("\\protect" <> linkAnchor)) + linkAnchor <- hypertarget id' + return (linkAnchor <> x)) inlineToLaTeX il@(Image _ _ (src, _)) | Just _ <- T.stripPrefix "data:" src = do report $ InlineNotRendered il diff --git a/src/Text/Pandoc/Writers/LaTeX/Util.hs b/src/Text/Pandoc/Writers/LaTeX/Util.hs index 79937e1fa..fbdd7a59c 100644 --- a/src/Text/Pandoc/Writers/LaTeX/Util.hs +++ b/src/Text/Pandoc/Writers/LaTeX/Util.hs @@ -254,15 +254,11 @@ wrapDiv (_,classes,kvs) t = do Nothing -> txt return $ wrapColumns . wrapColumn . wrapDir . wrapLang $ t -hypertarget :: PandocMonad m => Bool -> Text -> Doc Text -> LW m (Doc Text) -hypertarget _ "" x = return x -hypertarget addnewline ident x = do - ref <- literal `fmap` toLabel ident - return $ text "\\hypertarget" - <> braces ref - <> braces ((if addnewline && not (isEmpty x) - then "%" <> cr - else empty) <> x) +hypertarget :: PandocMonad m => Text -> LW m (Doc Text) +hypertarget "" = return mempty +hypertarget ident = do + label <- labelFor ident + return $ text "\\phantomsection" <> label labelFor :: PandocMonad m => Text -> LW m (Doc Text) labelFor "" = return empty diff --git a/test/command/1710.md b/test/command/1710.md index 64d86cffe..d14e35e14 100644 --- a/test/command/1710.md +++ b/test/command/1710.md @@ -55,7 +55,7 @@ ok </div> ^D \begin{frame}{Slide one} -\protect\hypertarget{slide-one}{} +\phantomsection\label{slide-one} \begin{columns}[T] \begin{column}{0.4\textwidth} \begin{itemize} diff --git a/test/command/2874.md b/test/command/2874.md index 99f46d2fb..41bd17959 100644 --- a/test/command/2874.md +++ b/test/command/2874.md @@ -10,5 +10,5 @@ % pandoc -f html -t latex <a name="foo"></a><br/> ^D -\protect\hypertarget{foo}{}{}\strut \\ +\phantomsection\label{foo}{}\strut \\ ``` diff --git a/test/command/4016.md b/test/command/4016.md index 8683c0056..54cbc78c3 100644 --- a/test/command/4016.md +++ b/test/command/4016.md @@ -14,11 +14,11 @@ </div> ^D \begin{frame}{Level 2 blocks} -\protect\hypertarget{level-2-blocks}{} +\phantomsection\label{level-2-blocks} \begin{columns}[T] \begin{column}{0.4\textwidth} \begin{block}{Block one} -\protect\hypertarget{block-one}{} +\phantomsection\label{block-one} \begin{itemize} \tightlist \item @@ -29,7 +29,7 @@ \begin{column}{0.6\textwidth} \begin{block}{Block two} -\protect\hypertarget{block-two}{} +\phantomsection\label{block-two} \begin{itemize} \tightlist \item diff --git a/test/command/4690.md b/test/command/4690.md index 9a46823cc..454f31171 100644 --- a/test/command/4690.md +++ b/test/command/4690.md @@ -12,7 +12,7 @@ content2 :::: ^D \begin{frame}{title} -\protect\hypertarget{title}{} +\phantomsection\label{title} \begin{columns}[T] \begin{column}{0.08\textwidth} content diff --git a/test/command/6030.md b/test/command/6030.md index 0801f9173..0e156a2db 100644 --- a/test/command/6030.md +++ b/test/command/6030.md @@ -83,11 +83,11 @@ Two \end{frame} \begin{frame}{Three} -\protect\hypertarget{three}{} +\phantomsection\label{three} Three \begin{block}{Four} -\protect\hypertarget{four}{} +\phantomsection\label{four} Four \end{block} \end{frame} diff --git a/test/command/6107.md b/test/command/6107.md index 2f9d3353c..eca60539b 100644 --- a/test/command/6107.md +++ b/test/command/6107.md @@ -16,11 +16,11 @@ Not much to do about nothing. \section{A circus!}\label{a-circus} \begin{frame}{Another silly title} -\protect\hypertarget{another-silly-title}{} +\phantomsection\label{another-silly-title} Not much to do about nothing. \end{frame} \begin{frame}[standout]{Epilogue} -\protect\hypertarget{epilogue}{} +\phantomsection\label{epilogue} \end{frame} ``` diff --git a/test/command/6360.md b/test/command/6360.md index 794307a83..1f8a9a6f2 100644 --- a/test/command/6360.md +++ b/test/command/6360.md @@ -2,5 +2,5 @@ % pandoc -f native -t latex --wrap=none [Link ("test2",[],[]) [Str "link",Space,Str "to",Space,Str "text",Space,Str "test1"] ("#test1","")] ^D -\protect\hypertarget{test2}{\hyperref[test1]{link to text test1}} +\phantomsection\label{test2}\hyperref[test1]{link to text test1} ``` diff --git a/test/command/7278.md b/test/command/7278.md index dcf71c29f..1ed490e6d 100644 --- a/test/command/7278.md +++ b/test/command/7278.md @@ -17,21 +17,21 @@ text in block text in block ^D \begin{frame}{Slide} -\protect\hypertarget{slide}{} +\phantomsection\label{slide} Some blocks: \begin{exampleblock}{example block title} -\protect\hypertarget{example-block-title}{} +\phantomsection\label{example-block-title} text in block \end{exampleblock} \begin{alertblock}{alert block title} -\protect\hypertarget{alert-block-title}{} +\phantomsection\label{alert-block-title} text in block \end{alertblock} \begin{block}{block title} -\protect\hypertarget{block-title}{} +\phantomsection\label{block-title} text in block \end{block} \end{frame} diff --git a/test/command/7857.md b/test/command/7857.md index 9fc128b5d..424fac8b5 100644 --- a/test/command/7857.md +++ b/test/command/7857.md @@ -15,7 +15,7 @@ ok \note{my note} \begin{frame}{slide} -\protect\hypertarget{slide}{} +\phantomsection\label{slide} ok \end{frame} ``` |
