0

在一个Rnw文件中

\documentclass[twoside,nobib,ls,notoc,nofonts]{tufte-book}
<<>>=
library(tidyverse)
library(xtable)
library(kableExtra)
@

这将被编译, knitr后跟pdflatex.

我有一个小标题:

tb <- tibble(measure = c("mean", "variance"), mean = c(24.4, 0.808), median = c(24.5, 4.37), efficiency = c(NA, 0.185))

我正在尝试制作一个保证金表:

\begin{margintable}
\caption[Results]{\label{tab:bootstrapEfficiency}Results of bootstrap}
\centering
\begin{tabular}[t]{lrrr}
\toprule
\multicolumn{1}{c}{} & \multicolumn{2}{c}{measure of location} & \multicolumn{1}{c}{} \\
\cmidrule(l{3pt}r{3pt}){2-3}
\rotatebox{45}{measure} & \rotatebox{45}{mean} & \rotatebox{45}{median} & \rotatebox{45}{efficiency}\\
\midrule
mean & 24.4 & 24.5 & NA\\
variance & 0.8 & 4.4 & 0.18\\
\bottomrule
\end{tabular}
\end{margintable}

kable没有产生我希望的结果:

kable(bootTb,
      format = "latex",
      booktabs = TRUE,
      caption = "Results of bootstrap",
      caption.short = "Results",
      label = "bootstrapEfficiency",
      digits = c(0, 1, 1, 2)) %>%
  add_header_above(c("", "measure of location" = 2, "")) %>%
  kable_styling(full_width = FALSE, latex_table_env = "margintable") %>%
  row_spec(0, angle = 45)

输出是:

\begin{table}
\caption[Results]{\label{tab:bootstrapEfficiency}Results of bootstrap}
\centering
\begin{margintable}{lrrr}
\toprule
\multicolumn{1}{c}{} & \multicolumn{2}{c}{measure of location} & \multicolumn{1}{c}{} \\
\cmidrule(l{3pt}r{3pt}){2-3}
\rotatebox{45}{measure} & \rotatebox{45}{mean} & \rotatebox{45}{median} & \rotatebox{45}{efficiency}\\
\midrule
mean & 24.4 & 24.5 & NA\\
variance & 0.8 & 4.4 & 0.18\\
\bottomrule
\end{margintable}
\end{table}

这不是正确的 LaTeX。当然,删除latex_table_env = "margintable"会产生很好的 LaTeX 结果,但不能用margintable.

相比之下,

xtable(tb) %>%
  print.xtable(type = "latex",
               floating = FALSE,
               booktabs = TRUE,
               rotate.colnames = TRUE)

生产

\begin{tabular}{rlrrr}
  \toprule
 & \begin{sideways} measure \end{sideways} & \begin{sideways} mean \end{sideways} & \begin{sideways} median \end{sideways} & \begin{sideways} efficiency \end{sideways} \\ 
  \midrule
1 & mean & 24.40 & 24.50 &  \\ 
  2 & variance & 0.81 & 4.37 & 0.18 \\ 
   \bottomrule
\end{tabular}

可以放置在 ``margintable` 环境中,但列名不会旋转 45 度,并且它们上方没有额外的标题。

我尝试使用xtable2kable但看不到如何让它产生所需的结果。

4

0 回答 0