Tables created from latex() from the Hmisc-package are horisontally left-aligned instead of being centered horisontally in the pdf-document
Asked Answered
E

1

6

Using R-studio and Knitr to create a pdf I am unable to get the tables centered horisontally. As seen from the example below, it works fine using xtable(), but the latex()-tabels are all left-aligned. As I understand the Hmisc-documentation, tables created from latex() should be horisontally centered automatically, but i must be doing something wrong.

\documentclass{article}

\begin{document}

<<>>=
library(Hmisc)
library(tables)
library(xtable)
@


The tables are all left-aligned:
<<results='asis'>>=
latex(    tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris )  )
@

<<results='asis'>>=
latex(    tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris ),center="center"   )
@

<<results='asis'>>=
latex(    tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris ),center="centering"   )
@


I have tried to use the fig.align option, but it does not do it:
<<results='asis',fig.align='center'>>=
latex(    tabular(   (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris      )    )
@


with xtable it automatically centers:
<<results='asis'>>=
xtable(table(Puromycin$conc, Puromycin$state))
@

\end{document}

R version 3.0.0 (2013-04-03)

Platform: x86_64-w64-mingw32/x64 (64-bit)

Eartha answered 7/5, 2013 at 7:43 Comment(2)
What features of latex are you missing that are not available in xtable? Looks like center is a dead argument. I'm investigating...Patronymic
The xtable() unfortunately does not accept tabular-objects.Eartha
V
5

I don't have the time to go through the code in latex.s of the Hmisc package, but until I do, feel free to wrap your chunks into centering environment. Not the cleanest solution, but it gets the job done.

\begin{centering}
  <<results='asis'>>=
  latex(tabular((Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris ))
@
\end{centering}

This produces a centered table.

Vehement answered 7/5, 2013 at 9:6 Comment(1)
That's a good approach. The help file for Hmisc::tabulr also shows how to enclose the output of latex(tabulr()) in arbitrary LaTeX markup. Note that the object produced by tabulr is not an object that Hmisc::latex is meant to handle. The simpler latex method in the tables package is meant for this output.Heredity

© 2022 - 2024 — McMap. All rights reserved.