gpt4 book ai didi

latex - 如何在 Rmarkdown 文档中创建 Latex 表?

转载 作者:行者123 更新时间:2023-12-02 03:22:23 24 4
gpt4 key购买 nike

我正在寻找将 latex 表插入到 .rmd 文件中。但是,当我尝试编译 pdf 时,我收到此错误(我已经能够在背页中重现该表)。

---
title: "Test"
author: "me"
date: "1/27/2019"
output:
pdf_document:
keep_tex: yes
---

## Table

\begin{table}[]
\centering
\begin{tabular}{|l|c|c|}
\hline
& Sad & Happy \\ \hline
Short & \begin{tabular}[c]{@{}l@{}}Sam\\ Beth\end{tabular} & \begin{tabular}[c]{@{}l@{}}Jim\\ Sara\end{tabular} \\ \hline
Tall & \begin{tabular}[c]{@{}l@{}}Erin\\ Ted\end{tabular} & \begin{tabular}[c]{@{}l@{}}Bob\\ Ava\end{tabular} \\ \hline
\end{tabular}
\caption{My caption}
\label{my-label}
\end{table}

#ERROR
! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=`}\fi \hrule \@height \arrayrulewidth \futurelet...
l.105 \textbackslash{} \hline
Tall \&

这是我希望表格的样子 enter image description here

最佳答案

我不太清楚为什么会出现这个错误。另一种解决方案是将 kable 与包 kableExtra 结合使用:

```{r}
library(knitr)
library(kableExtra)
df <- data.frame(Cat = c("Short", "Tall"),
Sad = linebreak(c("Sam\nBeth", "Erin\nTed")),
Happy = linebreak(c("Jim\nSara", "Bob\nAva")))
kable(df, col.names = c("", "Sad", "Happy"), escape = F, caption = "My caption") %>%
kable_styling(latex_options = "hold_position")
```

enter image description here

关于latex - 如何在 Rmarkdown 文档中创建 Latex 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54394289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com