group_rows() 命令中换行时的缩进 - R markdown 中的 kableExtra 包
Indentation when line break in group_rows() command - kableExtra package in R markdown
我正在使用 kableExtra 包在 R markdown 中将 table 输出为 PDF。
我使用命令 group_rows() 将我的 table 的一些行组合在一起。
我第一列的某些行中的文字对于列宽而言太长,因此分成两行。但是,第二行没有缩进。有没有办法缩进第二行或删除整个缩进?
不幸的是,增加列宽使文本不会分布在两行中是没有选择的,因为我的实际中有更多的列 table。
这是我的数据框的一个子集:
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 输出的样子。如您所见,例如文本 "top income tax rate" 分为两行,我希望第二行像第一行一样缩进。
感谢您的任何提示!
如果你只是 运行 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
环境中,并使用 fiddle 间距看起来更好。
因为这有点乱,我会写一个 R 函数来完成它:
inMinipage <- function(x, width)
paste0("\begin{minipage}[t]{",
width,
"}\raggedright\setstretch{0.8}",
x,
"\vspace{1.2ex}\end{minipage}")
这需要在放入 table 的数据上调用,并且需要告知 kable
不要转义那些反斜杠(使用 escape = FALSE
)。此外,\setstretch
命令来自 setspace
LaTeX 包。所以总体而言,您的示例文档如下所示:
---
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")
```
使用该代码我看到了这个:
间距不太对,但已经接近了。希望对您有所帮助。
我正在使用 kableExtra 包在 R markdown 中将 table 输出为 PDF。
我使用命令 group_rows() 将我的 table 的一些行组合在一起。
我第一列的某些行中的文字对于列宽而言太长,因此分成两行。但是,第二行没有缩进。有没有办法缩进第二行或删除整个缩进?
不幸的是,增加列宽使文本不会分布在两行中是没有选择的,因为我的实际中有更多的列 table。
这是我的数据框的一个子集:
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 输出的样子。如您所见,例如文本 "top income tax rate" 分为两行,我希望第二行像第一行一样缩进。
感谢您的任何提示!
如果你只是 运行 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
环境中,并使用 fiddle 间距看起来更好。
因为这有点乱,我会写一个 R 函数来完成它:
inMinipage <- function(x, width)
paste0("\begin{minipage}[t]{",
width,
"}\raggedright\setstretch{0.8}",
x,
"\vspace{1.2ex}\end{minipage}")
这需要在放入 table 的数据上调用,并且需要告知 kable
不要转义那些反斜杠(使用 escape = FALSE
)。此外,\setstretch
命令来自 setspace
LaTeX 包。所以总体而言,您的示例文档如下所示:
---
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")
```
使用该代码我看到了这个:
间距不太对,但已经接近了。希望对您有所帮助。