问题描述
我正在尝试复制第一个答案这个问题.
I am trying to copy the first answer in this question.
答案确实符合我的要求.但是,当latex_engine是xelatex时,会出现错误.
The answer really meets what I wanted. But it get an error when the latex_engine is xelatex.
但是我真的需要用中文显示输出.
But I really needs to show the output in chinese.
这是代码
---
title: "Untitled"
output:
pdf_document:
latex_engine: xelatex
---
This example highlights the issue I am having with formatting a nice table with the graphics and the vertical alignment of text.
```{r echo=FALSE, results='hide', warning=FALSE, message=FALSE}
## Load modules
library(dplyr)
library(tidyr)
library(ggplot2)
## Create a local function to plot the z score
varianceChart <- function(df, personNumber) {
plot <- df %>%
filter(n == personNumber) %>%
ggplot() +
aes(x=zscore, y=0) +
geom_rect(aes(xmin=-3.32, xmax=-1.96, ymin=-1, ymax=1), fill="orange2", alpha=0.8) +
geom_rect(aes(xmin=1.96, xmax=3.32, ymin=-1, ymax=1), fill="olivedrab3", alpha=0.8) +
geom_rect(aes(xmin=min(-4, zscore), xmax=-3.32, ymin=-1, ymax=1), fill="orangered3") +
geom_rect(aes(xmin=3.32, xmax=max(4, zscore), ymin=-1, ymax=1), fill="chartreuse4") +
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank()) +
geom_vline(xintercept=0, colour="black", alpha=0.3) +
geom_point(size=15, shape=4, fill="lightblue") ##Cross looks better than diamond
return(plot)
}
## Create dummy data
Person1 <- rnorm(1, mean=10, sd=2)
Person2 <- rnorm(1, mean=10, sd=2)
Person3 <- rnorm(1, mean=10, sd=2)
Person4 <- rnorm(1, mean=10, sd=2)
Person5 <- rnorm(1, mean=10, sd=2)
Person6 <- rnorm(1, mean=6, sd=1)
## Add to data frame
df <- data.frame(Person1, Person2, Person3, Person4, Person5, Person6)
## Bring all samples into one column and then calculate stats
df2 <- df %>% gather(key=Person, value=time)
mean <- mean(df2$time)
sd <- sqrt(var(df2$time))
stats <- df2 %>%
mutate(n = row_number()) %>%
group_by(Person) %>%
mutate(zscore = (time - mean) / sd)
graph_directory <- getwd() #'./Graphs'
## Now to cycle through each Person and create a graph
for(i in seq(1, nrow(stats))) {
print(i)
varianceChart(stats, i)
ggsave(sprintf("%s/%s.png", graph_directory, i), plot=last_plot(), units="mm", width=100, height=20, dpi=1200)
}
## add a markup reference to this dataframe
stats$varianceChart <- sprintf('\\raisebox{-.4\\totalheight}{\\includegraphics[width=0.2\\textwidth, height=20mm]{%s/%s.png}}', graph_directory, stats$n)
df.table <- stats[, c(1,2,5)]
colnames(df.table) <- c("Person Name", "Time taken", "Variance Chart")
```
```{r}
library(knitr)
kable(df.table[, c(1,2)], caption="Rows look neat and a sensible distance apart")
kable(df.table, caption="中文中文")
```
它给出了错误提示
! Missing $ inserted.
<inserted text>
$
l.163 ...ers/knitr_try/1.png}}
\tabularnewline
Try to find the following text in test.Rmd:
...ers/knitr_try/1.png}}
You may need to add $ $ around a certain inline R expression `r ` in test.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info.
Execution halted
我发现了此问题,该错误可以避免,但是我可以对此表不做任何更改.我是否缺少可以更改的内容?
I found this issue that says the error can be avoided,But I can't make any changes to this table. Am I missing something that can be changed?
非常感谢.
推荐答案
我无法重现该错误消息.取而代之的是,我得到了一个空白的汉字框,这也不是很有帮助.我很确定您收到的错误消息不是根本原因.我怀疑解决方案是选择一组支持所需字符的字体.例如,我安装了Noto的CJK变体,因此以下对YAML标头的添加对我有用:
I cannot reproduce the error message. Instead, I get empty boxes for the Chinese characters, which isn't all that helpful either. I am pretty sure that the error message you got is not the root cause. I suspect the solution is to select a set of fonts that support the required characters. For example, I have the CJK variant for Noto installed, hence the following addition to the YAML header works for me:
mainfont: Noto Serif CJK TC
monofont: Noto Sans Mono CJK TC
sansfont: Noto Sans CJK TC
结果:
顺便说一句,遵循 https://yihui.org/tinytex上的建议通常会很有帮助/r/#debugging 并设置
BTW, it is often helpful to follow the advice on https://yihui.org/tinytex/r/#debugging and set
options(tinytex.verbose = TRUE)
这篇关于尝试对齐kable上的图像时,更改latex_engine会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!