When including code for a figure between two paragraphs in RMarkdown, the float resulting from the code might be placed at a different place of the document, leaving space between the paragraphs in the LaTeX/PDF output file.
The following minimal example illustrates the issue:
---
output: pdf_document
---
This is my first paragraph.
Next in the Rmd file would be the code for the figure.
The figure is correctly treated as a float by LaTeX, meaning that LaTeX will put it to the next suitable position.
```{r pressure, echo=FALSE, fig.cap="Example", out.width='0.5\\textwidth', fig.pos='B'}
plot(pressure)
```
The problem now is, that this leaves space to my second paragraph.
Usually, this should not be the case.
In pure LaTeX, there is a linebreak, of course, but no vertical space in addition.
If you put \vspace{-\parsep} at the start of the following text, you'll get the line break without the extra space. That is,
---
output: pdf_document
---
This is my first paragraph.
Next in the Rmd file would be the code for the figure.
The figure is correctly treated as a float by LaTeX, meaning that LaTeX will put it to the next suitable position.
```{r pressure, echo=FALSE, fig.cap="Example", out.width='0.5\\textwidth', fig.pos='B'}
plot(pressure)
```
\vspace{-\parsep}The problem is now fixed.
I don't see any other way to do this: knitr puts blank lines around the figure environment when it generates the LaTeX.
Related
I think the question is quite self-explanatory but for avoidance of doubt I'll explain with more detail below:
I have an R Markdown document that works well if converted to HTML or uploaded to GitHub. When converting to PDF (using Latex), the results are not so pretty. I find that the biggest problem in a Latex PDF document are line breaks. I can fix the line breaks issue on the PDF document by adding "\ " characters, but that throws my HTML document out of whack too.
Is there a way to manually add line breaks (or "space before/after paragraphs") for the PDF output only?
Thank you!
You can redefine the relevant spacings in the YAML header. \parskip controls the paragraph spacing. Code blocks are shaded using a snugshade environment from the framed package. We can also redefine the shaded environment for code blocks to have some vertical space at the start. Here's a reproducible example. Note: I also added the keep_tex parameter so you can see exactly what the generated tex file looks like, in case this is useful:
title: "test"
author: "A.N. Other"
header-includes:
- \setlength{\parskip}{\baselineskip}
- \renewenvironment{Shaded}{\vspace{\parskip}\begin{snugshade}}{\end{snugshade}}
output:
pdf_document:
keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Once you output to HTML, you can just print the HTML webpage as PDF. that might be an easy way keep the original format
I'm working in Rmarkdown and knitting to PDF. I want to start a paragraph with (a) without creating a list. Adding \(a) works, but it highlights the rest of my Rmd file text, making it hard to read. Is there another way to indicate that I do not want to create a list in Rmarkdown?
---
title: "Untitled"
output:
pdf_document: default
html_document: default
---
This is the first paragraph.
```{r, include=FALSE}
1+1 # nice highlighting before problem
```
(a) This should be the second paragraph. (b) But it is interpreted as a list.
\(a) This slash works to keep the text as a paragraph, but it highlights all remaining text in my Rmd file, which makes it hard to read. (b) Is there another method to use (a) without creating a list?
Here is a good list, but still wrong color in Rmd:
(a) One
(b) Two
```{r, include=FALSE}
1+1 # bad highlighting after problem
```
Try putting (a) in <span> like
<span> (a) </span> This should be the second paragraph.
It's a bit hacky but it works for html & pdf and it fixes the highlighting problem.
Instead of the element placing a on the previous line works. If there is actual text on the previous line then ending the line with two spaces also works.
I am trying to add a cover page which is already created in a PDF.
```{r echo=FALSE,out.width='8.27in',out.height='11.69in'}
knitr::include_graphics('CoverPage.pdf')
```
I want to change the margins of the first page only. Can you please help me with this?
The following was tried but it changes the margin of the whole document:
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
If you are fine with some bare LaTeX in your RMD file, then this can be easily achieved using the geometry package. Just define a newgeometry before the cover is included and restoregeometry afterwards (reads almost like English …).
---
output:
pdf_document
---
```{r, echo = FALSE, results = "asis"}
cat("\\newgeometry{left=3cm,right=3cm,top=2cm,bottom=2cm}")
knitr::include_graphics("CoverPage.pdf")
cat("\\restoregeometry")
```
\clearpage
Note the position of the page number. Restoring the margins was successful!
It is important to use the chunk option results="asis" and cat() to print raw LaTeX and to escape the backslashes with an additional backslash.
I am reasking this question:
How to wrap text around plots in R Markdown?:
'Currently the default for R Markdown in R is to have one line of text in line with the plot but this looks very awkward and I would like to save space by having the text wrap around the plot (plot aligned left, with text wrapping on the right).'
It appears there is an easier way now by adding two arguments to the r chunk:
out.width= "65%", out.extra='style="float:right; padding:10px"'
This scales the plot to 65% of the page width and lets text float on the left side with a distance of 10px.
You can use CSS styling to position the elements however you want. However there can be some difficulties and it does require some tweaking. Here is a simple example:
---
title: "Untitled"
author: "Ian Wesley"
date: "April 21, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
<div style= "float:right;position: relative; top: -80px;">
```{r pressure, echo=FALSE}
plot(pressure)
```
</div>
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Yields:
I want to insert a picture at the beginning of text using a code block, not markdown. I am using the Tufte handout template output: rmarkdown::tufte_handout and when I insert it straight after the YAML header but before TOC like this:
\centering
![width='100%'](./cropped-banner_efpt.jpg)
\raggedright
\tableofcontents
\clearpage
the image then spans the main body. I know that with chunks there is an option to have the chunk to span the whole page placing fig.fullwidth = TRUE in the chunk header, but I am a bit stuck with this as I am not generating any graph from data and I do not know how to simpy place an image from within a chunk.
Another matter was that when I set toc: true in the YAML header, the image would only come after the inserted toc - that is why I am inserting toc with the latex command.
Thank you for your suggestions.
When a figure is not generated from R code, you may use knitr::include_graphics() to insert it to the document, e.g.
```{r echo=FALSE, out.width='100%'}
knitr::include_graphics('./cropped-banner_efpt.jpg')
```