title: Displaying \checkmark in latex table
body: <p>In an old <code>.Rnw</code> file I was using the <code>RODBC</code> package to read an excel sheet and display a latex table containing checkmarks.</p>
  
  <p>Recently, I tried to change the way I used to read the excel file, and use the <code>XLConnect</code> package instead of <code>RODBC</code>, but I came across a problem. I can't make the checkmark symbol be displayed correctly anymore. Note that the object I pass to xtable for printing is identical with the previous one, but the result is not!</p>
  
  <p>Get here <a href="https://drive.google.com/open?id=1iiOMeBysVCo-rwjMdMlqa2R4CAP6x19p" rel="nofollow noreferrer">the excel file</a> that I use. </p>
  
  <p>This is the working <code>.Rnw</code>. (I know the code isn't by no means optimal). The file encoding is UTF-8, and I weave the file using <code>knitr</code> and <code>XeLaTeX</code> </p>
  
  <pre><code>\documentclass[a4paper]{article}
  \usepackage{fontspec}
  \usepackage{bbding} % checkmark symbol
  \setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
  \setsansfont{Times New Roman}
  
  &lt;&lt;eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE&gt;&gt;=
  library(plyr)
  library(XLConnect)
  library(lubridate)
  library(RODBC)
  library(stringr)
  library(xtable)
  library(magrittr)
  @
  
  \begin{document}
  
  \begin{center}
  
  {\small
  &lt;&lt;eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE&gt;&gt;=
  
  sheet = 'DATA-BOOK.xls' 
  con = odbcConnectExcel2007(sheet)
  tbls = sqlTables(con)
  df &lt;- sqlQuery(con,"SELECT * FROM `r-crosstab$`",stringsAsFactors =FALSE)
  close(con)
  
  
  dfr &lt;- df[,2:ncol(df)]
  for (i in 1:ncol(dfr)){
    dfr[i][is.na(dfr[i])] &lt;- ""
  }
  
  temp &lt;- adply(dfr,1,function(x) length(x[x!=""]))
  
  dfr &lt;-  apply(dfr,2,function(x) gsub("\\?","\\\\Checkmark",x))
  
  aitia &lt;- paste0("c",1:nrow(dfr))
  aitia &lt;- 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)&lt;-NULL
  dfr &lt;- cbind(aitia,dfr,temp[ncol(temp)])
  
  dfr1 &lt;- t(dfr)
  dfr1 &lt;- apply(dfr1,2,as.character)
  rownames(dfr1) &lt;- c(colnames(dfr)[1:(length(colnames(dfr))-1)],"Αθρ.")
  colnames(dfr1) &lt;- dfr1[1,]
  dfr1 &lt;- dfr1[-1,]
  
  dfr1[nrow(dfr1),] &lt;- 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}
  </code></pre>
  
  <p>That's what I get when I compile it (it is the expected result)</p>
  
  <p><a href="https://i.stack.imgur.com/7DDSj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7DDSj.png" alt="enter image description here"></a></p>
  
  <p>That is the .Rnw where I face the problem. Can you tell me what I need to change?</p>
  
  <pre><code>\documentclass[a4paper]{article}
  \usepackage{fontspec}
  \usepackage{bbding} % checkmark symbol
  \setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
  \setsansfont{Times New Roman}
  
  &lt;&lt;eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE&gt;&gt;=
  library(plyr)
  library(XLConnect)
  library(lubridate)
  library(RODBC)
  library(stringr)
  library(xtable)
  library(magrittr)
  @
  
  \begin{document}
  
  \begin{center}
  
  {\small
  &lt;&lt;eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE&gt;&gt;=
  
  wb &lt;- loadWorkbook('DATA-BOOK.xls', create = FALSE)
  df &lt;- readWorksheet(wb, sheet="r-crosstab", check.names=FALSE)
  
  df %&gt;%
  dplyr::select(- dplyr::contains("ΑITIO")) %&gt;%
  names() -&gt; vnames 
  
  df[vnames] %&lt;&gt;%
  dplyr::mutate_if(is.factor, as.character)
  
  
  dfr &lt;- df[,2:ncol(df)]
  for (i in 1:ncol(dfr)){
    dfr[i][is.na(dfr[i])] &lt;- ""
  }
  
  temp &lt;- adply(dfr,1,function(x) length(x[x!=""]))
  
  dfr &lt;-  apply(dfr,2,function(x) gsub("√","\\\\Checkmark",x)) # √   &lt;U+221A&gt;
  
  aitia &lt;- paste0("c",1:nrow(dfr))
  aitia &lt;- 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)&lt;-NULL
  dfr &lt;- cbind(aitia,dfr,temp[ncol(temp)])
  
  dfr1 &lt;- t(dfr)
  dfr1 &lt;- apply(dfr1,2,as.character)
  rownames(dfr1) &lt;- c(colnames(dfr)[1:(length(colnames(dfr))-1)],"Αθρ.")
  colnames(dfr1) &lt;- dfr1[1,]
  dfr1 &lt;- dfr1[-1,]
  
  dfr1[nrow(dfr1),] &lt;- 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}
  </code></pre>
  
  <p>Here is how the compliled pdf looks like now: <a href="https://i.stack.imgur.com/FMDso.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FMDso.png" alt="enter image description here"></a></p>
  
  <pre><code>&gt; sessionInfo()
  R version 3.5.1 (2018-07-02)
  Platform: i386-w64-mingw32/i386 (32-bit)
  Running under: Windows &gt;= 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
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>In the following pair of code snipets I tried to minimize the examples, leaving out some irrelevant parts. Again, the first is the one that works ok. In the meantime I noticed that no matter what string I use as a replacement in <code>gsub</code> (in the second example), the result is always the same!!!</p>
  
  <p>Working code:</p>
  
  <pre><code>\documentclass[a4paper]{article}
  \usepackage{fontspec}
  \usepackage{bbding} % checkmark symbol
  \setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
  \setsansfont{Times New Roman}
  
  &lt;&lt;eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE&gt;&gt;=
  library(XLConnect)
  library(RODBC)
  library(xtable)
  library(dplyr)
  library(magrittr)
  @
  
  \begin{document}
  
  {\small
  &lt;&lt;eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE&gt;&gt;=
  
  sheet = 'DATA-BOOK.xls' 
  con = odbcConnectExcel2007(sheet)
  tbls = sqlTables(con)
  df &lt;- sqlQuery(con,"SELECT * FROM `r-crosstab$`",stringsAsFactors =FALSE)
  close(con)
  
  df %&gt;%
  dplyr::select(- dplyr::contains("ΑITIO")) %&gt;%
  names() -&gt; vnames 
  
  df[vnames] %&lt;&gt;%
  mutate_if(is.factor, as.character) %&gt;% 
  mutate_if(is.character, funs(coalesce(., "")))
  names(df)[1] &lt;- "TITLE"
  
  dfr &lt;-  apply(df,2,function(x) gsub("\\?","\\\\Checkmark",x))
  rownames(dfr) &lt;- NULL
  dfr1 &lt;- t(dfr)
  dfr1[1,] &lt;- paste0("c",1:19)
  
  print(xtable(dfr1), 
        scalebox=0.80, 
        hline.after=c(-1:nrow(dfr1)),
        sanitize.text.function = function(x){x}
  )
  @
  }
  
  \end{document}
  </code></pre>
  
  <p>NOT working code:</p>
  
  <pre><code>\documentclass[a4paper]{article}
  \usepackage{fontspec}
  \usepackage{bbding} % checkmark symbol
  \setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
  \setsansfont{Times New Roman}
  
  &lt;&lt;eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE&gt;&gt;=
  library(XLConnect)
  library(RODBC)
  library(xtable)
  library(dplyr)
  library(magrittr)
  @
  
  \begin{document}
  
  {\small
  &lt;&lt;eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE&gt;&gt;=
  
  wb &lt;- loadWorkbook('DATA-BOOK.xls', create = FALSE)
  df &lt;- readWorksheet(wb, sheet="r-crosstab", check.names=FALSE)
  
  df %&gt;%
  dplyr::select(- dplyr::contains("ΑITIO")) %&gt;%
  names() -&gt; vnames 
  
  df[vnames] %&lt;&gt;%
  mutate_if(is.factor, as.character) %&gt;% 
  mutate_if(is.character, funs(coalesce(., "")))
  names(df)[1] &lt;- "TITLE"
  
  dfr &lt;-  apply(df,2,function(x) gsub("√","\\\\Checkmark",x)) # √   &lt;U+221A&gt;
  dfr1 &lt;- t(dfr)
  dfr1[1,] &lt;- 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}
  </code></pre>