How to fix problem with special characters in Knitr::Kable and kableExtra for a PDF report

Asked

Viewed 166 times

2

I’m trying to customize a chart using R Sweave. The problem is that the R output messes up the symbol , in some cases did not exit the bar in the file . tex which causes error, but already fixed using gsub(). The problem now is that in place of the bar is leaving textbackslash{} and in place of { is leaving {.

A short example containing the error:

This file code . Rnw

\begin{document}
\SweaveOpts{concordance=TRUE}

\chapter{Something}

<<echo=FALSE, cache=TRUE>>=
library(knitr)
library(kableExtra)

#CONFIGURANDO UMA CELULA COM LOGICA

a<-c("Name1","Name2","Name3","Name4","Name5")
b<-c(70,45,80,55,20)
c<-data.frame(a,b)
colnames(c)<-c("Name", "Grade")
library(dplyr)
d<-c%>%
  mutate(
    Grade=cell_spec(Grade,"latex",color=ifelse(Grade>=60,"blue","red")))


d<-kable(d, "latex", booktabs=T, row.names = FALSE, align="c")
@

\Sexpr{gsub("\\\\", "\\\\\\\\", d)}

\end{document}

In file . tex the result is as follows

\documentclass{book}  
\usepackage[top=3cm,bottom=2cm,left=2cm,right=2cm]{geometry} 
\usepackage[brazil]{babel} 
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{xcolor}

\usepackage{Sweave}
\begin{document}
\input{teste-concordance}

\chapter{Something}



\begin{tabular}{cc}
\toprule
Name & Grade\\
\midrule
Name1 & \textbackslash{}textcolor\{blue\}\{70\}\\
Name2 & \textbackslash{}textcolor\{red\}\{45\}\\
Name3 & \textbackslash{}textcolor\{blue\}\{80\}\\
Name4 & \textbackslash{}textcolor\{red\}\{55\}\\
Name5 & \textbackslash{}textcolor\{red\}\{20\}\\
\bottomrule
\end{tabular}

\end{document}

Some way to solve?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The above question has been resolved, but I have a similar problem again:

The file . rnw with this code

\documentclass{book}  
\usepackage[top=3cm,bottom=2cm,left=2cm,right=2cm]{geometry} 
\usepackage[brazil]{babel} 
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{xcolor}


\begin{document}
\SweaveOpts{concordance=TRUE}

\chapter{Something}

<<echo=FALSE, cache=TRUE>>=
library(knitr)
library(kableExtra)

#CONFIGURANDO UMA CELULA COM LOGICA

a<-c("Name1","Name2","Name3","Name4","Name5")
b<-c(70,45,80,55,20)
c<-data.frame(a,b)
colnames(c)<-c("Name", "Grade")
library(dplyr)
d<-c%>%
  mutate_if( is.numeric, function(x) { cell_spec(x, "latex", bold = T, color = spec_color(x, end = 0.9),font_size = spec_font_size(x))})%>%
  mutate(
    Grade=cell_spec(Grade,"latex",color=ifelse(Grade>=60,"blue","red")))



d<-kable(d, "latex", booktabs=T, row.names = FALSE, align="c", escape=F)
@

\Sexpr{gsub("\\\\", "\\\\\\\\", d)}

\end{document}

produces this file . tex, which is not correct

\documentclass{book}  
\usepackage[top=3cm,bottom=2cm,left=2cm,right=2cm]{geometry} 
\usepackage[brazil]{babel} 
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{xcolor}


\usepackage{Sweave}
\begin{document}
\input{teste-concordance}

\chapter{Something}



\begin{tabular}{cc}
\toprule
Name & Grade\\
\midrule
Name1 & \textbackslash{}textcolor\{red\}\{\textbackslash{}textbackslash\{\}bgroup\textbackslash{}textbackslash\{\}fontsize\textbackslash{}\{15\textbackslash{}\}\textbackslash{}\{17\textbackslash{}\}\textbackslash{}textbackslash\{\}selectfont \textbackslash{}textbackslash\{\}textcolor[HTML]\textbackslash{}\{5DC962\textbackslash{}\}\textbackslash{}\{\textbackslash{}textbackslash\{\}textbf\textbackslash{}\{70\textbackslash{}\}\textbackslash{}\}\textbackslash{}textbackslash\{\}egroup\textbackslash{}\{\textbackslash{}\}\}\\
Name2 & \textbackslash{}textcolor\{red\}\{\textbackslash{}textbackslash\{\}bgroup\textbackslash{}textbackslash\{\}fontsize\textbackslash{}\{11\textbackslash{}\}\textbackslash{}\{13\textbackslash{}\}\textbackslash{}textbackslash\{\}selectfont \textbackslash{}textbackslash\{\}textcolor[HTML]\textbackslash{}\{2C718E\textbackslash{}\}\textbackslash{}\{\textbackslash{}textbackslash\{\}textbf\textbackslash{}\{45\textbackslash{}\}\textbackslash{}\}\textbackslash{}textbackslash\{\}egroup\textbackslash{}\{\textbackslash{}\}\}\\
Name3 & \textbackslash{}textcolor\{red\}\{\textbackslash{}textbackslash\{\}bgroup\textbackslash{}textbackslash\{\}fontsize\textbackslash{}\{16\textbackslash{}\}\textbackslash{}\{18\textbackslash{}\}\textbackslash{}textbackslash\{\}selectfont \textbackslash{}textbackslash\{\}textcolor[HTML]\textbackslash{}\{BBDF27\textbackslash{}\}\textbackslash{}\{\textbackslash{}textbackslash\{\}textbf\textbackslash{}\{80\textbackslash{}\}\textbackslash{}\}\textbackslash{}textbackslash\{\}egroup\textbackslash{}\{\textbackslash{}\}\}\\
Name4 & \textbackslash{}textcolor\{red\}\{\textbackslash{}textbackslash\{\}bgroup\textbackslash{}textbackslash\{\}fontsize\textbackslash{}\{13\textbackslash{}\}\textbackslash{}\{15\textbackslash{}\}\textbackslash{}textbackslash\{\}selectfont \textbackslash{}textbackslash\{\}textcolor[HTML]\textbackslash{}\{1F968B\textbackslash{}\}\textbackslash{}\{\textbackslash{}textbackslash\{\}textbf\textbackslash{}\{55\textbackslash{}\}\textbackslash{}\}\textbackslash{}textbackslash\{\}egroup\textbackslash{}\{\textbackslash{}\}\}\\
Name5 & \textbackslash{}textcolor\{red\}\{\textbackslash{}textbackslash\{\}bgroup\textbackslash{}textbackslash\{\}fontsize\textbackslash{}\{8\textbackslash{}\}\textbackslash{}\{10\textbackslash{}\}\textbackslash{}textbackslash\{\}selectfont \textbackslash{}textbackslash\{\}textcolor[HTML]\textbackslash{}\{440154\textbackslash{}\}\textbackslash{}\{\textbackslash{}textbackslash\{\}textbf\textbackslash{}\{20\textbackslash{}\}\textbackslash{}\}\textbackslash{}textbackslash\{\}egroup\textbackslash{}\{\textbackslash{}\}\}\\
\bottomrule
\end{tabular}

\end{document}
  • tries to put escape = F in kable

  • Great! Solved. As this option had not solved the initial problem that was omitting the bar in the output, nor have I tried it anymore. Thank you very much

  • for nothing!! I will post as reply then!

1 answer

2


To solve such a problem, just put:

escape = F in kable

d<-kable(d, "latex", booktabs=T, row.names = FALSE, align="c", escape = F)

Whenever you use a special character, or your function generates raw html code/LaTeX (that in that case the function cell_spec generates a code LaTeX), you need to talk to the Kable no "escape such characters/codes".

  • edited the question, the initial problem has been solved, but a new modification in the table is generating the same error even with escape=F

  • but what’s the mistake? Wouldn’t it be better to ask another question?

  • is coming out \textbackslash{} in place of the backslash and \{ in place of { and \} in place of }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.