28

I have a table that includes the following column:

 mytable <- data.frame(beta_0 = c(1,2,3)

What I want to do is output a table with a column header in latex markup, e.g. $\beta_0$

However, I can not seem to figure out how to output the "$\beta_0$" using print.xtable:

colnames(mytable) <- "$\beta_0$"
library(xtable)
print(xtable(mytable), include.rownames = F)

returns a column header of

\eta\_0\$

instead of

$\beta_0$

I presume that the answer is the "sanitize.colnames.function" argument to print.xtable, but it is not obvious to me how to use this, and ?print.xtable provides no examples.

Specifically, I would like to output a latex table like:

\begin{table}[ht]
 \begin{center}
  \begin{tabular}{r}
    \hline
    $\beta_0$ \\ 
    \hline
    1.00 \\ 
    2.00 \\ 
    3.00 \\ 
    \hline
  \end{tabular}
 \end{center}
\end{table}
David LeBauer
  • 31,011
  • 31
  • 115
  • 189
  • See also: http://stackoverflow.com/questions/32865384/function-to-sanitize-strings-for-latex-compilation – landroni Sep 30 '15 at 12:14

2 Answers2

33

Two issues here; first, you need a double backslash as otherwise it treats it as a control sequence. Second, by default, xtable sanitizes text so that it won't break LaTeX. Use one of the sanitize. parameters to control this; to do no sanitizing, pass it the identity function.

colnames(mytable) <- "$\\beta_0$"
print(xtable(mytable), include.rownames = F, sanitize.colnames.function = identity)
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • I had tried the double backslashes, but it was the `sanitize.colnames.function` that perplexed me. Is there a list of possible values for the `sanitize*` arguments? – David LeBauer Jan 04 '12 at 19:32
  • 1
    @David There is not a list of possible values for the `sanitize` arguments. As it clearly says in the docs: `it should be a function taking a character vector and returning one, and will be used for the sanitization instead of the default internal function`. Aaron has simply passed `identity` which is a function that does nothing. – joran Jan 04 '12 at 19:40
  • It takes any suitable function. `identity` is a base function that does nothing. – Aaron left Stack Overflow Jan 04 '12 at 19:41
  • got it: `identity <- function(x)x`. @joran I saw that part, I was curious to see if there were particular functions that are particularly useful with parsing "\"'s and other latex markup. But `identity` has solved my current issue. – David LeBauer Jan 04 '12 at 20:07
  • 1
    I don't know of any other particular functions that are out there; when I've needed something more complex I've started with the default one in xtable and modified it to meet my needs. – Aaron left Stack Overflow Jan 04 '12 at 20:10
  • 7
    @David you can always grep the source for a list of `sanitize` arguments: They are: `sanitize.rownames.function`, `sanitize.colnames.function`, `sanitize.text.function`, and can be set with `options` instead of as arguments to print, e.g. `options(xtable.sanitize.rownames.function=identity)` – cboettig May 13 '14 at 18:02
3

this is what did the trick for me:

mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)
rownames(mat) <- "$y_{t-1}$"
colnames(mat) <- c("$R^2$", "$\\bar{x}$", "F-stat", "S.E.E", "DW")
mat <- xtable(mat)
print(mat, sanitize.text.function = function(x){x})

This way you avoid the backslash issue in the table text.

Ane
  • 335
  • 1
  • 11