2012-09-13 33 views
6

首先,我会告诉你我正在试图做大局面,以防万一我错了。我有一个嵌套的表格,我想使用knitr将它作为LaTeX表格放入RStudio中。我很好,直到我尝试添加标题。我尝试了tables vignette (LINK)中第9页的示例。Hmisc ::乳胶不打印标题w /表格对象

它没有标题,但当我添加标题它没有。它也适用于非表格对象。有趣的是,latex.default的作品,但在RStudio/knitr的编译PDF和我所读的是由latex无论如何称为错误;再加上表格不适合了。我尝试了latexTabular,但是也没有适当地舍入。

library(Hmisc); library(tables) 
latex(head(mtcars), file="", caption="de") #works 

x <- tabular((Species + 1) ~ (n=1) + Format(digits=2)* 
     (Sepal.Length + Sepal.Width)*(mean + sd), data=iris) 

latex(x, file="", caption="de") #no caption :(

理想我希望能够在输出\caption{de}但无法弄清楚我要去哪里错了。

在情况下,它是有帮助这里的输入和输出:从平板状

> latex(x, file="", caption="de", label="tab1") 
\begin{tabular}{lccccc} 
\hline 
& & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\ 
Species & n & mean & sd & mean & sd \\ 
\hline 
setosa & $\phantom{0}50$ & $5.01$ & $0.35$ & $3.43$ & $0.38$ \\ 
versicolor & $\phantom{0}50$ & $5.94$ & $0.52$ & $2.77$ & $0.31$ \\ 
virginica & $\phantom{0}50$ & $6.59$ & $0.64$ & $2.97$ & $0.32$ \\ 
All & $150$ & $5.84$ & $0.83$ & $3.06$ & $0.44$ \\ 
\hline 
\end{tabular} 
+0

'tabular()'从哪里来? –

+0

@Josh我的道歉表格来自表格包。 –

+1

@Brandon我不认为xtable可以处理嵌套表,但我很想错,因为xtable非常适合使用。通常情况下,我很清楚他的表格,但我试图走向完全可重复的研究,并将所有内容作为一个文件与knitr一起运行。 –

回答

6

x对象()是类的片状'的和被分派到latex.tabular不具有标题参数。我猜测它的用例是在Sweave里面的,它的任务是提供标题。

但是,在页面22上有一个使用参数"\\caption{.}"参数的示例,可以使用表vignette中的选项。这似乎获得成功:

x <- tabular((Species + 1) ~ (n=1) + Format(digits=2)* 
      (Sepal.Length + Sepal.Width)*(mean + sd), data=iris) 

latex(x, file="", options = list(tabular="longtable", toprule="\\caption{This is a sample caption.}\\\\ \\toprule", midrule="\\midrule\\\\[-2\\normalbaselineskip]\\endhead\\hline\\endfoot")) 
\begin{longtable}{lccccc} 
\caption{This is a sample caption.}\\ \toprule 
& & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\ 
Species & n & mean & sd & mean & sd \\ 
\midrule\\[-2\normalbaselineskip]\endhead\hline\endfoot 
setosa & $\phantom{0}50$ & $5.01$ & $0.35$ & $3.43$ & $0.38$ \\ 
versicolor & $\phantom{0}50$ & $5.94$ & $0.52$ & $2.77$ & $0.31$ \\ 
virginica & $\phantom{0}50$ & $6.59$ & $0.64$ & $2.97$ & $0.32$ \\ 
All & $150$ & $5.84$ & $0.83$ & $3.06$ & $0.44$ \\ 
\hline 
\end{longtable} 
+0

这工作迪文 –

+0

发现并不困难。我搜索了单词'caption'。使用我对LaTeX有限的理解来工作是值得的“+”。 –

9

我不好意思地承认这一点,但整个问题我试图迫使不属于代码块里面的东西。我窒息了我的自豪感,以帮助未来的搜索者。乳胶的东西在外面。所以如果你想把上面的表格绘制成格式良好的表格,这就是你要找的东西:

\begin{table}[ht] 
\caption{This is a sample caption. \label{guy}} 
<<desc, echo = FALSE, results = 'asis'>>= 
x <- tabular((Species + 1) ~ (n=1) + Format(digits=2)* 
    (Sepal.Length + Sepal.Width)*(mean + sd), data=iris) 
latex(x) 
@ 
\end{table} 
+0

+1不需要为此感到尴尬。如果你无法在R中完成所有工作,这是一个很好的答案和很好的回退。 – Andrie

0

这应该是有效的。

cat('\\begin{table}[ht] 
    \\centering') 
latex(tabularTable) 
cat('\\caption{some caption}') 
cat('\\label{tab:table1}') 
cat('\\end{table}')