Image centering with pandoc markdown
Asked Answered
H

4

26

I need to create documents periodically for Word-using administrators. How can I centre an image using pandoc markdown? I see mention of div blocks, but I have no idea what they are.

![](myimage.png){.center}

With image code such as that above, and a command line such as:

pandoc -s test.md -o test.docx

or

pandoc -s test.md -o test.pdf

I always end up with left-aligned images in my document.

Hazel answered 15/2, 2019 at 0:32 Comment(0)
A
6

For PDF output, using the implicit_figure option will automatically center image on page and allow you to display an title for images.

In the .md document:

![This is an image](path/to/image.png)

Run the following command to generate PDF from you .md

pandoc ./README.md \
  -o ./README.pdf \
  -f markdown+implicit_figures
Anitraaniweta answered 6/10, 2019 at 0:16 Comment(5)
It seems it is the caption that is crucial here - not the implicit_figures extension. Also, this only works when creating pdf documents (not docx).Hazel
implicit_figures is on by default in the markdown input format. It only applies when the caption is there though. It also doesn't really answer the question, which doesn't want any caption but still centering.Circumscissile
The caption will work even if it's empty like ![\ ](image_file.jpg)Roughdry
The caption will work even if it's empty like ![\ ](image_file.jpg), but "Figure 1." will still be printedStadler
\captionsetup[figure]{labelformat=empty} to remove the "Figure: " prefix in the captionReticule
S
4

This has been bugging me since forever; but if you use the markdown -> pandoc -> Latex -> PDF route, this should work:

```{=latex}
\begin{center}
```

![](folder/img.png){ height=30% } 

```{=latex}
\end{center}
```

Note that without the {=latex} wrapper (raw attribute), the above Latex commands will get interpreted as text in the final PDF. References:

Allow to center images without caption in Beamer · Issue #3995 · jgm/pandoc

Pandoc doesn't aim to be a layout engine, see http://pandoc.org/CONTRIBUTING.html#out-of-scope

However, you can change your LaTeX template or even write raw TeX (for both, see the MANUAL).

Raw LaTeX gets interpreted as text in a markdown document. · Issue #4646 · jgm/pandoc

For such complicated raw TeX you should probably use http://pandoc.org/MANUAL.html#extension-raw_attribute

Stadler answered 28/9, 2023 at 3:9 Comment(0)
F
2

If you are using something such as pandoc-template that uses latex underneath, you can insert latex instructions, and they will be interprated.

\begin{center} 
\includegraphics[]{./images/my_image.png}
\end{center}
Fley answered 12/4, 2021 at 16:25 Comment(2)
OP never said they were using latexUnitarian
OP did mention compiling to pdf, however, and this answer did say "if . . . using . . . latex", which is my use case. This is the only answer that worked for me.Preemption
T
1

If you get the image to render as a figure, you can add styling to make it centered. There are two ways to make the image render as a figure:

  1. Give it a caption:

    ![Caption](folder/img.png){ style="width: 70%; margin: auto;" }

  2. For some reason, if you don't provide a caption, Pandoc won't create a figure. In that case you can do it manually:

    <figure>![](folder/img.png){ style="width: 70%; margin: auto;" }</figure>

Transmissible answered 7/6, 2020 at 5:13 Comment(1)
I find only the first works, and only for pdf documents.Hazel

© 2022 - 2024 — McMap. All rights reserved.