backslash in xtable (generate latex code from R)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

backslash in xtable (generate latex code from R)

miao
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: backslash in xtable (generate latex code from R)

R help mailing list-2
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
[hidden email]
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 <[hidden email]>:

> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: backslash in xtable (generate latex code from R)

R help mailing list-2
In reply to this post by miao
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 <[hidden email]> 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!

______________________________________________
[hidden email] 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.