首先,我会告诉你我要做什么,以防我做错了。我有一个嵌套表,我想在RStudio中使用knifer将其作为LaTeX表。我很好,直到我尝试添加一个标题。我尝试了tables
小插曲(链接)中第9页的示例。
它在没有标题的情况下工作,但当我添加标题时,它就不工作了。它也适用于非表格对象。有趣的是,latex.default
可以工作,但在RStudio/knitr的Compile 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}
我很不好意思承认这一点,但整个问题是我试图在不属于我的代码块中强行插入一些东西。我为帮助未来的搜索者而感到自豪。乳胶的东西在外面。因此,如果你试图将上面的表格绘制成一个格式良好的表格,这就是你想要的:
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}
tabular()中的x对象属于"tabular"类,并被调度到latex.tabular,后者没有标题参数。我猜它的预期用例在Sweave中,Sweave将负责提供标题。
然而,在第22页上有一个使用"\caption{.}"
参数对表中的选项进行渐晕处理的示例。这似乎带来了成功:
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\[-2normalbaselineskip]endheadhlineendfoot
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}
这应该可以工作。
cat('\begin{table}[ht]
\centering')
latex(tabularTable)
cat('\caption{some caption}')
cat('\label{tab:table1}')
cat('\end{table}')