使用半空格作为针织输出的大标记

时间:2015-05-31 08:10:46

标签: r escaping knitr

我希望像50000这样的数字打印在一个带有半空间作为千位分隔符的knitr文档中。在LaTeX中,我只使用50\,000,我希望knitr通过Sexpr命令打印它。也就是说,我希望函数reformat在以下

中使用
\documentclass{standalone}

\begin{document}
<<>>=
num <- 50000
@

The number is \Sexpr{reformat(num)}.

\end{document}

半空间打印50 000。我遇到的问题是使用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的正确功能是什么?

1 个答案:

答案 0 :(得分:1)

Roland和baptiste的解决方案都运行良好:

reformat <- function(num) formatC(num, format = "f", big.mark = "\\\\,")
reformat <- function(num) formatC(num, format = "f", big.mark = "\u2009")