I am writing a report.pdf in R Markdown and want to include a pdf as a figure.
The chunk looks like this:
include_graphics("path/to/figure.pdf")
The chunk works fine, but since the figure has a white margin, it is not clear on the output page where the figure starts/ends. Therefore, I would like to add a simple frame around the figure.
Does knitr provide that option?
I've looked for similar questions on rseek.org, scanned through Yihui's book on bookdown and searched the knitr package documentation without finding anything helpful.
Following this post:
---
output: pdf_document
header-includes: |
\usepackage[export]{adjustbox}
\let\includegraphicsbak\includegraphics
\renewcommand*{\includegraphics}[2][]{\includegraphicsbak[frame,#1]{#2}}
---
```{r}
knitr::include_graphics("images/example.pdf")
```
Result:
Related
I am looking to find a way to remove the italicized comments in Rmarkdown code chunks
---
output:
pdf_document:
keep_tex: true
number_section: no
---
```{r}
# This is a test.
```
And the results.
Is there a way to make it not italicized?
Thank you,
Several code highlight styles do not italicize the comments, including haddock, kate, and zenburn. See here for a preview of available styles.
---
output:
pdf_document:
keep_tex: true
number_section: no
highlight: kate
---
If you need to create a custom syntax highlighting theme, instructions in the pandoc manual are here.
This is what I typically put at the top of my documents in R markdown:
---
title: Title
author: Amar Al-Zubaidi
date: \today
mainfont: Inconsolata Nerd Font
sansfont: Inconsolata Nerd Font
monofont: Inconsolata Nerd Font
fontsize: 11pt
table-use-row-colors: true
bibliography: /home/amarakon/Documents/bibliography.bib
csl: /home/amarakon/Documents/Citations/apa-no-initials.csl
classoption: letterpaper
output:
pdf_document:
keep_tex: true
latex_engine: xelatex
template: eisvogel
number_sections: true
---
I do pretty much the same thing for every document.
In fact, the only thing I change for every document is the title.
I want to know how I can put all of these values in a file (except for the title), and change my compile command to read that file when compiling. This is the command I use to compile:
R -e "rmarkdown::render('file.Rmd')"
My end goal is to only have to put something like this in my Rmarkdown document:
---
title: my-title
---
I answered a similar question not that long ago using this information.
Since I had worked on that question, I only had to clone my last template and update the skeleton to create your template.
You won't have a YAML with just your title. However, all of the elements that you specified you wanted would be pre-written in the RMD file when you open a new file using this template.
You can see this repository at fraupflaume/customYAML2.
To install this template (it's not a package per se; there's no library):
devtools::install_github("fraupflaume/customYAML2")
When you want to start a new RMD script, go to templates -> Custom YAML -> customYAML2.
If you wanted to change this down the road, you can fork or clone that repository and edit it to your heart's content.
BTW:: let me know if you want me to remove the images. (It has your name in them: here and on Github.)
I'm using rmarkdown in rstudio and want to knit to pdf. This works well, except for when I use kable. I'm using the following yaml and it used to work in the past, but now I had to reinstall R, Rstudio and MacTex and now it doesn't work anymore.
---
title: "example"
output:
pdf_document:
df_print: kable
latex_engine: xelatex
---
```{r}
cars
```
The error message I get is:
output file: example.knit.md
! LaTeX Error: Environment kable-table undefined.
Error: LaTeX failed to compile example.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Study-1.log for more info.
Does anyone know how to solve this?
This is a bug of rmarkdown (against higher versions of Pandoc), which I just fixed on Github. Please try:
remotes::install_github('rstudio/rmarkdown')
everybody!
I usually output .RMarkdown as html file and use the following to create an in-text table.
variable|Description
--------|-----------
`var1` |abcdefg
`var2` |gfedcba
But it doesn't apply to pdf output. Because I want to keep variable names as var1, I prefer not to use code chunk to generate the table. I also tried Latex syntax and something I found online (e.g. below). However, none of these approaches works.
variable Description
-------- --------------
`var1` abcdefg
`var2` gfedcba
-------- --------------
|variable|Description|
|:-------|----------:|
|`var1` |abcdefg |
|`var2` |gfedcba |
It shouldn't be this twisted. Does anybody have a simple solution to this? Thank you very much!
There is a hack that can solve your problem. Export the file as HTML format (as it works fine in this way), open the file on browser and print[CTRL+P] and save as pdf.
I am writing a book with bookdown. Unfortunately, I have no clue how to format (e.g. setting font size) ATX-hearder (#, ##, ## etc.). So far, it does not work via pandoc or preamble.tex.
I have tried the following, with regard to this.
Unfortunately, there is an error message :
\usepackage{titlesec} \titleformat{\chapter}[display] {\normalfont\sffamily\huge\bfseries\color{blue}} {\chaptertitlename\ \thechapter}{20pt}{\Huge}
Thanks in advance!
Your best bet here is to add a LaTeX preamble to the document. In here, you can define the required LaTeX packages. Two changes are made to the base template:
We need to add subparagraph: true to make titlesec work with R Markdown, as explained here
# refers to a level one header in pandoc, and therefore you need to make the style changes for section not chapter https://www.sharelatex.com/learn/Sections_and_chapters
Here is a minimal example
---
output:
pdf_document:
includes:
in_header: header.tex
subparagraph: true
---
# Section
## Subsection
The preamble.tex file is saved in the same directory:
\usepackage{titlesec}
\usepackage{color}
\titleformat*{\section}{\LARGE}
\titleformat{\subsection}[display]
{\normalfont\sffamily\huge\bfseries\color{blue}} {\chaptertitlename\ \thechapter}{20pt}{\Huge}