【问题标题】:passing the value of r variable inside latex equation to display in kable在乳胶方程中传递 r 变量的值以在 kable 中显示
【发布时间】:2019-01-16 03:42:08
【问题描述】:

我必须在 kable 单元内的乳胶方程中显示 r 变量的输出。 当我使用下面的

data2$Physics<-c('$\\frac{7}{20}$')

它在 kable 单元中正确显示。 但问题是当我想传递一个 r 变量时,我尝试了以下所有失败:

data2$Physics<-c('$\\frac{`r phy`}{20}$')

data2$Physics<-c('$\\frac{\\Sexpr{phy}{20}$')

请帮忙

【问题讨论】:

    标签: r rstudio r-markdown knitr


    【解决方案1】:

    您已经在 R 代码块中,不需要内联 R 代码。只需粘贴值:

    data2$Physics <- paste0('$\\frac{', phy, '}{20}$')
    

    如果要控制phy的格式,可以使用sprintf。例如,如果phy 是 3.141593,但您只想显示两位小数,请使用

    data2$Physics <- sprintf('$\\frac{%.2f}{20}$', phy)
    

    phy 中的值按照 C 风格格式 %.2f 进行格式化,并嵌入到字符串中。

    【讨论】:

    • 我浪费了我的两天时间......你的解决方案是如此优雅......谢谢
    猜你喜欢
    • 2015-05-31
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 2015-04-14
    • 2023-03-26
    • 2021-09-03
    • 2019-10-07
    • 2019-09-17
    相关资源
    最近更新 更多