使用 half-space 作为 knitr 输出的大标记
Using a half-space as a big mark for knitr output
我希望在 knitr 文档中打印像 50000
这样的数字,并使用半 space 作为千位分隔符。在 LaTeX 中,我只使用 50\,000
,我希望 knitr 通过 Sexpr
命令打印它。也就是说,我想要一个函数 reformat
在下面使用时
\documentclass{standalone}
\begin{document}
<<>>=
num <- 50000
@
The number is \Sexpr{reformat(num)}.
\end{document}
用一半打印 50 000 space。我面临的问题是使用 formatC
似乎不允许正确数量的反斜杠
formatC(num, format = "f", big.mark = "\,")
Error: '\,' is an unrecognized escape in character string starting ""\,"
formatC(num, format = "f", big.mark = "\,")
[1] "50,000.0000"
formatC(num, format = "f", big.mark = "\\,")
Error: '\,' is an unrecognized escape in character string starting ""\\,"
代替 reformat
的正确函数是什么?
Roland 和 baptiste 的解决方案都很好用:
reformat <- function(num) formatC(num, format = "f", big.mark = "\\,")
reformat <- function(num) formatC(num, format = "f", big.mark = "\u2009")
我希望在 knitr 文档中打印像 50000
这样的数字,并使用半 space 作为千位分隔符。在 LaTeX 中,我只使用 50\,000
,我希望 knitr 通过 Sexpr
命令打印它。也就是说,我想要一个函数 reformat
在下面使用时
\documentclass{standalone}
\begin{document}
<<>>=
num <- 50000
@
The number is \Sexpr{reformat(num)}.
\end{document}
用一半打印 50 000 space。我面临的问题是使用 formatC
似乎不允许正确数量的反斜杠
formatC(num, format = "f", big.mark = "\,")
Error: '\,' is an unrecognized escape in character string starting ""\,"
formatC(num, format = "f", big.mark = "\,")
[1] "50,000.0000"
formatC(num, format = "f", big.mark = "\\,")
Error: '\,' is an unrecognized escape in character string starting ""\\,"
代替 reformat
的正确函数是什么?
Roland 和 baptiste 的解决方案都很好用:
reformat <- function(num) formatC(num, format = "f", big.mark = "\\,")
reformat <- function(num) formatC(num, format = "f", big.mark = "\u2009")