strange symbols generated from rmarkdown when I use Chinese characters in ggplot2 - r

Recently, I encounter a problem in Rmarkdown.
When I add Chinese characters into xlab() and ylab() in ggplot2. The pdf document generated by rmd will display strange ...... symbol. However, I try to search useful something and the result told me I should add dev: cairo_pdf. Ok, after I add the code to YAML, the new error strange symbols occur again. It displays another strange symbols of □□□.
I want to ask for help from someone who may pay attention to my quesiton. Could you help me? Thank you very much. And here my test code:
---
title: "test.Rmd"
author: "author"
documentclass: ctexart
output:
rticles::ctex:
fig_caption: yes
number_sections: yes
toc: no
dev: cairo_pdf
word_document: default
keywords:
header-includes: \usepackage{booktabs} \usepackage{longtable} \usepackage{array} \usepackage{multirow}
\usepackage{wrapfig} \usepackage{float} \floatplacement{figure}{H}
---
library(ggplot2)
ggplot(cars,aes(speed,dist)) +
geom_line()+xlab("速度\nSpeed") + ylab("距离\nDist")

Sorry, I cannot replicate your images. I just altered the header-includes and included the figure in a code chunk. Did you install xelatex and TeX Live?
Here is the code that worked fine for me:
---
title: "test.Rmd"
author: "author"
documentclass: ctexart
output:
rticles::ctex:
fig_caption: yes
number_sections: yes
toc: no
dev: cairo_pdf
word_document: default
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \floatplacement{figure}{H}
---
```{r}
library(ggplot2)
ggplot(cars,aes(speed,dist)) +
geom_line()+xlab("速度\nSpeed") + ylab("距离\nDist")
```
-output

Related

Compiling latex document in R Markdown

I am trying to compile a latex document in R markdown using R studio. These are the header options -
---
header-includes:
- \usepackage{float}
- \usepackage{wrapfig}
- \usepackage{graphicx}
- \usepackage{lipsum}
- \usepackage[fontsize=12pt]{scrextend}
- \usepackage{fontspec}
- \newcommand*{\bibfont}{\footnotesize}
- \PassOptionsToPackage{dvipsnames}{xcolor}
- \usepackage[dvipsnames]{xcolor}
- \usepackage{etoolbox}
- \defbibheading{bibliography}[\textcolor{Blue}\refname]{}
- \usepackage{blindtext}
- \usepackage{needspace}
- \usepackage{mwe}
output:
pdf_document:
latex_engine: xelatex
fig_caption: true
citation_package: biblatex
bibliography: Gibbs.bib
mainfont: ArialMT
geometry: margin=0.70in
---
And this is the error I get -
! Undefined control sequence.
<recently read> \defbibheading
Any help? Thanks!
The problem is that while rmarkdown will automatically load the biblatex package due to your citation_package: biblatex option, it will do so only after it has already processed your header includes. This means you can't directly use a macro like \defbibheading{} from the biblatex package in your header-includes.
As a workaround, you could try
---
header-includes:
- \usepackage{float}
- \usepackage{wrapfig}
- \usepackage{lipsum}
- \usepackage[fontsize=12pt]{scrextend}
- \usepackage{fontspec}
- \PassOptionsToPackage{dvipsnames}{xcolor}
- \usepackage[dvipsnames]{xcolor}
- \usepackage{etoolbox}
- \AtBeginDocument{\renewcommand*{\bibfont}{\footnotesize}\defbibheading{bibliography}[\textcolor{blue}{\refname}]{}}
- \usepackage{blindtext}
- \usepackage{needspace}
- \usepackage{mwe}
output:
pdf_document:
keep_tex: true
latex_engine: xelatex
fig_caption: true
citation_package: biblatex
bibliography: Gibbs.bib
mainfont: ArialMT
geometry: margin=0.70in
---
test

Is there a proper way to include images in the margins a an R Bookdown project?

I am working towards writing a textbook using R Bookdown, and I want to include pictures in the margins of the text. However, after trying multiple methods none of them seem to actually populate the image in the correct place. I have tried all the options in r bookdown manual and the instructions for using tufte style https://bookdown.org/yihui/rmarkdown/tufte-figures.html.
To make sure I am not having problems with conflicting packages I have been creating a fresh project then pasting the following code into the end of the index.Rmd file.
{r fig-margin, fig.margin=TRUE}
knitr::include_graphics('apple.png')
^![](C:\Users\n298l621\Documents\TESTING\apple.png)
{marginfigure, echo = TRUE}
knitr::include_graphics('apple.png')
The following image is the result of me either knitting the index.Rmd document or building the whole book using the build menu:
As you can see, the images are populating but certainly not in the margin. Any help or guidance would be greatly appreciated!
I do not know how to use r chunk inserting picture in the margin. Here is my document using latex in Rmarkdown insert the compamy logo in left-top margin.
---
documentclass: ctexart
fontsize: 12pt
geometry: a4paper,left=2cm,right=2cm,top=2cm,bottom=3cm
pagesize: a4paper
output:
rticles::ctex:
fig_caption: yes
number_sections: yes
toc: no
mainfont: 更纱黑体 UI SC #LiberationSerif
sansfont: 更纱黑体 UI SC Xlight #LiberationSans
monofont: 等距更纱黑体 SC Light #LiberationMono
header-includes:
- \usepackage{fancyhdr} #load fancyhdr for page header or foot
- \usepackage{booktabs}
# - \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
- \usepackage{xeCJK}
---
\addtolength{\headheight}{1.5cm}
\pagestyle{fancyplain}
\fancyhead[L]{\includegraphics[height=1.2cm]{cdmlogo.png}} # put the logo at top-left
\fancyhead[C]{打印日期\today}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r include=FALSE, label='oracle.connection'}
library(tidyverse)
library(ROracle)
library(kableExtra)
Could you include an example of the yaml front matter which you are using in your .Rmd?
A document produced with the following code:
---
title: "An MWE of tufte"
author: "chefnate119"
output:
tufte::tufte_html: default
---
```{r fig-margin, fig.margin=TRUE}
plot(cars)
```
Produces the following document
and to a pdf
---
title: "An MWE of tufte"
author: "chefnate119"
output:
tufte::tufte_handout: default
---
```{r fig-margin, fig.margin=TRUE}
plot(cars)
```

Right-aligning an image to the page border in a r-markdown PDF document

I'm looking to create a cover page for a PDF document rendered via rmarkdown, with an image cover.png that I'd have right-aligned right up against the page border. In my YAML below, I currently have it centered on the page. How do I move it to the right?
---
title: \vspace{3cm} \fontsize{1.5cm}{2cm}\selectfont Report Title
header-includes:
- \usepackage{titling}
- \pretitle{\begin{center}\includegraphics[width=20cm]{cover.png}\\[\bigskipamount]}
- \posttitle{\end{center}}
- \pagenumbering{gobble}
output:
pdf_document:
latex_engine: xelatex
mainfont: Calibri
editor_options:
chunk_output_type: console
---
You could include raw latex in your Rmd doc that will situate the picture where you want it. For example:
---
title: Dogs are the best
output:
pdf_document:
header-includes:
- \usepackage{graphicx}
---
```{r include = FALSE}
library(httr)
dog <- "https://www.cesarsway.com/sites/newcesarsway/files/styles/large_article_preview/public/Common-dog-behaviors-explained.jpg"
GET(dog, write_disk("dog.jpg", overwrite = TRUE))
```
\begin{figure}
\raggedleft
\begin{minipage}{5cm}
\includegraphics[width = 5cm]{dog.jpg}
\end{minipage}
\end{figure}
\pagebreak
## Introduction
Dogs dogs dogs
Which results in:
I had to add \hspace*{2cm} before \includegraphics to move it to as far right as I want. I used trial and error to move it to the edge of the page. I got the answer from here.
---
title: \vspace{3cm} \fontsize{1.5cm}{2cm}\selectfont Report Title
header-includes:
- \usepackage{titling}
- \pretitle{\begin{center}\hspace*{2cm}\includegraphics[width=20cm]{cover.png}\\[\bigskipamount]}
- \posttitle{\end{center}}
- \pagenumbering{gobble}
output:
pdf_document:
latex_engine: xelatex
mainfont: Calibri
editor_options:
chunk_output_type: console
---

Combine multiple pages of .pdf into RMarkdown

I am using RMarkdown to pull various .pdf's into one central file. These .pdfs are multiple pages in length. The code that I am using is:
---
title: <center> <h1>Analysis Data</h1> </center>
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: {10}
geometry: margin=0.30in
header-includes:
- \usepackage{booktabs}
- \usepackage{sectsty} \sectionfont{\centering}
- \renewcommand{\contentsname}{}\vspace{-2cm}
---
# File One
\begin{center}
\includegraphics[width=9.5in]{~/Desktop/DatasetOne.pdf}
\end{center}
\newpage
# File Two
\begin{center}
\includegraphics[width=9.5in]{~/Desktop/DatasetTwo.pdf}
\end{center}
However, when I knit the final .pdf together, only the first page of each .pdf document ("DatasetOne.pdf" and "DatasetTwo.pdf") are included and not the entire document.
Is it possible to pull through the entire .pdf's rather than just the first page?
Thank you.
I'm able to successfully include two different multi-page PDFs in your example document using pdfpages:
---
title: <center> <h1>Analysis Data</h1> </center>
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: {10}
geometry: margin=0.30in
header-includes:
- \usepackage{booktabs}
- \usepackage{sectsty} \sectionfont{\centering}
- \renewcommand{\contentsname}{}\vspace{-2cm}
- \usepackage{pdfpages}
---
# File One
\includepdf[pages={-}]{pdf1.pdf}
\newpage
# File Two
\includepdf[pages={-}]{pdf2.pdf}

part of square root latex equation dissappears in pdf rendering from RStudio's rmarkdown

When I use this code to make a square root in my pdf document (rendered by rmarkdown):
---
title: "Test"
author: "test test"
geometry: margin=1in
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
html_document:
css: tables.css
number_sections: yes
theme: cerulean
toc: yes
toc_depth: 3
header-includes:
- \usepackage[dutch]{babel}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyfoot[LE,RO]{this is a fancy foot}
- \usepackage{dcolumn}
- \usepackage{here}
- \usepackage{longtable}
- \usepackage{caption}
- \captionsetup{skip=2pt,labelsep=space,justification=justified,singlelinecheck=off}
subtitle: test test test
fontsize: 12pt
---
$$ S_E\text{=}S_x\sqrt{\text{(}\text{1}\text{-}r_{xx}\text{)}} $$
this is the result:
Notice the strange looking square root. Does anyone know what's going wrong here in my code?
I found the solution to this problem here:
https://tex.stackexchange.com/questions/210153/pdftex-font-expansion-error-during-compilation
In the directory C:\Program Files\MiKTeX 2.9\miktex\bin\x64 I ran updmap.exe. After that the problem was solved.

Resources