Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
samuel-emrys opened this issue Feb 28, 2022 · 11 comments
Open

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

samuel-emrys opened this issue Feb 28, 2022 · 11 comments

Comments

@samuel-emrys
Copy link

samuel-emrys commented Feb 28, 2022

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
Copy link
Owner

LaurentRDC commented Feb 28, 2022

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
Copy link

mfhepp commented Mar 16, 2022

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
Copy link
Owner

@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:

# 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
Copy link
Author

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
Copy link
Owner

@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
Copy link
Author

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
Copy link
Owner

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:

\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:

\includegraphics{plots/pandocplot6487719276475405948.png}

I'll try to make a release today

@LaurentRDC
Copy link
Owner

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

SanchayanMaity added a commit to SanchayanMaity/pandoc-plot that referenced this issue Aug 15, 2023
See LaurentRDC#37.

For whatever reason, the change introduced in the PR related to the
issue above seems to not have an effect any more. Bring that change
back. Seems we might have problems with pandoc-crossref and other
filters because of which the change was dropped but right now this
is the only filter we care about.
@fischerling
Copy link

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:

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


but this script:

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

will result in this:

\includegraphics{plots/pandocplot6487719276475405948.png}


I'll try to make a release today

This behaviour has changed since v1.5.1 for recent pandoc version.

With pandoc 3.1.9 and pandoc-plot 1.9.1 both plots get converted to figures.

Input:

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

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

Result:

\begin{figure}
\centering
\includegraphics{plots/pandocplot16601784956282164673.png}
\caption{hello}
\end{figure}

\begin{figure}
\centering
\includegraphics{plots/pandocplot16601784956282164673.png}
\caption{}
\end{figure}

@LaurentRDC
Copy link
Owner

I found the problem, which arose when pandoc-types added the figureWith function.

Do you need a new release for this?

@LaurentRDC LaurentRDC reopened this Sep 27, 2024
@fischerling
Copy link

Thank you very much! Empty captions are omitted again since a125b56.
I build pandoc-plot from source so I do not need a github release.
But maybe it is required by some distro packaging policy though.

SanchayanMaity added a commit to SanchayanMaity/pandoc-plot that referenced this issue Oct 3, 2024
See LaurentRDC#37.

For whatever reason, the change introduced in the PR related to the
issue above seems to not have an effect any more. Bring that change
back. Seems we might have problems with pandoc-crossref and other
filters because of which the change was dropped but right now this
is the only filter we care about.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants