[R] wrap long lines in table using "latex" in Hmisc

Marc Schwartz marc_schwartz at me.com
Sat Feb 27 01:03:33 CET 2010


On Feb 26, 2010, at 5:18 PM, Sharpie wrote:

> 
> 
> Ista Zahn wrote:
>> 
>> Hi Tao,
>> Just set the appropriate *.just argument, e.g.:
>> 
>> Dat <- data.frame(x1 = rep("this value consists of a long string of
>> text", 5), x2 = rep("this value consists of an even longer string of
>> text", 5))
>> 
>> library(Hmisc)
>> latex(Dat, col.just = rep("p{1in}",  2))
>> 
>> You can also set justification for column headings, column group
>> headings etc. See ?latex for details.
>> 
>> Best,
>> Ista
>> 
>> 
> 
> As Ista said, you can use the p{}, m{} and b{} LaTeX column specifications
> to create a table column that enforces a line wrap on it's contents.  See: 
> 
>  http://en.wikibooks.org/wiki/LaTeX/Tables#The_tabular_environment
> 
> for full details.
> 
> However, one problem with using say, p{2in}, is that the text is set *fully
> justified*.  This means that the inter-word spacing in each line is expanded
> so that the line fully occupies the allotted 2 inches of space.  For some
> tables the results are a typographical travesty.
> 
> The solution is to prepend a ">{justificationCommand}" to your column
> specification, such as:
> 
>> {\centering}p{2in}
> 
> The justification commands you can use are :
> 
>  \centering    -> Centers wrapped text
>  \raggedright -> *left* aligns wrapped text
>  \raggedleft   -> *right* aligns wrapped text
> 
> Remember to double the backslash if you are passing this command as an
> argument in R.
> 
> This trick will cause a LaTeX compilation error if used to specify the
> right-most column in a table, unless the hmisc latex() command produces
> tables that use "\tabularnewline" to invoke table row breaks instead of
> "\\".
> 
> Hope this helps.
> 
> -Charlie


One other option that you can use is to create a \newcommand that wraps text in a tabular, which you can then actually use within an existing table cell. This enables multiple lines of text within the cell, with line breaks that you specify. So you in effect end up with nested tables. Of course, the entire row height is adjusted accordingly, but this way, you don't need to specify a fixed column width.


For example, put the following in your .tex file (or .Rnw file) after the \begin{document} directive:

  \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}} 


Each of the above provides for Right, Left and Centered justification, respectively, within the table cell.

Then, you can create a cell entry that results in the following TeX markup:

   \multilineC{Line 1 \\ Line 2 \\ ...}


If you are cat()ing the output from R, you need to double the backslashes, so that you begin with something like:

   \\multilineC{Line 1 \\\\ Line 2 \\\\ ...}


I typically do this with headers for tables that would otherwise be too wide for the column.


So you would start with a long line of text:

LongLine <- "This is a really long line that needs to wrap in a table row"


Break it into chunks around 15 characters in length using strwrap():

> strwrap(LongLine, 15)
[1] "This is a"      "really long"    "line that"      "needs to wrap" 
[5] "in a table row"


Use paste() to begin to create the proper LaTeX markup for \multilineC:

TMP1 <- paste(strwrap(LongLine, 15), collapse = "\\\\")

> TMP1
[1] "This is a\\\\really long\\\\line that\\\\needs to wrap\\\\in a table row"


Now create the full line:

TMP2 <- paste("\\multiLine{", TMP1, "}") 

> TMP2
[1] "\\multiLine{ This is a\\\\really long\\\\line that\\\\needs to wrap\\\\in a table row }"


When you cat() the output, you get:

> cat(TMP2)
\multiLine{ This is a\\really long\\line that\\needs to wrap\\in a table row } 


"TMP2" can now be used in place of the original long line of text and when processed by 'latex', will be rendered properly.


Of course, rather than using strwrap(), you can hard code the line breaks into your character vector as you may otherwise require.


HTH,

Marc Schwartz



More information about the R-help mailing list