用于条件单元格格式化的xtable,不包含特殊字符

时间:2017-12-06 09:09:13

标签: r latex knitr xtable rnw

我将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

使用以下语法传递给时:

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{} 应用于包含某些内容的列。根据链接问题中讨论的答案,可以通过ifelsesanitize.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) ) } 时有效,理想情况下我想找到一个解决方案,我不必手动搜索每个单个字符。

1 个答案:

答案 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)
    }
)   

enter image description here