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

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

LaTeX in column names of a table in R markdown

问题

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

  1. library(gt)
  2. # 创建一个数据框
  3. data <- data.frame(
  4. x = c(1, 2, 3),
  5. y = c(4, 5, 6)
  6. )
  7. # 将列名设置为 LaTeX 公式
  8. colnames(data) <- c("$\\alpha$", "$\\beta$")
  9. # 创建一个 gt 表格
  10. gt(data)
英文:

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

  1. library(gt)
  2. # Create a data frame
  3. data &lt;- data.frame(
  4. x = c(1, 2, 3),
  5. y = c(4, 5, 6)
  6. )
  7. # Set the column names as LaTeX formulas
  8. colnames(data) &lt;- c(&quot;$\\alpha$&quot;, &quot;$\\beta$&quot;)
  9. # Create a gt table
  10. gt(data)

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

答案1

得分: 2

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

  1. library(gt)
  2. # 创建一个数据框
  3. data <- data.frame(
  4. x = c(1, 2, 3),
  5. y = c(4, 5, 6)
  6. )
  7. # 使用Unicode设置列名
  8. colnames(data) <- c("α", "β")
  9. # 创建一个gt表格
  10. gt(data)

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

英文:

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

  1. library(gt)
  2. # Create a data frame
  3. data &lt;- data.frame(
  4. x = c(1, 2, 3),
  5. y = c(4, 5, 6)
  6. )
  7. # Set the column names with unicode
  8. colnames(data) &lt;- c(&quot;\u03B1&quot;, &quot;\u03B2&quot;)
  9. # Create a gt table
  10. gt(data)

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

答案2

得分: 1

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

  1. library(modelsummary)
  2. # 创建一个数据框
  3. data <- data.frame(
  4. x = c(1, 2, 3),
  5. y = c(4, 5, 6)
  6. )
  7. # 将列名设为LaTeX公式
  8. colnames(data) <- c("$\\alpha + \\overline{x}$", "$\\beta$")
  9. 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.

  1. library(modelsummary)
  2. # Create a data frame
  3. data &lt;- data.frame(
  4. x = c(1, 2, 3),
  5. y = c(4, 5, 6)
  6. )
  7. # Set the column names as LaTeX formulas
  8. colnames(data) &lt;- c(&quot;$\\alpha + \\overline{x}$&quot;, &quot;$\\beta$&quot;)
  9. 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:

确定