2011-12-03 11 views
15

numaralarına yazdım. Sweave/R ile bazı belgeleri yazmaya başladım ve bir kopyanın doğrudan metin içinde numara yazmasını sağlayan \sexpr{} komutunu beğendim.R/Döngü biçimlendirme numaralarını Sexpr {} ile bilimsel gösterimde

mus=0.0002433121 gibi bir numaram varsa, onda bir sayıdaki ondalık basamağa, örn.

2.43 \times 10^{-4} 

çıkışı olacaktır

\Sexpr{round(mus,7)} 

Nasıl yani LaTeX olarak bilimsel gösterimde yazmaya ve anlamlı basamak sayısı bu örnekte 3 gibi çıkışı yapılacak kontrol edebilir?

ben belirtirseniz sigma = 2000000 gibi bir sayı 2e + 06 otomatik yazılır unutmayın

\Sexpr{round(sigma,2)}. 

Biz LaTeX gösterimde alacağı olduğunca

2 \times 10^6 

aynı yazılı olacağını tercih ediyorum ve belki de bize anlamlı basamakların sayısını kontrol etme imkanı veriyor.

Bu nasıl başarılır?

cevap

14

Bu fonksiyon çalışması gerekir düşünüyorum:

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

Bazı testler:

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

Eğer paste() çağrısında $ işaretleri girebilir matematik modunda sonucu istiyorum.

Düzenleme: İşte

bir Sweave örnektir:

\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

Dammit, hazır olduğum aynı cevapla beni döverim. :) –

+0

Fonksiyonu, 0 ve negatif durumlar da içerecek şekilde değiştirildi. –

+0

@ Sacha.İlginç ve kullanışlı. Çok teşekkürler ve 1 oy verin. – yCalleecharan

0

siunitxlink to pdfkullanan bir örnek. Giriş bölümünde, daha sonra belgede geçersiz kılınabilecek varsayılan seçeneklerinizi tanımlayabilirsiniz.

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

bilimsel çıkışı için: sayısal çıkış için

İşte

siunitx example

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)) 
} 
} 
tam tekrarlanabilir .Rnw komut olduğu için (... ile knitr kullanılacak sweave, iki tane yerine dört adet eğik çizgi kullanın, bunun yerine bu SO post'a bakın.)

\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} 
İlgili konular