Rmarkdown Image Skips Ahead of Text - r

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.

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...

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.

\newline seems to work.
History and Overview of R
![R programming](Images/R.PNG)\newline

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}
### Heading 2
where fig_var is an r variable that contains the full path to the figure image

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....

There is no correct answer to this.
Try adding fig.show='hold' to keep your images where they should be
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).

Related

Placement of pictures in markdown/bookdown

I am writing a text in bookdown with Rstudio and want to include a picture within the text like this:
Some text goes here
```{r, fig.cap="\\label{fig:figs}figlabel"}
knitr::include_graphics("images/image.png")
```
Some other text goes here.
However, when I render the book with bookdown::render_book("index.Rmd"), The inserted picture is placed on the next page rather than where it is placed in the text. I want it to be placed between the two sentences, but it is placed below the last one.
Is there a way to control where in the text the image is rendered? I have tried to look at chunk options for images, and also in the bookdown documentation, but neither seem to document ways to control placement of figures.
LaTeX will try to find an optimal location for the figure. You can force the placement of floating figures and tables with \FloatBarrier. Note that doing so, you might end up with a lot of white space on the bottom of the page.
Some text goes here. See fig. \#ref(fig:my-fig)
```{r my-fig, fig.cap="fig caption"}
knitr::include_graphics("images/image.png")
```
\FloatBarrier
Some other text goes here.

R Markdown pdf_document - footer without header

I am adding the following lines to add a customized footer to my pdf document, found here.
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyfoot[LE,RO]{My fancy footer}
The issue is, I get also a header, which includes first and second chapter titles. As my chapter titles sometimes are long, they overlap. I wanted to ask how to remove the header and keep the footer. If it is easier, I am interested to keep only the first titles in the header.
Adding \fancyhead{} will get you an empty header. Check the user manual for the fancyhdr LaTeX package to find out how to create custom headers and footers.
If you'd like to remove both the header and the header horizontal rule, you can add the following to your doc preamble:
\fancyhead{}
\renewcommand{\headrule}{}
You can find a similar answer at How to remove the top horizontal bar in fancyhdr.

RMarkdown table captions breaks page bottom margin

Whenever I use captions in tables,
like
kable(df[1:10, c(7,1:6)], caption = "This is a caption")
the tables and content in the pdf generated by knitr are pushed below the limits of the bottom margin, thus becoming unreadable. Sometimes entire sections are missing, hidden off margins.
Also, plots positions go crazy: they are printed anywhere but the right place in the pdf.
using results="asis" in chunk options doesn't help.
Using pander causes the same problems.
If I remove all table captions and use some \newpage in the .rmd,
the pdf margins are fine.
Is there a safe way to use table captions?
The pdf in question is here: see page 14 for an entire section missing and table hiding in the bottom margins. Also, the plots are where they want, like if they had proper needs...
github repo
this is kind of a anti-climax,
but as it happens,
this problem was being caused by chunks that printed var values to the document, something like:
```{r}
sampled.values <- sample(1:100, 10)
sampled.values
```
when rendered through render(), this code chunk prints the value of sampled.values and this ruins the pdf pagination.
That's it: all page bottom margins are ok now that I removed all those var calls.

How to end a header 3 box in rmarkdown beamer madrid presentation?

When using the Madrid beamer theme (and some others) the Header 3 is rendered as a box covering the whole section, but in markdown the header is only specified at the start. How can I specify the end of the section?
In the example below, I want the second sentence to be inside the box, but the third to be back on the white background.
Example:
---
title: "Example"
output:
beamer_presentation:
colortheme: beaver
theme: Madrid
---
## Slide with Box
Hello, I'm not in the box
### This is a box
And this text is in the box!
I don't want to be in the box, but I am.
Output
This works for me:
## Slide with block 2
Hello, I'm not in the block.
\begin{block}{This is a block}
And this text is in the block!
\end{block}
Hi, I'm not in the block either!
With newer versions of Pandoc (I have 2.2.3.2), you can use fenced divs to enclose a block. Did not work for me previously with 1.6.
Thus, this would work:
## Slide with Box
Hello, I'm not in the box
::: {.block}
### This is a box
And this text is in the box!
:::
I don't want to be in the box, and I am not.
::: {.block} and ::: delimit the block. See https://github.com/jgm/pandoc/issues/2957
An advantage with respect to using \begin{block} and \end{block} is that you get to use markdown within the block.

Subsections in reStructuredText

I'm trying to write a document in reStructuredText, but am currently facing a problem.
I want the document to have a title, this is going to be centered, then immediately after that I want a subsection.
I tried doing the following
##############
Title
##############
+++++++++
Subtitle
+++++++++
content
But when I convert this to PDF, it makes both the title and subtitle centered.
From the reStructuredText quick start guide titles and sub-titles are specified as follows (emphasis mine):
To indicate the document title in reStructuredText, use a unique adornment style at the beginning of the document. To indicate the document subtitle, use another unique adornment style immediately after the document title.
So in the reST example in the question, Subtitle is being formatted as a sub title and not a section heading since the adornment style used around Subtitle is not used anywhere else in the document. In the following, this adornment is used around two section headings, so is not unique and not treated as a subtitle:
##############
Document Title
##############
+++++++++++++++
Section 1 Title
+++++++++++++++
Section 1 content...
+++++++++++++++
Section 2 Title
+++++++++++++++
Section 2 content...
Have a play with http://www.tele3.cz/jbar/rest/rest.html This allows you to quickly try out some simple reStructuredText and test things like sub-titles vs. section titles.
Edit: Alternatively you could put some text between your title and section heading (like an abstract, for example).
P.s. I tend you use adornments above and below a heading to indicate a document title and sub-title and a single adornment below a heading to indicate a (sub-)section title. This makes it easy to see what I intended to be my title/sub-title. For example:
==============
Document title
==============
-----------------
Document subtitle
-----------------
Section
=======
Sub-section
-----------
etc.
I think it's quite natural to have a subsection start immediately under a section heading, as in
MY THINGS
My First Thing
...
My Second Thing
...
A work-around I found by trial and error is to put "\ " (backslash-space) as null content between the section heading and the subsection heading. With rst2html this has the desired effect without introducing any unwanted space.

Resources