并排排列多个表。

时间:2022-01-01 02:23:29

The following code produces 2 tables on top of each other. How would I set it to have them aligned side by side, e.g. 3 to a row?

下面的代码生成两个表。我如何让它们并排对齐,例如,3到一行?

---
title: "sample"
output: pdf_document
---

```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```   

```{r sample, echo=FALSE, results='asis'}
library(knitr)
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
print(kable(t1))
print(kable(t2))
```

Output looks like this: 并排排列多个表。

输出是这样的:

2 个解决方案

#1


16  

Just put two data frames in a list, e.g.

把两个数据帧放在一个列表中,例如

t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
knitr::kable(list(t1, t2))

Note this requires knitr >= 1.13.

注意,这要求knitr >= 1.13。

#2


12  

I used this Align two data.frames next to each other with knitr? which shows how to do it in html and this https://tex.stackexchange.com/questions/2832/how-can-i-have-two-tables-side-by-side to align 2 Latex tables next to each other. It seems that you cannot freely adjust the lines of the table as you can do it with xtable (does anybody know more about this?). With format = Latex you get a horizontal line after each row. But the documentation shows two examples for other formats. One using the longtable package (additional argument: longtable = TRUE) and the other using the booktabs package (booktabs = TRUE).

我用了这个对齐的两个数据。帧与knitr相邻吗?它展示了如何在html和这个https://tex.stackexchange.com/questions/2832/how-can-i-have-two- tables-并排对齐两个乳胶表。似乎您不能随意调整表的行,因为您可以使用xtable(有人知道更多吗?)使用format = Latex时,每一行之后都有一条水平线。但是文档显示了其他格式的两个示例。一个使用长表包(附加参数:longtable = TRUE),另一个使用booktabs包(booktabs = TRUE)。

---
title: "sample"
output: pdf_document
header-includes:
- \usepackage{booktabs}
---

```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```


```{r sample, echo=FALSE, results='asis'}
library(knitr)
library(xtable)

t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE)
t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE)

cat(c("\\begin{table}[!htb]
    \\begin{minipage}{.5\\linewidth}
      \\caption{}
      \\centering",
        t1,
    "\\end{minipage}%
    \\begin{minipage}{.5\\linewidth}
      \\centering
        \\caption{}",
        t2,
    "\\end{minipage} 
\\end{table}"
))  
```

并排排列多个表。

#1


16  

Just put two data frames in a list, e.g.

把两个数据帧放在一个列表中,例如

t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
knitr::kable(list(t1, t2))

Note this requires knitr >= 1.13.

注意,这要求knitr >= 1.13。

#2


12  

I used this Align two data.frames next to each other with knitr? which shows how to do it in html and this https://tex.stackexchange.com/questions/2832/how-can-i-have-two-tables-side-by-side to align 2 Latex tables next to each other. It seems that you cannot freely adjust the lines of the table as you can do it with xtable (does anybody know more about this?). With format = Latex you get a horizontal line after each row. But the documentation shows two examples for other formats. One using the longtable package (additional argument: longtable = TRUE) and the other using the booktabs package (booktabs = TRUE).

我用了这个对齐的两个数据。帧与knitr相邻吗?它展示了如何在html和这个https://tex.stackexchange.com/questions/2832/how-can-i-have-two- tables-并排对齐两个乳胶表。似乎您不能随意调整表的行,因为您可以使用xtable(有人知道更多吗?)使用format = Latex时,每一行之后都有一条水平线。但是文档显示了其他格式的两个示例。一个使用长表包(附加参数:longtable = TRUE),另一个使用booktabs包(booktabs = TRUE)。

---
title: "sample"
output: pdf_document
header-includes:
- \usepackage{booktabs}
---

```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```


```{r sample, echo=FALSE, results='asis'}
library(knitr)
library(xtable)

t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE)
t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE)

cat(c("\\begin{table}[!htb]
    \\begin{minipage}{.5\\linewidth}
      \\caption{}
      \\centering",
        t1,
    "\\end{minipage}%
    \\begin{minipage}{.5\\linewidth}
      \\centering
        \\caption{}",
        t2,
    "\\end{minipage} 
\\end{table}"
))  
```

并排排列多个表。