首先,我将告诉您我打算做的事情,以防万一我做错了。我有一个嵌套表,希望使用knitr在RStudio中作为LaTeX表放出来。在尝试添加标题之前,我很好。我尝试了tables插图(LINK)中第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}

最佳答案

来自tabular()的x对象属于'tabular'类,将被分派(dispatch)到没有标题参数的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}

关于r - Hmisc::latex不打印带有表格对象的标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12398093/

10-15 23:27