LaurentRDC / pandoc-plot

Render and include figures in Pandoc documents using your plotting toolkit of choice
https://laurentrdc.github.io/pandoc-plot/
GNU General Public License v2.0
216 stars 8 forks source link

[feature] Ability to remove the caption from pdf output #37

Closed samuel-emrys closed 2 years ago

samuel-emrys commented 2 years ago

Is there a way to remove the caption from pdf output? When I try something similar to the following:

```{.graphviz}
digraph G {
    a [label = "A"]
    b [label = "B"]
    a->b
}
```

It renders the figure as follows: Screenshot_20220228_152705

This is compiled with:

pandoc --filter pandoc-plot -t beamer presentation.md -o presentation.pdf

I don't see a good way of being able to suppress the figure caption

LaurentRDC commented 2 years ago

This is because pandoc-plot always creates figures (image + caption).

I don't have much free time these days to add the option to change this. Would you be able to make a pull request? Otherwise, I'll get around to it when I get some time

mfhepp commented 2 years ago

I can confirm this has nothing to do with pandoc-plot, because if you transform the same document with

pandoc foo.md --to markdown --standalone ...filters etc... foo-output.md

you will see that nothing except for an image element has been added.

The \caption{} in LaTeX is added by Pandoc; I did not find any way to supress it. The -implicit_figures extension will only work for inline images. So you have to tweak the LaTeX output (e.g. by modifying the Pandoc template).

Spent quite some time on the very same issue. Strangely, Pandoc does not generate the captions for images that use a Data URL, like those from the Mermaid filter for Pandoc.

LaurentRDC commented 2 years ago

@samuel-emrys I'm hesitant to remove the figure numbering for empty captions because of backwards compatibility.

However, here's an example for a small Python filter you can use to transform figures generated from pandoc-plot which have empty captions, into normal images.

Input file:

# title

paragraph 

```{.matplotlib}
import matplotlib.pyplot as plt
plt.plot([1,2,3,4], [1,2,3,4])

Filter:

```python
# empty-captions.py
from pandocfilters import toJSONFilter, Image

def empty_captions(key, value, *_):
    if key == 'Image':
        attrs, caption, (url, title) = value
        if not caption: 
            # pandoc considers the image to be a figure (with numbering)
            # if the title starts with "fig:"
            if title.startswith("fig:"):
                title = title[4::]
            return Image(attrs, [], (url, title))

if __name__ == "__main__":
  toJSONFilter(empty_captions)

Command:

$ pandoc --filter pandoc-plot --filter empty-captions.py -i myfile.md -t latex

Let me know if that is enough for you

samuel-emrys commented 2 years ago

Thanks for that @LaurentRDC, this works great. As the maintainer, this is obviously your decision but I think that an option to toggle this would be well placed within pandoc-plot. Manipulating how figures are generated seems to be directly within the scope of such a filter. As far as backward compatibility goes, you could maintain compatibility with the current API by providing an optional argument that defaults to the current behaviour, but gives those that want to do this an option to turn the captions off. What are your thoughts?

From a user perspective, I think this would be a lot cleaner.

LaurentRDC commented 2 years ago

@samuel-emrys if you insist, I can add a parameter like hide_caption which hides the caption AND turns off figure numbering.

If you want to do it yourself, I'm happy to review pull requests. Otherwise, I'll try to find time this week to do it

samuel-emrys commented 2 years ago

If you want to do it yourself, I'm happy to review pull requests. Otherwise, I'll try to find time this week to do it

I've just taken a quick look at the code base, and not being familiar with Haskell at all I think it would take me longer than a week to work out what I'm doing and where to inject the required changes unfortunately. Thanks a lot for your responsiveness though, I really appreciate it.

LaurentRDC commented 2 years ago

I didn't want to increase complexity for the user with another parameter in the end.

Following the latest commit (f24a95d), the figure numbering will disappear if the caption is empty or unspecified and there's no link to the script source.

So this script:

```{.matplotlib caption="hello"}
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5], [1,2,3,4,5])

will result in the following LaTeX:

```latex
\begin{figure}
\centering
\includegraphics{...} 
\caption{hello}
\end{figure}

but this script:

```{.matplotlib}
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5], [1,2,3,4,5])

will result in this:

```latex
\includegraphics{plots/pandocplot6487719276475405948.png}

I'll try to make a release today

LaurentRDC commented 2 years ago

Release 1.5.1 contains this change. Give it a try and don't hesitate to reopen this issue if there's a problem