[R] backslash in xtable (generate latex code from R)

Marc Schwartz m@rc_@chw@rtz @end|ng |rom me@com
Fri Nov 6 14:04:28 CET 2020


Hi,

It looks like xtable is "sanitizing" special characters in the LaTeX output by default, by adding a double backslash, so that LaTeX will process the characters as literals, rather than specials. It is not parsing the boundary '$' characters to define insertion of math symbols inside text mode.

You have 'sanitize.colnames.function = identity', which is why the column name is output 'as is'.

You would seem to need to do the same thing for the text content within the table content:

> print(xtable(mytable), include.rownames = F, 
        sanitize.colnames.function = identity, 
        sanitize.text.function = identity)
% latex table generated in R 4.0.3 by xtable 1.8-4 package
% Fri Nov  6 07:54:51 2020
\begin{table}[ht]
\centering
\begin{tabular}{l}
  \hline
$\beta_0$ \\ 
  \hline
aa \\ 
  bb \\ 
  cc$\alpha_1$ \\ 
   \hline
\end{tabular}
\end{table}


Regards,

Marc Schwartz


> On Nov 6, 2020, at 7:39 AM, John <miaojpm using gmail.com> wrote:
> 
> I'd like to have $\alpha_1$ in my table, and the column name is $\beta_0$
> ####
> library(xtable)
> mytable <- data.frame(beta_0 = c("aa","bb","cc$\\alpha_1$"))
> colnames(mytable) <- "$\\beta_0$"
> print(xtable(mytable), include.rownames = F, sanitize.colnames.function =
> identity)
> ####
> 
> No problem with \beta_0, but a problem with \alpha_1:
> 
> \begin{table}[ht]
> \centering
> \begin{tabular}{l}
>  \hline
> $\beta_0$ \\
>  \hline
> aa \\
>  bb \\
>  cc\$$\backslash$alpha\_1\$ \\
>   \hline
> \end{tabular}
> \end{table}
> 
> How may I fix the $\alpha_1$? Thanks!



More information about the R-help mailing list