Smich7 Smich7 - 4 months ago 61
LaTeX Question

Automatic linebreak knitr xtable (R/Latex)

I'm trying to do a

xtable
with long sentences at each row in my PDF document generated with
knitr
.

I tried that :

<<echo=F,results="asis",command=NA>>=
df<-c("This is a very very very very very very very very very very very very very very very very very very very very long line",
"Another very very very very very very very very very very very long line")
df<-as.data.frame(df)
xtab <- xtable(df)
print.xtable(xtab,sanitize.text.function=function(x){x})
@


But my result is :

enter image description here

As you can see. My xtable overflow the page because my lines are too long. Can someone help me ?

PS : I can't use
{tabular}
because the length of my data frame depend of a shiny app user's input.

Answer

Have a look at the align function:

<<echo=FALSE,results=tex>>=
library(xtable)
df<-c("This is a very very very very very very very very very very very very very very very very very very very very long line",
"Another very very very very very very very very very very very long line")
df<-as.data.frame(df)
xtab <- xtable(df)
align(xtab) <- "|lp{6cm}|"
print.xtable(xtab,sanitize.text.function=function(x){x})
@

You can use it to change the column alignment like you would for any other LaTeX table. These resources may be helpful to you:

  1. xtable gallery
  2. LaTeX Wikibook
Comments