6

首先,我会告诉你我正在尝试做些什么,以防万一我做错了。我有一个嵌套表,我想在 RStudio 中使用 knitr 将其作为 LaTeX 表输出。我很好,直到我尝试添加标题。tables我在小插图(LINK)中尝试了第 9 页上的示例。

它可以在没有标题的情况下使用,但是当我添加标题时它不会。它也适用于非表格对象。有趣的是,它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}
4

3 回答 3

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}
于 2012-09-13T03:51:39.500 回答
7

tabular() 中的 x 对象属于 'tabular' 类,并且被分派到没有标题参数的 latex.tabular 。我猜它的预期用例在 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\\[-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}
于 2012-09-13T01:30:47.143 回答
0

这应该有效。

cat('\\begin{table}[ht]
    \\centering')
latex(tabularTable)
cat('\\caption{some caption}')
cat('\\label{tab:table1}')
cat('\\end{table}')
于 2015-08-29T09:02:27.407 回答