LaTeX在R Markdown中表格列名中的应用

huangapple go评论55阅读模式
英文:

LaTeX in column names of a table in R markdown

问题

如何在 HTML 输出的列名中使用 LaTeX 符号,如 α 或 δ,使用 gt 包。

library(gt)

# 创建一个数据框
data <- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# 将列名设置为 LaTeX 公式
colnames(data) <- c("$\\alpha$", "$\\beta$")

# 创建一个 gt 表格
gt(data)
英文:

How can I have LaTeX symbols like alpha or delta in column names of html output with gt package.

library(gt)

# Create a data frame
data &lt;- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# Set the column names as LaTeX formulas
colnames(data) &lt;- c(&quot;$\\alpha$&quot;, &quot;$\\beta$&quot;)

# Create a gt table
gt(data)

LaTeX在R Markdown中表格列名中的应用

答案1

得分: 2

以下是您要翻译的代码部分:

library(gt)

# 创建一个数据框
data <- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# 使用Unicode设置列名
colnames(data) <- c("α", "β")

# 创建一个gt表格
gt(data)

LaTeX在R Markdown中表格列名中的应用

英文:

If you do not mind using unicode, this works but I appreciate it does not use LaTeX equation mode.

library(gt)

# Create a data frame
data &lt;- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# Set the column names with unicode
colnames(data) &lt;- c(&quot;\u03B1&quot;, &quot;\u03B2&quot;)

# Create a gt table
gt(data)

LaTeX在R Markdown中表格列名中的应用

答案2

得分: 1

由于我无法使用gt软件包找到解决方案,我切换到modelsummarydatasummary_df()。这是另一个在Markdown文件中生成HTML输出的很棒的软件包。它可以接受完整的LaTeX语法。

library(modelsummary)

# 创建一个数据框
data <- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# 将列名设为LaTeX公式
colnames(data) <- c("$\\alpha + \\overline{x}$", "$\\beta$")

datasummary_df(data)
英文:

Since I could not find a solution with the gt package, I switched to modelsummary and datasummary_df(). Another great package that produces html output in markdown files. It can take full LaTeX syntax.

library(modelsummary)

# Create a data frame
data &lt;- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# Set the column names as LaTeX formulas
colnames(data) &lt;- c(&quot;$\\alpha + \\overline{x}$&quot;, &quot;$\\beta$&quot;)

datasummary_df(data)

LaTeX在R Markdown中表格列名中的应用

huangapple
  • 本文由 发表于 2023年7月3日 19:18:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定