2

我正在使用 kableExtra 包在 R markdown 中将表格输出为 PDF。

我使用命令 group_rows() 将表中的一些行组合在一起。

我的第一列的某些行中的文本对于列宽来说太长了,所以它被分成两行。但是,第二行没有缩进。有没有办法缩进第二行或整体删除缩进?

不幸的是,增加列宽以使文本不会分布在两行上是没有选择的,因为我的真实表格中有更多的列。

这是我的数据框的一个子集:

data <- structure(list(`Control variables` = c("GDP growth", "GDP per capita", 
"Top income tax rate", "Right-wing executive"), Treated = structure(c("2.29", 
"21,523.57", "0.70", "0.62"), class = "AsIs"), top10_synthetic = structure(c("3.37", "19,939.72", "0.68", "0.63"), class = "AsIs"), top10_mean = structure(c("2.95", "30,242.60", "0.64", "0.43"), class = "AsIs")), .Names = c("Control variables", "Treated", "top10_synthetic", "top10_mean"), row.names = c(NA, 4L), class = "data.frame")

这是我正在使用的代码:

```{r}

kable(data, "latex", caption = "table 1", booktabs = T, col.names = c("Control variables", "Treated", "Synthetic", "Mean")) %>%
  add_header_above(c("", "", "Top 10%" = 2)) %>%
  group_rows("UK", 1, 2) %>%
  group_rows("Japan", 3, 4, latex_gap_space = "0.8cm") %>%
  footnote(general = "xxx") %>%
  kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
  column_spec(1, width = "3cm")

```

这就是 .pdf 输出的样子。如您所见,例如文本“最高所得税率”分为两行,我希望第二行像第一行一样缩进。

在此处输入图像描述

谢谢你的任何提示!

4

1 回答 1

1

如果您只是在 R 控制台中运行该块,您将看到以下 LaTeX 输出:

\begin{table}[H]

\caption{\label{tab:}table 1}
\centering
\resizebox{\linewidth}{!}{
\begin{tabular}[t]{>{\raggedright\arraybackslash}p{3cm}lll}
\toprule
\multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{2}{c}{Top 10\%} \\
\cmidrule(l{2pt}r{2pt}){3-4}
Control variables & Treated & Synthetic & Mean\\
\midrule
\addlinespace[0.3em]
\multicolumn{4}{l}{\textbf{UK}}\\
\hspace{1em}GDP growth & 2.29 & 3.37 & 2.95\\
\hspace{1em}GDP per capita & 21,523.57 & 19,939.72 & 30,242.60\\
\addlinespace[0.8cm]
\multicolumn{4}{l}{\textbf{Japan}}\\
\hspace{1em}Top income tax rate & 0.70 & 0.68 & 0.64\\
\hspace{1em}Right-wing executive & 0.62 & 0.63 & 0.43\\
\bottomrule
\multicolumn{4}{l}{\textit{Note: }}\\
\multicolumn{4}{l}{xxx}\\
\end{tabular}}
\end{table}

如您所见,kableExtra不是在该标题中插入换行符,而是 LaTeX 正在这样做。这意味着您需要 LaTeX 修复该问题。也许其他人知道一个更简单的方法,但我能找到的最好的方法如下:将长行标题包装在一个minipage环境中,并调整间距以看起来更好。

由于这有点混乱,我会编写一个 R 函数来做到这一点:

inMinipage <- function(x, width) 
  paste0("\\begin{minipage}[t]{", 
         width, 
         "}\\raggedright\\setstretch{0.8}", 
         x, 
         "\\vspace{1.2ex}\\end{minipage}")

这需要在放入表中的数据上调用,并且kable需要被告知不要转义那些反斜杠(使用escape = FALSE)。此外,该\setstretch命令来自setspaceLaTeX 包。因此,总体而言,您的示例文档将如下所示:

---
output: 
  pdf_document:
    extra_dependencies: setspace
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
library(knitr)
```

```{r}
inMinipage <- function(x, width) 
  paste0("\\begin{minipage}[t]{", 
         width, 
         "}\\raggedright\\setstretch{0.8}", 
         x, 
         "\\end{minipage}")

data <- structure(list(`Control variables` = c("GDP growth", "GDP per capita", "Top income tax rate", "Right-wing executive"), Treated = structure(c("2.29", 
"21,523.57", "0.70", "0.62"), class = "AsIs"), top10_synthetic = structure(c("3.37", "19,939.72", "0.68", "0.63"), class = "AsIs"), top10_mean = structure(c("2.95", "30,242.60", "0.64", "0.43"), class = "AsIs")), .Names = c("Control variables", "Treated", "top10_synthetic", "top10_mean"), row.names = c(NA, 4L), class = "data.frame")

data[[1]] <- inMinipage(data[[1]], "2.5cm")

kable(data, "latex", caption = "table 1", booktabs = T, col.names = c("Control variables", "Treated", "Synthetic", "Mean"), escape = FALSE) %>%
  add_header_above(c("", "", "Top 10%" = 2)) %>%
  group_rows("UK", 1, 2) %>%
  group_rows("Japan", 3, 4, latex_gap_space = "0.8cm") %>%
  footnote(general = "xxx") %>%
  kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
  column_spec(1, width = "3cm")
```

使用该代码,我看到了:

在此处输入图像描述

间距不太对,但越来越近了。我希望这有帮助。

于 2018-06-17T00:10:35.910 回答