尝试在 kable 上对齐图像时更改 latex_engine 会出错

Changing the latex_engine gets an error when trying to align images on kable

我正在尝试复制此问题中的

这个答案果然符合我的要求。但是当 latex_engine 是 xelatex.

时会出错

但我真的需要用中文显示输出。

这是代码

---
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

我发现this issue说这个错误是可以避免的,但是我无法对此table进行任何更改。我是否遗漏了可以更改的内容?

非常感谢。

我无法重现错误消息。相反,我得到了汉字的空框,这也没什么用。我很确定您收到的错误消息不是根本原因。我怀疑解决方案是 select 一组支持所需字符的字体。例如,我安装了 Noto 的 CJK 变体,因此 YAML header 的以下添加对我有用:

mainfont: Noto Serif CJK TC
monofont: Noto Sans Mono CJK TC
sansfont: Noto Sans CJK TC

结果:

顺便说一句,遵循 https://yihui.org/tinytex/r/#debugging 上的建议并设置

通常很有帮助
options(tinytex.verbose = TRUE)