在乳胶表中显示\ checkmark

时间:2018-11-10 20:17:03

标签: r latex knitr xtable

在一个旧的.Rnw文件中,我正在使用RODBC包读取Excel工作表并显示包含复选标记的乳胶表。

最近,我尝试更改用于读取excel文件的方式,并使用XLConnect包而不是RODBC包,但是遇到了一个问题。我无法使对勾符号正确显示。请注意,我传递给xtable进行打印的对象与上一个对象相同,但是结果不一样!

在这里使用我使用的the excel file

这是有效的.Rnw。 (我知道代码绝不是最佳)。文件编码为UTF-8,我使用knitrXeLaTeX

编织文件
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{bbding} % checkmark symbol
\setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
\setsansfont{Times New Roman}

<<eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE>>=
library(plyr)
library(XLConnect)
library(lubridate)
library(RODBC)
library(stringr)
library(xtable)
library(magrittr)
@

\begin{document}

\begin{center}

{\small
<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

sheet = 'DATA-BOOK.xls' 
con = odbcConnectExcel2007(sheet)
tbls = sqlTables(con)
df <- sqlQuery(con,"SELECT * FROM `r-crosstab$`",stringsAsFactors =FALSE)
close(con)


dfr <- df[,2:ncol(df)]
for (i in 1:ncol(dfr)){
  dfr[i][is.na(dfr[i])] <- ""
}

temp <- adply(dfr,1,function(x) length(x[x!=""]))

dfr <-  apply(dfr,2,function(x) gsub("\\?","\\\\Checkmark",x))

aitia <- paste0("c",1:nrow(dfr))
aitia <- unlist(sapply(aitia,function(x) if (nchar(x) == 2) {x=paste0(str_sub(x, 1, 1),"0",str_sub(x, 2, 2))} else{x} ))
names(aitia)<-NULL
dfr <- cbind(aitia,dfr,temp[ncol(temp)])

dfr1 <- t(dfr)
dfr1 <- apply(dfr1,2,as.character)
rownames(dfr1) <- c(colnames(dfr)[1:(length(colnames(dfr))-1)],"Αθρ.")
colnames(dfr1) <- dfr1[1,]
dfr1 <- dfr1[-1,]

dfr1[nrow(dfr1),] <- paste0("\\textbf{",dfr1[nrow(dfr1),],"}")

print(xtable(dfr1, align=paste0("l|",paste(rep("c|",length(aitia)),collapse="")),
             label='tab_crosstab'), 
      scalebox=0.80, 
      include.rownames=TRUE, 
      hline.after=c(-1:nrow(dfr1)),
      sanitize.text.function = function(x){x}
)

@
}
\end{center}

\end{document}

那是我编译时得到的(这是预期的结果)

enter image description here

这是我面临问题的.Rnw。你能告诉我我需要改变什么吗?

\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{bbding} % checkmark symbol
\setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
\setsansfont{Times New Roman}

<<eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE>>=
library(plyr)
library(XLConnect)
library(lubridate)
library(RODBC)
library(stringr)
library(xtable)
library(magrittr)
@

\begin{document}

\begin{center}

{\small
<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

wb <- loadWorkbook('DATA-BOOK.xls', create = FALSE)
df <- readWorksheet(wb, sheet="r-crosstab", check.names=FALSE)

df %>%
dplyr::select(- dplyr::contains("ΑITIO")) %>%
names() -> vnames 

df[vnames] %<>%
dplyr::mutate_if(is.factor, as.character)


dfr <- df[,2:ncol(df)]
for (i in 1:ncol(dfr)){
  dfr[i][is.na(dfr[i])] <- ""
}

temp <- adply(dfr,1,function(x) length(x[x!=""]))

dfr <-  apply(dfr,2,function(x) gsub("√","\\\\Checkmark",x)) # √   <U+221A>

aitia <- paste0("c",1:nrow(dfr))
aitia <- unlist(sapply(aitia,function(x) if (nchar(x) == 2) {x=paste0(str_sub(x, 1, 1),"0",str_sub(x, 2, 2))} else{x} ))
names(aitia)<-NULL
dfr <- cbind(aitia,dfr,temp[ncol(temp)])

dfr1 <- t(dfr)
dfr1 <- apply(dfr1,2,as.character)
rownames(dfr1) <- c(colnames(dfr)[1:(length(colnames(dfr))-1)],"Αθρ.")
colnames(dfr1) <- dfr1[1,]
dfr1 <- dfr1[-1,]

dfr1[nrow(dfr1),] <- paste0("\\textbf{",dfr1[nrow(dfr1),],"}")

print(xtable(dfr1, align=paste0("l|",paste(rep("c|",length(aitia)),collapse="")),
             label='tab_crosstab'), 
      scalebox=0.80, 
      include.rownames=TRUE, 
      hline.after=c(-1:nrow(dfr1)),
      sanitize.text.function = function(x){x}
)
@
}
\end{center}

\end{document}

这是现在编译的pdf的样子:enter image description here

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Greek_Greece.1253 
[2] LC_CTYPE=Greek_Greece.1253   
[3] LC_MONETARY=Greek_Greece.1253
[4] LC_NUMERIC=C                 
[5] LC_TIME=Greek_Greece.1253    

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
 [1] magrittr_1.5         extrafont_0.17      
 [3] xtable_1.8-3         stringr_1.3.1       
 [5] RODBC_1.3-15         lubridate_1.7.4     
 [7] XLConnect_0.2-15     XLConnectJars_0.2-15
 [9] plyr_1.8.4           tidyr_0.8.2         
[11] dplyr_0.7.7         

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19     Rttf2pt1_1.3.7   rstudioapi_0.8  
 [4] bindr_0.1.1      tidyselect_0.2.5 R6_2.3.0        
 [7] rlang_0.3.0.1    tools_3.5.1      extrafontdb_1.0 
[10] yaml_2.2.0       assertthat_0.2.0

编辑

在下面的代码片段对中,我尝试最小化示例,而忽略了一些无关的部分。再次,第一个是行得通的。同时,我注意到无论在gsub中使用什么字符串作为替换(在第二个示例中),结果始终相同!

工作代码:

\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{bbding} % checkmark symbol
\setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
\setsansfont{Times New Roman}

<<eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE>>=
library(XLConnect)
library(RODBC)
library(xtable)
library(dplyr)
library(magrittr)
@

\begin{document}

{\small
<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

sheet = 'DATA-BOOK.xls' 
con = odbcConnectExcel2007(sheet)
tbls = sqlTables(con)
df <- sqlQuery(con,"SELECT * FROM `r-crosstab$`",stringsAsFactors =FALSE)
close(con)

df %>%
dplyr::select(- dplyr::contains("ΑITIO")) %>%
names() -> vnames 

df[vnames] %<>%
mutate_if(is.factor, as.character) %>% 
mutate_if(is.character, funs(coalesce(., "")))
names(df)[1] <- "TITLE"

dfr <-  apply(df,2,function(x) gsub("\\?","\\\\Checkmark",x))
rownames(dfr) <- NULL
dfr1 <- t(dfr)
dfr1[1,] <- paste0("c",1:19)

print(xtable(dfr1), 
      scalebox=0.80, 
      hline.after=c(-1:nrow(dfr1)),
      sanitize.text.function = function(x){x}
)
@
}

\end{document}

没有工作代码:

\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{bbding} % checkmark symbol
\setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
\setsansfont{Times New Roman}

<<eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE>>=
library(XLConnect)
library(RODBC)
library(xtable)
library(dplyr)
library(magrittr)
@

\begin{document}

{\small
<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

wb <- loadWorkbook('DATA-BOOK.xls', create = FALSE)
df <- readWorksheet(wb, sheet="r-crosstab", check.names=FALSE)

df %>%
dplyr::select(- dplyr::contains("ΑITIO")) %>%
names() -> vnames 

df[vnames] %<>%
mutate_if(is.factor, as.character) %>% 
mutate_if(is.character, funs(coalesce(., "")))
names(df)[1] <- "TITLE"

dfr <-  apply(df,2,function(x) gsub("√","\\\\Checkmark",x)) # √   <U+221A>
dfr1 <- t(dfr)
dfr1[1,] <- paste0("c",1:19)

print(xtable(dfr1), 
      scalebox=0.80, 
      include.rownames=TRUE, 
      hline.after=c(-1:nrow(dfr1)),
      sanitize.text.function = function(x){x}
)
@
}

\end{document}

1 个答案:

答案 0 :(得分:2)

问题是您的gsub行的格式。

建议的解决方案:

替换:

dfr <- apply(df,2,function(x) gsub("√","\\\\Checkmark",x)) # √ <U+221A>

使用

dfr <- apply(dfr,2,function(x) gsub("\u221A","\\\\Checkmark",x)) # √ <U+221A>

结果:

https://www.dropbox.com/s/evzknzobfwh0ag1/53243041v2.pdf?dl=0

设置:

我在Windows 10以及RStudio和R3.5.1上使用knitr和XeLatex进行排版测试。

说明:

.rnw文件在编译时会创建一个中间.tex文件作为输出,然后将其转换为pdf。您必须将正确格式的代码\u221A"传递给pdf格式应用程序。