From a9498a15687e072b1694aa037c1fc96554310cc1 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 29 Mar 2022 09:02:39 -0700 Subject: [PATCH] LaTeX writer: support `page`,`trim`,`clip` attributes on images. These are actually supported by `\includegraphics`, though this is not well documented. See https://tex.stackexchange.com/questions/7938/pdflatex-includegraphics-and-multi-page-pdf-files Partially addresses #7181. --- src/Text/Pandoc/Writers/LaTeX.hs | 15 +++++++++------ test/command/7181.md | 11 +++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 test/command/7181.md diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index 778e13d9a..2f4190139 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -930,7 +930,7 @@ inlineToLaTeX il@(Image _ _ (src, _)) | Just _ <- T.stripPrefix "data:" src = do report $ InlineNotRendered il return empty -inlineToLaTeX (Image attr _ (source, _)) = do +inlineToLaTeX (Image attr@(_,_,kvs) _ (source, _)) = do setEmptyLine False modify $ \s -> s{ stGraphics = True } opts <- gets stOptions @@ -953,10 +953,13 @@ inlineToLaTeX (Image attr _ (source, _)) = do Height | isJust (dimension Width attr) -> [d <> "\\textheight"] _ -> [] - dimList = showDim Width <> showDim Height - dims = if null dimList - then empty - else brackets $ mconcat (intersperse "," dimList) + optList = showDim Width <> showDim Height <> + maybe [] (\x -> ["page=" <> literal x]) (lookup "page" kvs) <> + maybe [] (\x -> ["trim=" <> literal x]) (lookup "trim" kvs) <> + maybe [] (\_ -> ["clip"]) (lookup "clip" kvs) + options = if null optList + then empty + else brackets $ mconcat (intersperse "," optList) source' = if isURI source then source else T.pack $ unEscapeString $ T.unpack source @@ -964,7 +967,7 @@ inlineToLaTeX (Image attr _ (source, _)) = do inHeading <- gets stInHeading return $ (if inHeading then "\\protect\\includegraphics" else "\\includegraphics") <> - dims <> braces (literal source'') + options <> braces (literal source'') inlineToLaTeX (Note contents) = do setEmptyLine False externalNotes <- gets stExternalNotes diff --git a/test/command/7181.md b/test/command/7181.md new file mode 100644 index 000000000..e2cf7a731 --- /dev/null +++ b/test/command/7181.md @@ -0,0 +1,11 @@ +``` +% pandoc -t latex +![Global frog population.](slides.pdf){page=13,trim=1cm,clip,width=4cm} +^D +\begin{figure} +\centering +\includegraphics[page=13,trim=1cm,clip,width=4cm]{slides.pdf} +\caption{Global frog population.} +\end{figure} + +```