Latex Figures appear before text in pandoc markdown

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
    %...
    
    \begin{figure}[H]
    %...
    \caption[<ToC>]{<regular>}
    \end{figure}
    

    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
    %...
    
    \noindent\begin{minipage}{\textwidth}
    %...
    \captionof{figure}[<ToC>]{<regular>}
    \end{minipage}
    %...
    

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:

\usepackage{float}
\floatplacement{figure}{H}

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')`"
output:
  rmarkdown::pdf_document:
    fig_caption: yes        
    includes:  
      in_header: figure_placement.tex
---

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


A simple solution is to add a line with a backslash and space immediately after the figure, followed by a blank line:

![Alt text](image.png)
\ 

Some text after the figure...

Do not forget the space after the backslash! This seems to work on Pandoc 1.12.4.2.

Edit: as pointed out in the comments, this will suppress figure captions.


I don't like the solution to use a 2-step compilation (Latex -> sed -> pdf).

You can overwrite the figure environment in your Latex template:

% Overwrite \begin{figure}[htbp] with \begin{figure}[H]
\usepackage{float}
\let\origfigure=\figure
\let\endorigfigure=\endfigure
\renewenvironment{figure}[1][]{%
  \origfigure[H]
}{%
  \endorigfigure
}

In this way you can still use the direct pdf generation.