问题
I am trying to copy the first answer in this question.
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.
Here's the code
---
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="中文中文")
```
And it gives an error says
! 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?
Many thanks.
回答1:
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
Result:
BTW, it is often helpful to follow the advice on https://yihui.org/tinytex/r/#debugging and set
options(tinytex.verbose = TRUE)
来源:https://stackoverflow.com/questions/60200275/changing-the-latex-engine-gets-an-error-when-trying-to-align-images-on-kable