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! [[alternative HTML version deleted]]
Thierry Onkelinx
2020-Nov-06 12:42 UTC
[R] backslash in xtable (generate latex code from R)
You could use kable() from the knitr package. kable(mytable, format = "latex", escape = FALSE) \begin{tabular}{l} \hline $\beta_0$\\ \hline aa\\ \hline bb\\ \hline cc$\alpha_1$\\ \hline \end{tabular} ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkelinx at inbo.be Havenlaan 88 bus 73, 1000 Brussel www.inbo.be /////////////////////////////////////////////////////////////////////////////////////////// To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey /////////////////////////////////////////////////////////////////////////////////////////// <https://www.inbo.be> Op vr 6 nov. 2020 om 13:30 schreef John <miaojpm at gmail.com>:> 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! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
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 at 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!