2011-12-03 30 views
15

Tôi chỉ mới bắt đầu viết một số tài liệu với Sweave/R và tôi thích lệnh \sexpr{} cho phép người ta viết số trực tiếp trong văn bản.R/Sweave định dạng số với Sexpr {} trong ký hiệu khoa học

Nếu tôi có số như mus=0.0002433121, tôi có thể nói nó với số chữ số thập phân, ví dụ:

\Sexpr{round(mus,7)} 

Làm thế nào để viết nó trong ký hiệu khoa học ví dụ như LaTeX sẽ được xuất ra

2.43 \times 10^{-4} 

và chúng tôi có thể kiểm soát số lượng đáng kể chữ số được outputted như 3 trong ví dụ này?

Tôi lưu ý rằng một số lượng như sigma = 2000000 được viết tự động để 2e + 06 nếu tôi chỉ định

\Sexpr{round(sigma,2)}. 

Tôi muốn rằng nó sẽ được viết như

2 \times 10^6 

giống như chúng ta sẽ nhận được trong LaTeX ký hiệu và có lẽ cho chúng ta khả năng kiểm soát số chữ số có nghĩa.

Làm cách nào để đạt được điều này?

Trả lời

14

Tôi nghĩ rằng chức năng này nên làm việc:

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="")) 
} 

Một số xét nghiệm:

> 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" 

Nếu bạn muốn kết quả trong chế độ toán học bạn có thể nhập $ dấu hiệu trong paste() gọi.

Edit:

Dưới đây là một ví dụ 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} 
+1

Chết tiệt, đánh tôi với câu trả lời giống hệt mà tôi đã sẵn sàng. :) –

+0

Đã thay đổi chức năng cũng bao gồm 0 và các trường hợp âm. –

+0

@ Sacha.Thú vị và hữu ích. Cảm ơn rất nhiều và 1 phiếu bầu. – yCalleecharan

0

Một ví dụ sử dụng siunitxlink to pdf. Trong phần mở đầu, bạn có thể xác định các tùy chọn mặc định mà bạn có thể ghi đè sau trong tài liệu.

Đối với đầu ra số:

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)) 
    } 
} 

Đối với đầu ra khoa học:

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

Đây là một tái sản xuất đầy đủ kịch bản .Rnw (được sử dụng với knitr ... cho sweave sử dụng bốn antislashes trong các chức năng thay vì hai xem này 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} 
Các vấn đề liên quan