楼主: janyiyi
891 0

scientific notation for R/latex [推广有奖]

  • 3关注
  • 17粉丝

讲师

27%

还不是VIP/贵宾

-

威望
0
论坛币
3206 个
通用积分
5056.6800
学术水平
539 点
热心指数
537 点
信用等级
538 点
经验
10157 点
帖子
300
精华
2
在线时间
90 小时
注册时间
2010-10-3
最后登录
2024-4-6

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Motivation

Using R within a latex document can be a component of reproducible research,
offering (a) some assurance against typographical errors in transcribing
results to the latex file and (b) the ability for others to reproduce the
results.

For example, one might like to explain how close the computed
integral of the Witch of Agnesi function

12
woa <- function(x, a=1) 8 * a^3 / (x^2 + 4*a^2)integrate(woa, -Inf, Inf)
## 12.56637 with absolute error < 1.3e-09

is to the true value of $4pi$. One way to do that is to compute the mismatch

12
estimate <- integrate(woa, -Inf, Inf)$valuetheory <- 4 * pi

and to write something like

dots the misfit is Sexpr{abs(estimate-theory)}

in latex. However, the slew of digits is not especially useful, and the
computer-type exponential notation is not conventional in written work.

It would be good to have a function that writes such results in latex format.

Methods

A trial function is below.

1 2 3 4 5 6 7 8 9101112131415161718
scinot <- function(x, digits=2, showDollar=FALSE){    sign <- ""    if (x < 0) {        sign <- "-"        x <- -x    }    exponent <- floor(log10(x))    if (exponent) {        xx <- round(x / 10^exponent, digits=digits)        e <- paste("\\times 10^{", as.integer(exponent), "}", sep="")    } else {        xx <- round(x, digits=digits)        e <- ""    }    if (showDollar) paste("$", sign, xx, e, "$", sep="")    else paste(sign, xx, e, sep="")}

and a latex/sweave test use is

Limits may be infinite if set to texttt{Inf}, e.g. for the witch of Agnesifunction<<>>=woa <- function(x, a=1)    8 * a^3 / (x^2 + 4*a^2)integrate(woa, -Inf, Inf)@<<results=hide, echo=false>>=woa <- function(x, a=1)    8 * a^3 / (x^2 + 4*a^2)i <- integrate(woa, -Inf, Inf)$valueerr <- abs((i - 4 * pi) / (4 * pi))@the results differ from the true value $4pi$ by $Sexpr{scinot(err, 0)}$.

which yields as shown in the screenshot below. (Note that there is some
colourization and margin decoration that is not described by the code given
above.)

References and resources
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Scientific LaTeX ATION atex SCIE scientific

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
oliyiyi + 5 + 5 + 5 精彩帖子

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 10:51