Search code examples
rlatexxtable

xtable: how to only keep part of the tex output?


Consider this simple example

data <- data.frame(string = c('one','two'))

data
#   string
# 1    one
# 2    two

I want to write this data.frame to a tex file.

I can use xtable

xtable(data)

with TeX output

% latex table generated in R 3.3.1 by xtable 1.8-2 package
% Wed Jan 04 11:39:40 2017
\begin{table}[ht]
\centering
\begin{tabular}{rl}
  \hline
 & string \\ 
  \hline
1 & one \\ 
  2 & two \\ 
   \hline
\end{tabular}
\end{table}

Problem is: I need to get rid of

\begin{table}[ht]
\centering
\begin{tabular}{rl}
  \hline

and

\end{tabular}
\end{table}

so that I only have

 & string \\ 
  \hline
1 & one \\ 
  2 & two \\ 
   \hline

in my tex file.

Any ideas how to do that? Thanks!


Solution

  • That's exactly the point of the only.contents argument to print.xtable:

    print(xtable(data), only.contents = TRUE)
    

    With TeX output

    % latex table generated in R 3.3.1 by xtable 1.8-2 package
    % Wed Jan  4 11:49:25 2017
     & string \\ 
      \hline
    1 & one \\ 
      2 & two \\ 
       \hline
    

    (it also has an argument comment if you'd like to suppress the TeX comment)