19

From the pandoc documentation I know how to insert an image

http://johnmacfarlane.net/pandoc/README.html#images

Now I want to align two images in the same row, how can I do that? My desired output format is pdf.

finefoot
  • 9,914
  • 7
  • 59
  • 102
LittleSweet
  • 534
  • 2
  • 6
  • 9

4 Answers4

22

You can try something like this:

![](tests/lalune.jpg)\ ![](tests/lalune.jpg)

This will give you two side-by-side images. You won't have a caption, though; pandoc only treats an image as a captioned figure if it is by itself in a paragraph.

John MacFarlane
  • 8,511
  • 39
  • 33
  • Thanks, it works. But I have another problem now. My images are a little large, and when put in the same row they cannot fit into one slide. Is it possible to control the size of the image? – LittleSweet Apr 03 '13 at 14:02
  • You can't specify the size in Markdown. You have to modify your image externally – ivotron Oct 15 '13 at 18:23
  • I feel like http://tex.stackexchange.com/a/139205/13192 should be able to be combined with http://www.johndcook.com/blog/2009/01/14/how-to-display-side-by-side-figurs-in-latex/ or similar to allow raw latex commands with `\subfigure`, but when I tried it, I got an error (but I've since merged the figures, and I didn't copy down the error, sorry). – naught101 Jun 04 '14 at 06:03
  • 3
    The figure size issue has been addressed in more recent versions of pandoc. You can now use ![](tests/lalune.jpg){height=3cm} This should give your images all the same height. – Ben K. Jun 24 '16 at 08:47
15

Expanding on from John's answer, if you do want a single caption under two side by side figures, a 'hack' would be to do this:

![](https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png){width=60%}
![](https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png){width=40%}
\begin{figure}[!h]
\caption{A single caption for the two subfigures}
\end{figure}

This results in one caption for two images placed side by side. You might need to tweak each individual image's width setting, or the !h caption placement specifier to get things looking like this:

hacked caption

I found this helpful because you don't have to download the picture off the internet as in a pure LaTeX \subfigure solution. I.e. just use pandoc markdown to get the image, and LaTeX to generate the caption.

If you want to go crazy, you can actually use the same idea above to make subfigure captions like so:

![](https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png){width=60%}
![](https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png){width=40%}
\begin{figure}[!h]
\begin{subfigure}[t]{0.6\textwidth}
\caption{Caption for the left subfigure}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.4\textwidth}
\caption{Caption for the right subfigure}
\end{subfigure}
\caption{A single caption for the two subfigures}
\end{figure}

enter image description here

Edit 20180910:

You'll need to include the following packages in the pandoc YAML frontmatter/header:

header-includes: |
    \usepackage{caption}
    \usepackage{subcaption}
weiji14
  • 477
  • 7
  • 14
  • I am getting a "! LaTeX Error: Environment subfigure undefined." error if I include the code as you suggested. – Akshay Gaur Sep 07 '18 at 06:54
  • 2
    Oops, forgot to mention that you'll need to include the packages. In pandoc you would have ```header-includes: | \usepackage{caption} \usepackage{subcaption}``` I'll edit the answer to be clearer. – weiji14 Sep 10 '18 at 02:34
  • I had a conflict, and had to remove subfigures package, to use the subcaption package. It works now though. – Ben Butterworth Aug 30 '20 at 23:29
  • Sometimes the caption (Figure 1. ...) goes into next page. I am trying to fix this by wrapping around a minipage or vbox but I am not capable of doing so. It is the first time I try to use such environments in Pandoc and I dont know if its even possible. My current error message is `l.500 ...backslash begin\{minipage\}\{\linewidth\}`. I am sure someone thought of this. Any ideas? – manolius Nov 26 '20 at 11:32
  • I know this is an old thread, but how did you center those? – Slendi Dec 22 '20 at 12:24
  • Well, finally I decided to go full latex just for figures. And for the case where I don't want floating figures, I use [this](https://tex.stackexchange.com/questions/226313/how-can-i-insert-multiple-images-with-captions-in-a-non-floating-environment) approach using `captionof` – manolius Jun 02 '21 at 12:27
0

One simple way is convert your file first to a .tex file, where you can adjust your image alignment with LaTeX command minipage or so. Then you could obtain your .pdf file running latex or pandoc in command line. See Pandoc Demos for example.

gthink
  • 29
  • 5
0

You could use a preprocessor like gpp to include options like align image. Or you could do it like how John told you:

![](tests/lalune.jpg)\ ![](tests/lalune.jpg)
Shruti Kapoor
  • 1,106
  • 2
  • 12
  • 32
  • This is a duplicate of [John's accepted answer](https://stackoverflow.com/a/15755523/3817004). – Uwe Feb 10 '19 at 15:01