19

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

  \begin{figure}
  \centering
  {\includegraphics[width=2.5in]{some_figure.png}}
  \caption{Comparing Dq from different p-model}
  \end{figure}

and then I use the following command:

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

and test_fig.sty have:

 \usepackage{graphicx}

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

migrated from stackoverflow.com Mar 10 '13 at 1:37

This question came from our site for professional and enthusiast programmers.

15

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.

  • +1 for \floatplacement{figure}{...}. I have a customised template for using with pandoc, and couldn't figure out how to set the default float placement to {!htbp} - setting it with \floatplacement does the trick. – seanlano Oct 6 '17 at 3:41
  • Same! I'm using pandoc and \floatplacement{figure}{H} in a custom template prevents images from floating to top of the page. – josephdpurcell Oct 21 '18 at 11:25
15

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.

  • Worked for me, nice tips for little projects (reports, articles) without to have to override templates. Thanks – Metal3d Nov 16 '15 at 13:43
  • I like this one too - but am curious as to what that backslash does? – Thomas Kimber Dec 22 '15 at 12:14
  • 2
    In Pandoc Markdown, a backslash followed by a newline generates a hard line break. The tex file will have an inline image (no figure environment) followed by a hard line break. The image will be without caption. The same can be achieved with spaces add the and of the line (after the image) and then no empty lines before the next paragraph. – hlg Feb 4 '16 at 10:02
5

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.

  • Probably the better solution for me. No sed, only a include in header to append to the command line. Thanks – Metal3d Apr 14 '17 at 8:29
2

Pandoc generates tex output with all of the tables and figures defined with the [htbp] placement options. This isn't a massive problem though, since you can use sed to change all instances of [htbp] to [H] e.g.

sed -i 's/begin{figure}\[htbp\]/begin{figure}\[H\]/g' tex/out.tex

Basically, just use pandoc to generate the tex output, then run your tex engine afterwards (twice) and you'll be good to go. You'll also need to make sure that the float package is actually being used in your template file (which contains your tex preamble):

\usepackge{float}

NOTE: Although in the default latex template, the float package is used, but it's only used if you're actually using tables:

$if(tables)$
\usepackage{ctable}
\usepackage{float}
$endif$

...I just moved \usepackage{float} to the line outside of the if block in my custom template.

  • Do you mean s/begin{figure}\[[htbp]*\]/begin{figure}[H]/g ? – user10274 Nov 5 '13 at 14:38
  • Nope, the square brackets in this case are literal and not a regex character grouping; [htbp] should become [H]. – agosse Nov 6 '13 at 13:03
  • Wouldn't the \[[htbp]\]' be more robust? It would work for placement involving _any_ subset of htbp. (BTW I use sed` a lot. I wasn't aware of the-i flag. Useful that.) – user10274 Nov 6 '13 at 13:08
  • Sure it will be more robust - in case pandoc changes the way it writes figures in the future. For now, probably not needed, but do whatever makes you happy. :-) – agosse Nov 7 '13 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.