使用 knitr 中的 xtable 清理 Bold 和 Identity 的列名称
Sanitize column names for Bold and Identity with xtable in knitr
有没有办法通过 xtable 的身份函数来清理列名,并通过另一个自定义函数来加粗列名?下面有两个代码块,一个用于设置虚拟函数,另一个用于打印 xtable。它在第一列名称中的 $ 符号上失败,并且 table 值中的 $ 符号已正确清理。
谢谢!
<<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))
@
我认为解决方案可能与在 two_functions
调用中使用 gsub
一样简单。
\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
函数,然后将结果字符串放置在 LaTeX \textbf{}
环境中。
结果table是:
有没有办法通过 xtable 的身份函数来清理列名,并通过另一个自定义函数来加粗列名?下面有两个代码块,一个用于设置虚拟函数,另一个用于打印 xtable。它在第一列名称中的 $ 符号上失败,并且 table 值中的 $ 符号已正确清理。
谢谢!
<<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))
@
我认为解决方案可能与在 two_functions
调用中使用 gsub
一样简单。
\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
函数,然后将结果字符串放置在 LaTeX \textbf{}
环境中。
结果table是: