Tyl*_*ker 6 latex r tabular hmisc
首先,我会告诉你我正在尝试做大事,以防我错了.我有一个嵌套的表,我希望使用knitr将其作为LaTX表放在RStudio中.我很好,直到我尝试添加标题.我在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 :(
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望能够\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}
Run Code Online (Sandbox Code Playgroud)
我很尴尬承认这一点,但整个问题是我试图在不属于的代码块内强制某些东西.我为自己的骄傲感到窒息,以帮助未来的搜索者.乳胶的东西在外面.因此,如果您尝试将上面的表格绘制为格式良好的表格,那么这就是您要寻找的内容:
\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}
Run Code Online (Sandbox Code Playgroud)
tableular()中的x对象是"tabular"类,并且被分派到latex.tabular,它没有caption参数.我猜测它的用例是在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}
Run Code Online (Sandbox Code Playgroud)