lierdakil / pandoc-crossref

Pandoc filter for cross-references
https://lierdakil.github.io/pandoc-crossref/
GNU General Public License v2.0
943 stars 76 forks source link

Subfigures (with caption) #20

Closed arve0 closed 8 years ago

arve0 commented 9 years ago

Is subfigures possible?

Something like this would be great:


![Caption for (a).](img.png) ![Caption for (b).](img2.png) {#fig:two_figs}

or


![Caption for (a).](img.png) {#fig:one}
![Caption for (b).](img2.png) {#fig:two}

Referencing directly to [@fig:one].
lierdakil commented 9 years ago

That's not something that's possible with Pandoc at the moment, so no, and I think managing subfigures "properly" would be quite a nightmare.

I might suggest that there is another possible solution, if it would be possible to treat a general div as a referenceable element. For example,

<div id="fig:subfigures" class="subfigures" data-caption="Caption for figure">
![Caption for subfigure (a).](img.png)

![Caption for subfigure (b).](img.png)
</div>

Reference [@fig:subfigures (a)], or [@fig:subfigures] (a), or @fig:subfigures (b)

That would be relatively simple to implement, at the cost of user being responsible for tracking subfigure references (not a big deal IMO, since there are rarely more than 4 subfigures, and that's quite manageable). Note that subfigure captions won't be automatically prefixed with Figure ... (but to my experience, that would be desired behavior anyway)

You'd be on your own with converting this to LaTeX though, I don't think I can hack together something even remotely serviceable, since my memories on subfigures in LaTeX are hazy at best (never really got into those).

Out of the box, this would work reasonably well with HTML-based output (some css hacking might be required), and not at all with Docx and ODT (I don't think those two formats even have a concept of subfigure, besides both are a horrible mess to work with).

lierdakil commented 9 years ago

There might be a better convention for div caption, data-caption attribute is just something from the top of my head.

ivotron commented 9 years ago

I know scholarly markdown and its implementation does it, but have no idea how.

arve0 commented 9 years ago

Thanks for the quickly response! Sounds like I'll have to latex subfigures :happy:

lierdakil commented 9 years ago

@ivotron, well, it doesn't work with docx, while pandoc-crossref (in most cases) does. That's saying something at least.

As I said, managing subfigures "properly" would be a nightmare. One has to consider localization, different output formats, different number formatting, and gods know what else. Scholarly doesn't offer much in terms of customisation, I don't think. And subfigures could be numbered in a multitude of ways, f.ex.

Numbering figures with arabic numerals is somewhat more ubiquitous, since those are usually acceptable even in countries with hieroglyphic writing. Not as much with subfigures.

lierdakil commented 8 years ago

This somewhat works in released versions, with some improvements in master. Feel free to open new issues if something doesn't work as expected.

gandalfsaxe commented 6 years ago

@lierdakil What is the status on subfigures with LateX? It sounds like you finished something (also judging from #48 ), so what did you finish?

lierdakil commented 6 years ago

When target format is LaTeX, subfigure blocks will be converted to LaTeX subfig package and subfloat directives. This is a bit limited compared to other formats, mostly due to LaTeX being a bit too smart, but it works more or less as one would expect.

gandalfsaxe commented 6 years ago

Usually when people use subfigures, it's because they want to have two figures side-by-side. In the demo document pdf, the two subfigures are just below each other in the same size of all the others figures in the document.

So is the subfigure support just for labelling them a) and b)? Is there a way to achieve them being side by side?

lierdakil commented 6 years ago

A particular alignment depends on image size and the presence of line breaks. If your images are 100% of page width, those can't be reasonably expected to be rendered side by side. See Pandoc's and pandoc-crossref's documentation for more details on setting image size and subfigures typesetting options.

Besides, subfigures are a semantic element, not a formatting option. You might just as well format figures to render side by side without subfigures if you really wanted to.

gandalfsaxe commented 6 years ago

Can you explain in other words? I'm not sure I understand. What is the point of this semantic element? And where does the subfig LaTeX package come in, that you mentioned?

Wouldn't it be valuable to be able to convert some custom syntax (could be HTML as <div id="fig:subfigures"> as in your example) into a LaTeX subfig or subcaption environment where it would just auto adjust the two figures as sharing the width of the page, side by side as subfigures? :)

lierdakil commented 6 years ago

I don't have time for this back and forth, especially not in a long-closed issue. LaTeX doesn't auto-adjust image size. Use width attribute if you have to. Open a new issue if you formulate a feature request. Or if you have a concrete question that you need help with.

gandalfsaxe commented 6 years ago

Ok one final comment then, let me know if you want me to create a new issue.

I basically want what Scholarly Markdown provides, i.e. what ivotron linked to earlier in this issue. Specifically this: http://scholarlymarkdown.com/Scholarly-Markdown-Guide.html#floating-subfigs

I.e. the ability to quickly and easily make subfigures side-by-side just by having multiple !()[] in consecutive lines. In their example, this:

![look at me](sealbaby.jpg){#sealA width=50%}\
![and also me](sealbaby.jpg){#sealB width=30%}
![](sealbaby.jpg){#sealC width=same}

Caption: Look at all my baby seals!

Displays like this:

image

However Scholarly Markdown, which is itself a pandoc fork, seems pretty abandoned, and pandoc+pandoc-crossref already does everything I want, except basic subfigures. I know I'm pushing Markdown/pandoc-crossref to the limit and some would say "just use LaTeX", but writing in Markdown have so many advantages.

lierdakil commented 6 years ago

Sigh... This is described in the docs https://lierdakil.github.io/pandoc-crossref/#subfigures

<div id="fig:fig1">
![Cool horse](img1.png){width=50%}

![Book horse](img2.png){width=30%}
![Pink horse](img3.png){width=30%}

Look at my tiny horses!
</div>

will render like this with -t latex -o file.pdf: image

But there's a caveat: the same won't work for most other output formats, due to Pandoc not liking multiple figures on the same line in the same paragraph. That's where subfigGrid option comes in, which will typeset this thing in a table based on the first paragraph (note that has to quite literally be a grid, since IIRC Pandoc can't do rowspans). Obviously not exactly equivalent to LaTeX, but close enough for most uses.

P.S. And yes you can reference subfigure elements individually if those have identifiers starting with fig:. I omitted it for the sake of simplicity.

gandalfsaxe commented 6 years ago

Sorry, I just looked in the demo. I thought all functionality would be showcased there, but I should also have checked the docs. Looks good!

One last thing, is there a way to add a little spacing between subfigures? I.e. in your example above, a little spacing between your Book horse and Pink horse? :)

ahoereth commented 6 years ago
<div id="fig:fig1">
![Cool horse](img1.png){width=50%}

![Book horse](img2.png){width=30%}\hfill
![Pink horse](img3.png){width=30%}

Look at my tiny horses!
</div>
gandalfsaxe commented 6 years ago

Ah yes, \hfill of course! Thanks a lot, this is perfect.

mb21 commented 6 years ago

The discussion how this may be implemented best natively in pandoc has picked up again, feedback/your-experience welcome! https://github.com/jgm/pandoc/issues/3177

ashwinvis commented 5 years ago

Using a <div> element works as suggested in the thread. I just want to make it clear that to convert to LaTeX, one should include the following too (perhaps in a file like header.tex and call pandoc --include-in-header header.tex ...):

\usepackage{subfig}