[Tex/LaTex] Latex Figures appear before text in pandoc markdown


I am using pandoc to convert markdown to pdf, but I need to place some figures with more formating than the

  ![Alt text](image.png)

so I use something like this:

  # Document with figures

  This document have figures but they appear before the title

  \caption{Comparing Dq from different p-model}

and then I use the following command:

 pandoc -H test_fig.sty test_fig.md -o test_fig.pdf

and test_fig.sty have:


the resulting pdf have first the figure and then the title.

Best Answer

This is most likely because the figure environment floats, which is not what you're after. For this you have a couple of options:

  1. Add the float package which provides the H float specifier, allowing you to use

    \usepackage{float}% http://ctan.org/pkg/float

    stopping the float from moving around.

  2. Add the caption (or the super-tiny capt-of) package and wrap your figure inside a minipage to keep the image and caption together. Use it as follows:

    \usepackage{caption}% http://ctan.org/pkg/caption
    %\usepackage{capt-of}% http://ctan.org/pkg/capt-of

For more information on the placement of figures, see How to influence the position of float environments like figure and table in LaTeX? and Keeping tables/figures close to where they are mentioned.

The above proposals are purely LaTeX-driven.

If you want to manage this within pandoc, consider adding the following to a file called float_adjustment.tex and place it in your project folder:


Then include this file as part of your preamble using the pandoc header

title: "A title"
author: "An author"
date: "`r format(Sys.time(), '%d %B %Y')`"
    fig_caption: yes        
      in_header: figure_placement.tex

All figures should be forced in-place via the [H]ERE float specification.

Related Question