【问题标题】:R / Sweave formatting numbers with \Sexpr{} in scientific notationR / Sweave 用科学记数法 \Sexpr{} 格式化数字
【发布时间】:2011-12-03 07:36:17
【问题描述】:

我刚开始用Sweave/R 编写一些文档,我喜欢\sexpr{} 命令,它可以让一个拖车直接在文本中写入数字。

如果我有一个像mus=0.0002433121 这样的数字,我可以说将它四舍五入到小数位,例如

\Sexpr{round(mus,7)}

如何用科学记数法写出来,即LaTeX 会输出

2.43 \times 10^{-4} 

在这个例子中,我们可以控制要输出的有效数字的数量像 3 吗?

我注意到,如果我指定,sigma = 2000000 这样的数字会自动写入2e + 06

\Sexpr{round(sigma,2)}. 

我希望它写成

2 \times 10^6 

与我们在LaTeX 表示法中得到的相同,也许还可以让我们控制有效数字的数量。

如何做到这一点?

【问题讨论】:

    标签: r latex sweave


    【解决方案1】:

    我认为这个功能应该可以工作:

    sn <- function(x,digits)
    {
      if (x==0) return("0")
      ord <- floor(log(abs(x),10))
      x <- x / 10^ord
      if (!missing(digits)) x <- format(x,digits=digits)
      if (ord==0) return(as.character(x))
      return(paste(x,"\\\\times 10^{",ord,"}",sep=""))
    }
    

    一些测试:

    > sn(2000000)
    [1] "2\\\\times 10^{6}"
    > sn(0.001)
    [1] "1\\\\times 10^{-3}"
    > sn(0.00005)
    [1] "5\\\\times 10^{-5}"
    > sn(10.1203)
    [1] "1.01203\\\\times 10^{1}"
    > sn(-0.00013)
    [1] "-1.3\\\\times 10^{-4}"
    > sn(0)
    [1] "0"
    

    如果您想要数学模式的结果,您可以在 paste() 调用中输入 $ 符号。

    编辑:

    这是一个 Sweave 示例:

    \documentclass{article}
    
    \begin{document}
    <<echo=FALSE>>= 
    sn <- function(x,digits)
    {
      if (x==0) return("0")
      ord <- floor(log(abs(x),10))
      x <- x / 10^ord
      if (!missing(digits)) x <- format(x,digits=digits)
      if (ord==0) return(as.character(x))
      return(paste(x,"\\\\times 10^{",ord,"}",sep=""))
    }
    @
    
    Blablabla this is a pretty formatted number $\Sexpr{sn(0.00134,2)}$.
    
    \end{document}
    

    【讨论】:

    • 将函数更改为还包括 0 和否定案例。
    • @Sacha Epskamp 我决定取消我的“接受答案”,因为您没有明确展示如何与 \Sexpr{} 一起使用。是的,您的代码在 R 中确实可以正常工作,但我无法使其与 Sweave 中的 \Sexpr{} 一起工作。谢谢。
    • @Sacha Epskamp。谢谢你的耐心。现在我得到了 1.34times10-3(功率得到了很好的体现)。如何正确插入 $$ 以使数字 1.34 也在 $ 和 $ 之间并且 \times 显示为 x。谢谢。
    • 啊,我没有在 Sweave 中实际测试过它,显然我不得不使用四个反斜杠而不是两个:) 这应该可以。
    • @Sacha Epskamp 谢谢。这非常有帮助并且有效。如果您可以考虑添加我在帖子中提到的指定有效位数的可能性,例如在您的示例中,我只想输出 1.3 x 10 -3 而不是 1.34 x 10-3。设置 options(digits=1) 或类似设置将不起作用。
    【解决方案2】:

    使用 siunitx link to pdf 的示例。在序言中,您可以定义默认选项,稍后您可以在文档中覆盖这些选项。

    对于数字输出:

    num <- function(x,round_precision=NULL)
    {
      if (is.null(round_precision)) {
        return(sprintf("\\num{%s}", x))
      } else {
        return(sprintf("\\num[round-precision=%s]{%s}",round_precision, x))
      }
    }
    

    科学输出:

    sci<- function(x,round_precision=NULL){
      if (is.null(round_precision)) {
      return(sprintf("\\num[scientific-notation = true]{%s}", x))
    } else {
      return(sprintf("\\num[round-precision=%s,scientific-notation = true]{%s}",round_precision, x))
    }
    }
    

    这是一个完全可重现的 .Rnw 脚本(用于 knitr ... 对于 sweave,在函数中使用四个反斜杠而不是两个,请参见 SO post。)

    \documentclass[a4paper]{article}
    \usepackage{siunitx}
    %\usepackage{Sweave}
    \title{siunitx}
    
    \sisetup{
    round-mode = figures,
    round-precision = 3,
    group-separator = \text{~}
    }
    \begin{document}
    
    \maketitle
    <<sanitize_number,echo=FALSE>>=
    num <- function(x,round_precision=NULL)
    {
      if (is.null(round_precision)) {
        return(sprintf("\\num{%s}", x))
      } else {
        return(sprintf("\\num[round-precision=%s]{%s}",round_precision, x))
      }
    }
    
    sci<- function(x,round_precision=NULL){
      if (is.null(round_precision)) {
      return(sprintf("\\num[scientific-notation = true]{%s}", x))
    } else {
      return(sprintf("\\num[round-precision=%s,scientific-notation = true]{%s}",round_precision, x))
    }
    }
    
    @
    Examples :\\
    $num$ for number formatting :
    
    \begin{itemize}
    \item \textbf{num(pi, round\_precision=2)} $\Rightarrow$
    \num[round-precision=2]{3.14159265358979} 
    \item \textbf{num(pi, round\_precision=4)}  $\Rightarrow$
    \num[round-precision=4]{3.14159265358979}
    \item The default formatting (here round-precision=3) is taken from
    \textbf{\textbackslash sisetup} 
    \textbf{num(pi)}  $\Rightarrow$ \num{3.14159265358979}\\
    \end{itemize}
    
    \noindent $sci$ for scientific notation :
    
    \begin{itemize}
    \item \textbf{sci(12.5687e4)}  $\Rightarrow$ \num[scientific-notation =
    true]{125687}
    \item \textbf{sci(125687.11111)}  $\Rightarrow$
    \num[scientific-notation = true]{125687.11111}
    \item \textbf{sci(125687.11111, round\_precision=4)} $\Rightarrow$
     \Sexpr{sci(125687.11111, round_precision=4)}
    \end{itemize}
    
    \end{document}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 2019-04-15
      相关资源
      最近更新 更多