0

Hello all I am new to R and Latex and have been stuck on creating a PDF with the xtable function.

The code runs fine and the table is generated in the console however I can not figure out how to save just the table as a pdf. Do I need to link R and Latex with a folder on my computer to save the file?

library(xtable)
options(xtable.floating = FALSE)
options(xtable.timestamp = "")

data(tli)
table<- xtable(tli[1:10, ])

print(table)

And I get this output

> library(xtable). 
> options(xtable.floating = FALSE)
> options(xtable.timestamp = "")
> 
> data(tli). 
> table<- xtable(tli[1:10, ])
> 
> print(table)
% latex table generated in R 3.5.0 by xtable 1.8-2 package
% 
\begin{tabular}{rrlllr}
  \hline
 & grade & sex & disadvg & ethnicty & tlimth \\ 
  \hline
1 &   6 & M & YES & HISPANIC &  43 \\ 
  2 &   7 & M & NO & BLACK &  88 \\ 
  3 &   5 & F & YES & HISPANIC &  34 \\ 
  4 &   3 & M & YES & HISPANIC &  65 \\ 
  5 &   8 & M & YES & WHITE &  75 \\ 
  6 &   5 & M & NO & BLACK &  74 \\ 
  7 &   8 & F & YES & HISPANIC &  72 \\ 
  8 &   4 & M & YES & BLACK &  79 \\ 
  9 &   6 & M & NO & WHITE &  88 \\ 
  10 &   7 & M & YES & HISPANIC &  87 \\ 
   \hline
\end{tabular}

What am I supposed to do next to save this table as a PDF?

Thank you for your time.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • It's typical to have this in an [rmarkdown file](https://rmarkdown.rstudio.com/), which can be rendered into docx and pdf (assuming you have some LaTeX compiler around, such as LiveTeX, AucTeX, MikTeX, TinyTeX). – r2evans Aug 07 '18 at 15:21

1 Answers1

2

To generate a PDF, make sure you have a LaTeX compiler installed (e.g. MikTeX). I would then install a LaTeX editor. There are plenty of editors around (almost all of them free, too). For example, I use TeXstudio.

In R, modify your code to: print(table,file="mytable.tex")

This will create a TeX document. Now, in your LaTeX editor, you can create a new TeX document with, such as:

\documentclass{article}
%This is the preamble

\begin{document}
%Put input here     
\end{document}

In LaTeX, you have a preamble where you can put any packages you may need. It seems with this output that you don't need any extra packages, but you may in the future. Put a % in front of anything that's a comment, similar to R's use of #.

Where it says %Put input here, you can replace that with \input(mytable.tex). Just make sure that your new TeX document and the table TeX document are in the same folder/directory.

When you compile your TeX document, you'll produce a PDF document with the table. As you learn LaTeX (and will probably have questions on how to modify your table), you can find helpful answers at tex.stackexchange.com.

Mason Malone
  • 166
  • 4