Skip to content

Commit

Permalink
Propagate extra attributes at the figure AND image level
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed Sep 27, 2024
1 parent 2562d7e commit 7e49525
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

pandoc-plot uses [Semantic Versioning](http://semver.org/spec/v2.0.0.html)

## Release 1.9.1

* Fixed an issue where extra parameters were not passed down to `pandoc`, depending on the output format (#38).

## Release 1.9.0

* Added support for [Mermaid](https://mermaid.js.org/), thanks to a contribution by Sanchayan Maity (#74).
Expand Down
33 changes: 32 additions & 1 deletion docs/manual-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ There are parameters that affect the figure that will be included in your docume
dependencies=[...]
file=(path)
executable=(path)
caption_format=(text)
caption_format=(text),
...
}
# script content
```
Expand All @@ -219,6 +220,36 @@ All following parameters are optional, with their default values controlled by t
* `executable` is a path to the executable to use (e.g. `C:\\python3.exe`) or the name of the executable (e.g. `python3`).
* `caption_format` is the text format of the caption. Possible values are exactly the same as `pandoc`'s format specification, usually `FORMAT+EXTENSION-EXTENSION`. For example, captions in Markdown with raw LaTeX would be parsed correctly provided that `caption_format=markdown+raw_tex`. See Pandoc's guide on [Specifying formats](https://pandoc.org/MANUAL.html#specifying-formats).

**All parameters not understood by `pandoc-plot` will be forwarded to pandoc**. For example, the following script:

````markdown
Paragraph

```{.graphviz width=50mm height=60mm}
digraph D {

A [shape=diamond]
B [shape=box]
C [shape=circle]

A -> B [style=dashed, color=grey]
A -> C [color="black:invis:black"]
A -> D [penwidth=5, arrowhead=none]

}
```
````

contains two extra parameters, `width=50mm` and `height=60mm`. When rendered with `pandoc --filter pandoc-plot -i example.md -o example.tex`, the output will be:

````latex
\begin{figure}
\centering
\includegraphics[width=50mm,height=60mm]{plots/11623886912816197297.png}
\caption{}
\end{figure}
````


#### Code highlighting

Expand Down
2 changes: 1 addition & 1 deletion pandoc-plot.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: pandoc-plot
version: 1.9.0
version: 1.9.1
synopsis: A Pandoc filter to include figures generated from code blocks using your plotting toolkit of choice.
description: A Pandoc filter to include figures generated from code blocks.
Keep the document and code in the same location. Output is
Expand Down
9 changes: 6 additions & 3 deletions src/Text/Pandoc/Filter/Plot/Embed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ figure as fp caption' =
-- so that pandoc-plot plays nice with pandoc-crossref and other filters
figureWith as (simpleCaption (plain caption')) $
plain $
imageWith mempty (pack fp) mempty caption'
imageWith as (pack fp) mempty caption'

-- TODO: also add the case where SVG plots can be
-- embedded in HTML output
Expand All @@ -111,19 +111,22 @@ figure as fp caption' =
-- |]

latexInput :: Attr -> FilePath -> Inlines -> PlotM Block
latexInput _ fp caption' = do
latexInput (_, _, attrs) fp caption' = do
renderedCaption' <- writeLatex caption'
let renderedCaption =
if renderedCaption' /= ""
then [st|\caption{#{renderedCaption'}}|]
else ""
-- We need to propagate extra attributes, for example, to control
-- figure sizes. See #38
let renderedExtraAttributes = mconcat $ [key <> "=" <> value | (key, value) <- attrs]
return $
RawBlock
"latex"
[st|
\begin{figure}
\centering
\input{#{pack $ normalizePath $ fp}}
\includegraphics[#{renderedExtraAttributes}]{#{pack $ normalizePath $ fp}}
#{renderedCaption}
\end{figure}
|]
Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pandoc/Filter/Plot/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ parseFigureSpec block@(CodeBlock (id', classes, attrs) _) = do
extraAttrs = Map.toList extraAttrs'
blockAttrs = (id', filter (/= cls toolkit) classes, filteredAttrs)

debug $ "Propagating attributes unrelated to pandoc-plot: " <> tshow blockAttrs

let blockDependencies = parseFileDependencies $ fromMaybe mempty $ Map.lookup (tshow DependenciesK) attrs'
dependencies = defaultDependencies conf <> blockDependencies

Expand Down

0 comments on commit 7e49525

Please sign in to comment.