有没有办法传递xtable的标识函数来清理列名和另一个自定义函数来加粗列名?下面有两个代码块,一个用于设置虚拟功能,另一个用于打印xtable。它在第一列名称中的$符号上失败,并且表值中的$符号已正确清理。
谢谢!
<<setup>>=
library(knitr)
library(xtable)
two_functions = function(x){
paste("\\textbf{", x, "}", sep = "")
# use xtable's 'identity' function to convert special characters
}
options(xtable.sanitize.colnames.function = two_functions)
@
<<xtable, results='asis'>>=
xtab = data.frame(a = c("Horse and $buddy", "Paddy Wagon", "Hospital Care", "Peanut butter and toast", "Cheese Whiz with Mayo"),
b = c(10000000, 200000.4533, 3098765435.65456, 408765467.654456, 50.00000))
colnames(xtab) = c("Hello money $ bag$", "Numbers")
print(xtable(xtab))
@
答案 0 :(得分:0)
我认为解决方案可能就像在gsub
调用中使用two_functions
一样简单。
\documentclass{article}
\begin{document}
<<<setup>>=
library(knitr)
library(xtable)
two_functions = function(x){
gsub("\\$", "\\\\$", paste("\\textbf{", x, "}", sep = ""))
}
options(xtable.sanitize.colnames.function = two_functions,
xtable.sanitize.rownames.function = NULL,
xtable.sanitize.text.function = NULL)
@
<<xtable, results='asis'>>=
xtab = data.frame(a = c("Horse and $buddy", "Paddy Wagon", "Hospital Care", "Peanut butter and toast", "Cheese Whiz with Mayo"),
b = c(10000000, 200000.4533, 3098765435.65456, 408765467.654456, 50.00000))
colnames(xtab) = c("Hello money $ bag$", "Numbers")
print(xtable(xtab))
@
\end{document}
要使用默认的xtable
函数来清理字符串,请使用以下内容替换上面的two_functions
函数:
two_functions = function(x){
paste0("\\textbf{", xtable::sanitize(x, type = "latex"), "}")
}
此处首先调用xtable::sanitize
函数,然后将生成的stings放在LaTeX \textbf{}
环境中。
结果表是: