Placement of pictures in markdown/bookdown - r

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.

Related

How can I make an R Markdown figure chunk hyperlink to the raw image when rendering as HTML?

I would like all of my graphics to link to a full image view once rendered (as the page layout constrains the width and I's like users to be able to zoom in). The chunks are rendered as static images, how do I wrap those in an anchor to the image?
e.g.
```{r "totals", echo=FALSE}
qplot(mtcars$mpg, fill = mtcars$cyl, binwidth = 2)
Renders as
![plot of chunk totals](totals-1.png)
But I want something like
[![plot of chunk totals](totals-1.png)](totals-1.png)
I can wrap the chunk in a markdown link, but that seems a bit clunky and I wondered if there is a way to dynamically code it in the chunk so the image name comes from the chunk name in the same way?

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.

Rmarkdown Image Skips Ahead of Text

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

Centering Text in IPython notebook markdown/heading cells?

I want to customize my notebook, make it more readable and stylish. So for that, I want to start with centering my headers. Is it possible?
You can actually use the markdown mode for the cell and use the normal HTML code, as in
<h1><center>Centered text!</center></h1>
Not directly with markdown i think, but you can just enter HTML in the markdown cells:
<h3 align="center">This is a centered header</h3>
Knowing that in jupyter (ipython) notebook markdown cells the title level is identified by the number of # (# for top level headings or h1, ## for h2, ....), I use the following combination of HTML and markdown:
# <center>Your centered level h1 title</center>
## <center>Your centered level h2 title</center>
etc..
It's straightforward and the easiest to remember but please do comment if there's any disadvantage in doing so.
Tested in jupyter version 4.4.0 (not sure about other version but no reason it doesn't work).
the easy way for me is just :
# <center> some fancy title here
it works without closing the tag, so i usually copy "<center>" and past it away in a single step when i finish.
You can use one of the following lines - text cell.
<center>Your centered level h1 title</center>
<center> some fance title here
from Markdown_Guideline
It's quite simple to make your text stylish in Jupyter-notebook as it's syntax are similar to HTML.
For example, you can use the following commands to play with your texts:
1.**bold_text** will make your test bold like bold_text.
2.*italic_text* will result in italic text like italic_text
3.***bold_italic_text*** will result in bold and italic text like bold_italic_text
4. You can use <h1> header1 </h1> to make the text as header like header1
You can replace h1 within the angular brackets with any number h2,h3 etc. to get header of different sizes.
5. For text alignment you can use Centered_text to align you text to the center.
6. Use <br> to breakline or if you want the following text to be printed in next line.
These are just basic things, you can do many more things.
if you want to make a header you can easily use # at the beginning of text to make it big size and also bold, ## will also do the same but text size will be smaller, you can reduce the size of the bold text by increasing the #s up to 6.

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