R / Sweave中使用\Sexpr{}以科学计数法格式化数字

16

我刚开始使用编写一些文档,我喜欢<\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 表示法中所得到的一样,并且也许能够提供控制有效数字位数的可能性。

如何实现这个功能?

2个回答

15

我认为这个函数应该可行:

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
@Sacha Epskamp。感谢您的耐心等待。现在我得到了1.34times10-3(幂表示得很好)。如何正确插入$ $,使数字1.34也位于$和$之间,并且\times显示为x。谢谢。 - yCalleecharan
啊,我实际上还没有在Sweave中测试过它,显然我必须使用四个反斜杠而不是两个 :) 这应该可以工作。 - Sacha Epskamp
@Sacha Epskamp 謝謝。這非常有幫助,它起作用了。如果您可以考慮添加一個選項來指定有效位數,就像我在帖子中提到的那樣,例如,在您的示例中,我只想輸出1.3 x 10-3而不是1.34 x 10 -3。設置options(digits=1)或類似選項不起作用。 - yCalleecharan
@Sacha Epskam,"swst"是您正在开发的软件包吗? - yCalleecharan
显示剩余4条评论

2

一个使用siunitx的例子,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))
}
}

siunitx example

这里是一个完整可复制的.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}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接