【问题标题】:Use LaTeX expressions inside a kable table in a Revealjs presentation在 Revealjs 演示文稿中的 kable 表中使用 LaTeX 表达式
【发布时间】:2023-01-13 20:04:44
【问题描述】:

自从 Quarto 发布以来,我一直热衷于使用 Revealjs 幻灯片来展示数据项目,这是一种很好且非常互动的格式。

就像在 Rmarkdown 中一样,在幻灯片中使用 LaTeX 表达式很容易:$for_inline_exp$$$centered_displayed_exp$$

我在表内使用 LaTeX 表达式时从未遇到过问题(感谢 kableExtra),它产生以下输出(参见 colnames):

但是,我不确定 Revealjs 当前是否支持它,但是尽管使用了 escape = FALSE,colnames 显示如下:$cos^2 (F_1)$

为了避免它,我不喜欢在 Revealjs 表中使用 LaTeX,这给出了:

所以,对于我的问题,有谁知道是否有办法在 Revealjs 输出中使用 kable 中的 LaTeX 表达式?

这是一些代码的示例:

依赖关系

library(kableExtra)
library(ggplot2)
library(tidyverse)

随机数据帧

x <- data.frame(cbind(rnorm(5, mean=50, sd=10),
                      rnorm(5, mean=50, sd=10),
                      rnorm(5, mean=50, sd=10)))

names(x) <- c("some text", "$Cont(F_1)$","$\\frac{1}{n^2}$")

凯布尔

color_1 = ifelse(x[,1] > mean(x[,1]),
       "green","red")

x %>%
kable(booktabs = T ,escape = F, align = 'c') %>%
    kable_classic(full_width = F,
                  position = "center",font_size = 35) %>%
column_spec(2,color = color_1)

【问题讨论】:

  • 你好,你能分享一个可重现的例子吗?
  • 确实忘记分享了,上面已经更新了

标签: r latex kable kableextra quarto


【解决方案1】:

您可以将 format = "markdown" 添加到您的 kable() 功能。请注意,这会破坏您的颜色编码。请参阅 here 以了解为什么 kable 在四开本中的工作方式与 Rmarkdown 不同。

编辑 1:

您可以组合使用 format = "latex"[parse-latex](https://github.com/tarleb/parse-latex) 过滤器:

---
format: revealjs
filters:
  - parse-latex
---


```{r}
library(kableExtra)
library(ggplot2)
library(tidyverse)

x <- data.frame(cbind(
  rnorm(5, mean = 50, sd = 10),
  rnorm(5, mean = 50, sd = 10),
  rnorm(5, mean = 50, sd = 10)
))

names(x) <- c("some text", "$Cont(F_1)$", "$\frac{1}{n^2}$")
color_1 <- ifelse(x[, 1] > mean(x[, 1]),
  "green", "red"
)

x %>%
  kable(booktabs = T, escape = F, align = "c", digits = 2, format = "latex") %>%
  kable_classic(
    full_width = F,
    position = "center", font_size = 15
  ) %>%
  column_spec(2, color = color_1)
```

【讨论】:

  • 谢谢你的回答,但是,还有办法减小表的大小吗? kable_styling() 由于输出而无法工作。
  • 您可以尝试将两个 kable 绑定在一起,一个只有 kable 标题(格式:markdown),另一个带有没有列标题的列,包括 kable_Styling(格式:html)。但是说实话,这不是一个非常优雅的解决方案..
  • 我做了一个编辑。
猜你喜欢
  • 2018-06-13
  • 2018-01-05
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
相关资源
最近更新 更多