Skip to content
Prev 156575 / 398506 Next

getting line breaks with xtable

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)
$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 =
"\\\\"), "}")
[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: