8

I am putting together an Rmarkdown PDF document with the following YAML settings:

---
output: 
  pdf_document:
    fig_caption: true 
    fig_crop: true
    toc_depth: 3
  header-includes:
  - \usepackage{hyperref}
 ---

Within the body of the document I've inserted a few PNG images, using the following syntax

Paragraph 1..........

![Caption](path/image.png)

Paragraph 2....

And when the document is rendered, the image appears as expected within the text, between Paragraph 1 and Paragraph 2. However, I am getting some unpredictable results where the rendered image appears after Paragraph 2 in some cases and I can't manage to solve it.

rbatt
  • 4,677
  • 4
  • 23
  • 41
Ryan Erwin
  • 807
  • 1
  • 11
  • 30

6 Answers6

6

I have run into the same problem. It appears if you wrap the image in "paragraph" tags, the image will be in-line with the text.

Paragraph 1...

<p>
![](image.png)
</p>

Paragraph 2...
troh
  • 1,354
  • 10
  • 19
  • 1
    It does work for PDF. I was knitting to pdf and I got the correct result after adding the `

    ` tag.

    – troh Apr 24 '18 at 13:40
  • 1
    Hmm... well, maybe something weird is happening in pandoc. But

    is HTML, not TeX, so this is a pretty odd hack.

    –  Apr 25 '18 at 10:01
4

When tackling similar issues, I've used \FloatBarrier (from the placeins package) to control positioning. I'm not the most experienced knitr rmarkdown LaTeX user, but I've had success with that before.

Basically, the images "float"; you can control what the can't float past by inserting a barrier. That description is crude, but you might find the technique effective.

rbatt
  • 4,677
  • 4
  • 23
  • 41
  • 2
    `\FloatBarrier` is correct to confine floating, but I don't think it's what the OP is looking for. He expects the figure not to float at all. Unfortunately, this is not possible when writing RMD, see [this answer by Yihui](http://stackoverflow.com/a/17648350/2706569). Moreover, you should add that `\FloatBarrier` requires the `placeins` package. – CL. Dec 08 '15 at 09:29
  • @user2706569, So, at least the way I envisioned the solution, what I've asked is not possible? – Ryan Erwin Dec 08 '15 at 17:41
  • Well, you can use `\FloatBarrier` after the image which prevents the figure from appearing *after* the barrier. However, the "clean LaTeX" solution would be to use the `H` position from the `float` package. But *this* is not possible when writing markdown. Why do you write markdown when you want PDF output? Quoting Yihu (link above): "If you choose Markdown for its simplicity, you should not expect too much power from it, even with powerful tools like `pandoc`. Bottom line: *Markdown is not LaTeX.* It was designed for HTML instead of LaTeX." – CL. Dec 08 '15 at 17:47
  • You use it because you're writing R code, and want to create a dynamic report. Right? Like I said in answer, I'm no expert in any of these things; I just happened to have resolved a similar issue for myself. In my mind, "why don't you just write it in LaTeX?" is a pretty intense request. Notice the difference between what you're asking "why use markdown when you want pdf?" and what @YiHui is suggesting "don't expect much". That difference is huge, and key. – rbatt Dec 09 '15 at 06:36
  • @user2706569 see above (sorry for double comment) – rbatt Dec 09 '15 at 06:41
  • @rbatt I'm aware that moving from markdown to LaTeX involves (some|a lot of|...) effort and I don't want to offend your answer. My point is, it seems like many people write markdown with the goal of producing PDFs, and afterwards they wonder how to control certain details of the PDF. However, this is not what markdown is designed for – basically these problems are [XY problems](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). My suggestion to write LaTeX adressed the `X`; your answer provides a (probably the best) solution for `Y`. – CL. Dec 09 '15 at 08:14
3

\newline seems to work.

History and Overview of R

![R programming](Images/R.PNG)\newline

enter image description here

gbganalyst
  • 364
  • 4
  • 8
1

&nbsp; will insert vertical space

to keep your figure captions make sure the ![] is still in a separate paragraph (separated by blank lines above and below) in the Rmarkdown document

### Heading 1 ![This is my figure caption](`r fig_var`){width=400px} 

&nbsp;

### Heading 2

where fig_var is an r variable that contains the full path to the figure image

eeny
  • 111
  • 1
  • 3
0

One option is to add

\newpage

to act in a similar way as the Floatbarrier. It is not to elegant but seems to work.

For greater clarity consider the example from above:

Paragraph 1..........

![Caption](path/image.png)

Paragraph 2....

To avoid the image to move in front of the second paragraph, you could do the following:

Paragraph 1..........

![Caption](path/image.png)

\newpage

Paragraph 2....
0

There is no correct answer to this.

  1. Try adding fig.show='hold' to keep your images where they should be
  2. The paragraphs might be skipped because Latex will try and fit the text/images with least space.

I sorted my issue out using (1) and to "work with" (2), you can use \pagebreak in an appropriate position depending on what is before and after paragraphs 1 and 2.

This can be done only after seeing the pdf result, by better fitting of the image in question into potentially a next page (more space). Of course, it would also mean adding the page break elsewhere (e.g before or after any of the p1, p2 or the image).

Hiwa
  • 587
  • 6
  • 21