[R] getting line breaks with xtable

Marc Schwartz marc_schwartz at comcast.net
Fri Sep 19 17:02:46 CEST 2008


I don't believe that \linebreak will work within a tabular environment
as you expect here. Plus, be aware, that it is print.xtable() and not
LaTeX that is adding the additional $\backslash$ characters, as part of
the text sanitization process.

I think that you basically have two options:

1. Define a so-called 'p' column for the wide text column, which
essentially creates a paragraph environment within each cell in that
column, and therefore allows for line wrapping. See the 'align' argument
in ?xtable. So use something like this:

  xtable(x, align = "lrp{3in}")

'p' columns can have undesirable effects as well depending upon certain
details, so you can also look at the following option.


2. Use a LaTeX macro and embed that in the character vector. I have
created three such macros, one each for Right, Left and Center
justification of the text within the cell:

\newcommand{\multilineR}[1]{\begin{tabular}[b]{@{}r@{}}#1\end{tabular}}
\newcommand{\multilineL}[1]{\begin{tabular}[b]{@{}l@{}}#1\end{tabular}}
\newcommand{\multilineC}[1]{\begin{tabular}[b]{@{}c@{}}#1\end{tabular}}


Put these in your .Rnw file, after the \begin{document} declaration.
This essentially creates a tabular environment within the cell in the
parent table. Then modify your long vectors by using something like:

# See ?strwrap. Wrap each vector at max char length 20
LVec <- lapply(x[, 2], strwrap, 20)

> LVec
$A
[1] "this is an example" "for a long"         "character string"
[4] "that I want break"  "into several lines"

$B
[1] "this is an example" "for a long"         "character string"
[4] "that I want break"  "into several lines"

$C
[1] "this is an example" "for a long"         "character string"
[4] "that I want break"  "into several lines"


# Beware of any line wrapping in the e-mail here
# Add in the line breaks and macro text
FinalVec <- paste("\\multilineL{", sapply(LVec, paste, collapse =
"\\\\"), "}")

> FinalVec
[1] "\\multilineL{ this is an example\\\\for a long\\\\character
string\\\\that I want break\\\\into several lines }"
[2] "\\multilineL{ this is an example\\\\for a long\\\\character
string\\\\that I want break\\\\into several lines }"
[3] "\\multilineL{ this is an example\\\\for a long\\\\character
string\\\\that I want break\\\\into several lines }"


# Modify the second column in the table
x[, 2] <- FinalVec


Now use:

  print(xtable(x), sanitize.text.function = function(x){x})

which will output the table and leave the LaTeX directives intact. Watch
line wrapping in the e-mail again here:

% latex table generated in R 2.7.2 by xtable 1.5-3 package
% Fri Sep 19 09:54:10 2008
\begin{table}[ht]
\begin{center}
\begin{tabular}{rll}
  \hline
 & A & B \\
  \hline
A & 1 & \multilineL{ this is an example\\for a long\\character
string\\that I want break\\into several lines } \\
  B & 2 & \multilineL{ this is an example\\for a long\\character
string\\that I want break\\into several lines } \\
  C & 3 & \multilineL{ this is an example\\for a long\\character
string\\that I want break\\into several lines } \\
   \hline
\end{tabular}
\end{center}
\end{table}


HTH,

Marc Schwartz


on 09/19/2008 09:11 AM Erich Studerus wrote:
> Ok. Sorry, here's a reproducible example:
> 
> library(xtable)
> x<-as.table(cbind(1:3,rep("this is an example for a long character string
> that I want break into several lines")))
> xtable(x)
> 
> Regards
> 
> Erich
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] 
> Gesendet: Freitag, 19. September 2008 15:50
> An: Erich Studerus
> Cc: r-help at r-project.org
> Betreff: Re: [R] getting line breaks with xtable
> 
> Read the last line to every message to r-help to find out
> one reason you may be getting no responses.
> 
> On Fri, Sep 19, 2008 at 9:39 AM, Erich Studerus
> <erich.studerus at bli.uzh.ch> wrote:
>> Sorry, for asking the same question again, but I got no reactions the last
>> time. Maybe it was just overseen by the experts.
>> I'm using the xtable function with Sweave and Lyx and I would like to know
>> how to get automatic line breaks for long strings in a column of the
> table.
>> I've learned from the Lyx wiki that the Latex command \linebreak produces
>> table cells with multiple lines. I tried to insert \linebreak into the
>> character string, but it didn't work out, because Sweave transforms it
>> automatically to $\backslash$linebreak.
>>
>> Any help is highly appreciated.
>>
>> Erich
>>



More information about the R-help mailing list