我将xtable for conditional cell formatting significant p-values of table的解决方案应用于我的数据集;但是,根据建议的答案,我无法使用 sanitize.text.function = function(x) x
。
数据集包含使用 _
字符的文本列,如下例所示:
dta <- data.frame(
varA = c("this_and_that", "something_else", "def"),
varB = c("other_thing", "something_different", "abc")
)
xtable
使用以下语法传递给xtable时:
print.xtable(x = xtable(dta),
booktabs = TRUE,
comment = FALSE,
floating = TRUE,
type = "latex",
include.rownames = FALSE,
NA.string = "NA",
# Font and linespace size
size = "\\fontsize{5pt}{5pt}\\selectfont")
代码生成可用的latex片段:
\begin{table}[ht]
\centering
\begingroup\fontsize{5pt}{5pt}\selectfont
\begin{tabular}{ll}
\toprule
varA & varB \\
\midrule
this\_and\_that & other\_thing \\
something\_else & something\_different \\
def & abc \\
\bottomrule
\end{tabular}
\endgroup
\end{table}
我想将 \textbf{}
应用于包含某些内容的列。根据链接问题中讨论的答案,可以通过ifelse
和sanitize.text.function = function(x) x
完成,如下例所示:
print.xtable(
x = xtable(dta %>%
mutate_all(funs(
ifelse(grepl("something", .), paste0("\\textbf{", ., "}"), .)
))),
booktabs = TRUE,
comment = FALSE,
floating = TRUE,
type = "latex",
include.rownames = FALSE,
NA.string = "NA",
# Font and linespace size
size = "\\fontsize{5pt}{5pt}\\selectfont",
sanitize.text.function = function(x) {
x
}
)
这会针对 \\textbf{}
生成正确的语法,但 _
不会因:sanitize.text.function = function(x) {x}
而转义。
\begin{table}[ht]
\centering
\begingroup\fontsize{5pt}{5pt}\selectfont
\begin{tabular}{ll}
\toprule
varA & varB \\
\midrule
this_and_that & other_thing \\
\textbf{something_else} & \textbf{something_different} \\
def & abc \\
\bottomrule
\end{tabular}
\endgroup
\end{table}
sanitize.text.function
代码:
print.xtable(
x = xtable(dta %>%
mutate_all(funs(
ifelse(grepl("something", .), paste0("\\textbf{", ., "}"), .)
))),
booktabs = TRUE,
comment = FALSE,
floating = TRUE,
type = "latex",
include.rownames = FALSE,
NA.string = "NA",
# Font and linespace size
size = "\\fontsize{5pt}{5pt}\\selectfont"
)
会产生可怕的混乱,因为默认的清理行为会尝试逃避\\textbf
\begin{table}[ht]
\centering
\begingroup\fontsize{5pt}{5pt}\selectfont
\begin{tabular}{ll}
\toprule
varA & varB \\
\midrule
this\_and\_that & other\_thing \\
$\backslash$textbf\{something\_else\} & $\backslash$textbf\{something\_different\} \\
def & abc \\
\bottomrule
\end{tabular}
\endgroup
\end{table}
可能更好的方法是使用消毒功能:
print.xtable(
x = xtable(dta),
booktabs = TRUE,
comment = FALSE,
floating = TRUE,
type = "latex",
include.rownames = FALSE,
NA.string = "NA",
# Font and linespace size
size = "\\fontsize{5pt}{5pt}\\selectfont",
sanitize.text.function = function(x) {
ifelse(grepl("something", x), paste0("\\textbf{", x, "}"), x)
}
)
有没有其他方法来解决这个问题,而不是通过greping我表中的所有特殊字符并通过自定义清理函数手动转义它们来处理 两个 元素{{1 }和textbf{}
正确吗?
使用:
_
会在手动转义 sanitize.text.function = function(x) {
gsub(fixed = TRUE, pattern = "_", replacement = "\\_",
x = ifelse(grepl(pattern = "something", x = x), paste0("\\textbf{", x, "}"), x)
)
}
时有效,理想情况下我想找到一个解决方案,我不必手动搜索每个单个字符。
答案 0 :(得分:1)
Hmisc
中有一项功能可以完成工作:latexTranslate
。首先运行它,然后应用LATEX格式命令。
library("Hmisc")
library("xtable")
dta <- data.frame(
varA = c("this_and_that", "something_else", "def"),
varB = c("other_thing", "something_different", "abc")
)
print.xtable(
x = xtable(dta),
file=paste0(getwd(),"/table/sanitize_test.tex"),
booktabs = TRUE,
comment = FALSE,
floating = TRUE,
type = "latex",
include.rownames = FALSE,
NA.string = "NA",
# Font and linespace size
size = "\\fontsize{5pt}{5pt}\\selectfont",
sanitize.text.function = function(x) {
x <- latexTranslate(x)
x <- ifelse(grepl("something", x), paste0("\\textbf{", x, "}"), x)
return(x)
}
)