代码块中有一个很大的数字:
a <- 1.234 * 10^36然后,我在我的文档中内联地打印:
What does this look like when knitted: `r a`在一个标准的.Rmd中编织这个过程会产生预期的1.234 x 10^36。
但是,在papaja模板中编织可以使用1.234e+36生成“计算格式”。
是否有自动格式化的方法,而不必求助于像solution given here这样的自定义函数?
发布于 2022-06-03 19:43:08
您可以使用函数papaja::apa_num()
> papaja::apa_num(1.234 * 10^36, format = "e")
[1] "$1.23 \\times 10^{36}$"发布于 2022-03-09 13:22:33
可以使用knitr强制格式化。
knitr::knit_hooks$set(inline = function(x) {
knitr:::format_sci(x, 'md')
})这将将外观格式化为如下所示(使用了上面描述的代码)。

我不喜欢x空间的不均匀分布,所以我发现捕获了函数(和支持函数),并能够获得:

对于前者,我用的是你所说的。不过,这并不是第二个选项所使用的。
`r a`您不需要调用hooks$set,但是您需要调用修改后的函数并用$封装调用。
$`r format_sci(a)`$下面是经过稍微修改的knitr函数,产生了第二个选项:
```{r doAsISay}TeX、HTML和reST的科学表示法
format_sci_one =函数(
x,format =‘胶乳’,times =getOption(‘针织品.inline.time’,‘\倍')
){
如果(!(class(x)1 ==‘数值’)\x is.na(x) \x == 0)返回(as.character(X))
if (is.infinite(x)) {
return( switch(format, latex = { sprintf("%s\\infty{}", ifelse(x < 0, "-", "")) }, html = { sprintf("%s∞", ifelse(x < 0, "-", "")) }, as.character(x)))}
if (abs(lx ) <- floor(log10(abs(X)< getOption('scipen') +4L
return(round_digits(x)) # no need sci notationB= round_digits(x / 10^lx)
bb %,% c(1,-1) = '‘
开关(格式,latex ={
sci_notation('%s%s10^{%s}', b, times, lx)},
html =sci_notation(‘’%s%S10%s ',b,‘×’,lx),
md = sci_notation('%s%s10^%s^',b,'×',lx),
rst ={
# if AsIs, use the :math: directiveif (inherits(x, 'AsIs')) { s = sci_notation('%s%s10^{%s}', b, times, lx) sprintf(':math:`%s`', s)} else { # This needs the following line at the top of the file to define |times| # .. include <isonum.txt> sci_notation('%s%s10 :sup:`%s`', b, ' |times| ', lx)}},as.character(x))
}
format_sci_one()的矢量化版本
format_sci =函数(x,.){
如果(继承(x,'roman'))返回(as.character(X))
vapply(x,format_sci_one,字符(1L),.,USE.NAMES = FALSE)
}
sci_notation =函数(格式、基、时间、幂){
sprintf(格式,基,如果是(基== '','',时间),功率)
}
round_digits =函数(X){
if (getOption(‘针织品.Digs.signif’,FALSE))格式(X)format{
as.character(round(x, getOption('digits')))}
}
或者,您可以在您下载的编织品包中更改此选项。(我以前对下载的软件包做过修改,而不是knitr。)
FYI,这是测试和图像是从针织RMD使用输出集为output: papaja::apa6_pdf。
https://stackoverflow.com/questions/71407795
复制相似问题