Pandoc Markdown to PDF image position
Asked Answered
K

3

40

When converting a Markdown document to PDF with pandoc, my images are not placed in the same position I put them in the source code. I believe this is due to the conversion through LaTeX, but I'm not sure how to remedy this in the Markdown source.

If I use a placeholder image with several paragraphs of sample text and strategically place the image in the source, it becomes too big to fit on the page in the place where I've put it, so the LaTeX layout engine kindly places it on the next page. However, I'd rather this didn't happen because it means the image isn't where I expect and is harder to reference.

I can include an example if necessary, but it's trivial to reproduce and the source needs to be somewhat extensive to fill an entire page.

Kit answered 25/3, 2018 at 23:55 Comment(0)
J
53

Did you try to deactivate the implicit_figures as in

pandoc -f markdown-implicit_figures -t pdf myfile.md

To solve the size problem you could also try to fix the size within the markdown file with an attribute. Something like that can do the trick:

![Caption text](/path/to/image){ width=50% }
Jugendstil answered 28/3, 2018 at 14:49 Comment(3)
-f markdown-implicit_figures works for me. But looks like { width=50% } has no effectSiberson
{ width=50% } has effect if you remove space symbol between ) and {.Tobacconist
This answer seems to be solving the problem, but if removing the +implicit_figures+ then the images do not automatically take a figured-numbered caption.Heartthrob
S
30

Although Bruno's solution for forcing the figures into the specified position works, it also removes the captions specified in the .md file from the resulting .pdf.

To prevent the floating of figures but keep the captions:

1. Create a .tex file with the following content:

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

This will prevent LaTeX from floating the figures.

2. Adapt the pandoc call

Assuming you have named the newly created .tex file disable_float.tex, just add the -H disable_float.tex to your pandoc call:

pandoc -H disable_float.tex input.md -o output.pdf

Props to this answer on SO and this comment on github.

Sec answered 13/11, 2019 at 15:32 Comment(2)
The accepted answer didn't work for me but this approach solved my problem.Wolfhound
I too found this to be the only method that worked for me, but due to the way I programmatically wrote my routines, I did have to provide an absolute path to my tex file.Ibert
I
4

An alternative to @apitsch's answer achieving the same result: an image at the point called in text (although not entirely "in-line" with the text), with its caption.

Do this:

  • In your pandoc --include-in-header template (disable_float.tex in @apitsch's answer) use, instead

    % Override default figure placement To be within the flow of the text rather
    % than on it's own page.
    \usepackage{float}
    \makeatletter
    \def\fps@figure{H} 
    \makeatother
    

    For other default positioning schemes see: https://www.overleaf.com/learn/latex/Inserting_Images#Positioning. E.g. in place of H you could specify t.

  • In your markdown source you'll probably also want to specify a height attribute. In the latex context this is height of \textheight (almost the height of the page).

    ![Caption for my image](MyImage.jpg){height=55%}
    
Indoor answered 5/6, 2021 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.