Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R to LaTeX - use xtable to produce long table with line wrapping

Tags:

r

export

latex

I have a table with a large number of rows (90) and each contains a name, a definition, plus another text column. I am trying to output to latex such that this spans multiple pages and also fits to page width.

library("xtable")
glossary2<-data.frame(names=letters[1:4],definition=c("very long long long text","very long long long long long text","very long long long long long long text","very long long long text"),include=c(NA,"YES",NA,"NO"))
glossaryprint<-xtable(glossary2,label="tab:codebook",caption="glossary")

Further to the xtable documentation I have been able to produce the following

To make it fit to page width:

align(glossaryprint)<-"lXXX"
print(glossaryprint,tabular.environment="tabularx",width="\\textwidth")

To make it fit over multiple pages:

print(glossaryprint,tabular.environment="longtable",floating=FALSE)

However, combining the two techniques is causing me difficulties. I think I may have to have split the table into tables of say 20 rows each and use tabularx but that's very clunky and a lot of repeating code.

Is anyone able to suggest a method for making one long table fit to page width and linewrap inside columns when outputting to Latex using R functions?

like image 511
Steph Locke Avatar asked Feb 16 '23 18:02

Steph Locke


1 Answers

Will leave this open for a more elegant solution but...

The addition of this line allowed the hardcoding of column widths

library("xtable")
glossary2 <- data.frame(names=letters[1:4], definition=c("very long long long text","very long long long long long text","very long long long long long long text","very long long long text"), include=c(NA,"YES",NA,"NO"))
glossaryprint <- xtable(glossary2, label="tab:codebook", caption="glossary")

align(glossaryprint) <- "lp{2in}p{3in}p{1in}" #here is the change
print(glossaryprint, tabular.environment="longtable", floating=FALSE)
like image 99
Steph Locke Avatar answered Feb 18 '23 10:02

Steph Locke