Skip to content

Commit

Permalink
LaTeX writer: support page,trim,clip attributes on images.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jgm committed Mar 29, 2022
1 parent 7a7e1b2 commit a9498a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Text/Pandoc/Writers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -953,18 +953,21 @@ 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
source'' <- stringToLaTeX URLString source'
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
Expand Down
11 changes: 11 additions & 0 deletions test/command/7181.md
Original file line number Diff line number Diff line change
@@ -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}
```

0 comments on commit a9498a1

Please sign in to comment.