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.
Related
Text in the code section of a Tufte formatted, Rmarkdown document does not seem to wrap. I have tried several options and suggestions, but nothing works. I am guessing that all of the other suggestions I could find on StackOverflow did not work, because of the "tufte" format. I am using knitr to construct HTML or PDF documents. Here is the minimal example:
---
title: "Width Minimal Example"
output:
tufte::tufte_handout: default
tufte::tufte_html: default
---
```{r setup, include=FALSE}
library(tufte)
library(formatR)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = TRUE, cache.extra = packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```
```{r, tidy='styler', tidy.opts=list(width.cutoff=50)}
filename <- "/Test/test/test/test/test/test/test/test/test/test/test/test.data"
```
When I use knitr to produce HTML or PDF (handout), the "filename" variable runs out of the textbox allotted for code. I've looked through the "tufte.css" file and nothing there seems to describe the width of text in code boxes.
Ideally, the text in the code box would not only wrap, but indent a bit so that the person reading the code would easily be able to see which lines were new lines and which lines were wrapped lines. Thanks!
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
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.
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')
```
I'm producing a solutions manual for a book, using .Rmd files with the following YAML header:
---
title: "DDAR: Solutions and Hints for Exercises"
date: "`r Sys.Date()`"
output:
word_document:
reference_docx: solutions-setup.docx
---
where I control the general layout of the document with the reference_docx to get an output Word document.
There will be many figures, and I'd like to set some global graphics parameters to give relatively tight bounding boxes and reasonable font sizes
in the figures without having to tweak each one from what I see in a PDF document.
I tried the following, but the par() setting doesn't seem to have any effect:
{r setup, echo=FALSE}
options(digits=4)
par(mar=c(5,4,1,1)+.1)
Instead I get images like the following in my document with larger bounding boxes than I would like and with much larger font sizes than I would like.
I know how to control all this in .Rnw files produced with LaTeX, but I
can't find how to do it in .Rmd -> Word. Is there a chunk hook I could
use? I don't think that there is an out.width chunk option that re-scales
a figure as in LaTeX.
#scoa's answer shows how to use a hook to set some graphical parameters at the beginning of each chunk. This is necessary because "by default, knitr opens a new graphics device to record plots and close it after evaluating the code, so par() settings will be discarded", i.e. graphical parameters for later chunks cannot be set in an early setup-chunk but need to be set for each chunk separately.
If this behavior is not wanted, the package option global.par = TRUE can be used:
opts_knit$set(global.par = TRUE)
Finding the correct values for the margins is sometimes quite painful. In these cases, hook_pdfcrop can help. In all chunks where the option crop = TRUE, white margins will be removed. To apply this to all chunks, use
library(knitr)
knit_hooks$set(crop = hook_pdfcrop)
opts_chunk$set(crop = TRUE)
This works for docx output as well because "when the plot format is not PDF (e.g. PNG), the program convert in ImageMagick is used to trim the white margins" (from ?hook_pdfcrop).
Note that under some circumstances, cropping plots has the side effect of sometimes apparently different "zoom" factors of plots: This happens in cases where we start with identical sized elements on two plots but larger white margins around one of the plots. If then both are resized to a fixed output width after cropping, elements on the plot with larger margins look larger. However, this is not relevant for docx output because out.width/out.height cannot be used in that case.
The knitr documentation for hooks actually uses small margins as an example of what you can do with hooks. Here is a solution (adapted from this documentation).
---
output: word_document
---
```{r setup, echo=FALSE}
library(knitr)
knit_hooks$set(small.mar = function(before, options, envir) {
if (before) par(mar=c(5,4,1,1)+.1) # smaller margin on top and right
})
opts_chunk$set(small.mar=TRUE)
```
```{r}
plot(iris$Sepal.Length)
```
Using opts_chunk$set(small.mar=TRUE) is a way to avoid passing it to every chunk in the document.
The margin appears fixed (screenshot from the docx output in libreoffice with default reference-docx).