在markdown中对齐两个表

时间:2018-03-29 20:55:19

标签: r latex knitr

问题描述:

我正在rMarkdown中撰写报告,我需要在A4 PDF文档中并排设置两个表格。

previous question的回答建议将表格列表传递给kable;或将表格插入以下latex代码:

 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}"
)) 

将其添加到我的代码(下面)会在生成的PDF中生成一个latex代码页,其中没有我想要的表。将表作为列表传递似乎不起作用,可能是因为每个表上都有唯一的kableExtra格式。

可重复的例子:

---
output: pdf_document
header-includes:
- \usepackage{booktabs}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(kableExtra)
library(tibble)
```
```{r echo = FALSE}
df1 <- tribble(~Location, ~Jan_Qrt, ~Jan_year, ~growth,
"New South Wales",  16000,  403300,       3.30,
"Victoria",        -21200,    134100,       3.50,
"Queensland",        2100,  100200,       3.20,
"South Australia",  19700,  117900,       5.00,
"Western Australia",   5300,   13200,     1.60,
"Tasmania",         -8900,   24300,     1.90,
"Northern Territory",    -400,    5700,    2.40,
"Australian Cap",     300,   -4300,    -3.10,
"Australia",         800,   10600,     4.80)

df2 <- read.table(text = '"Location" "qtr"  "year" "growth"

 "New South Wales"               " 16,000"  403,300       3.30
 "Victoria"                      -21,200    134,100       3.50
 "Queensland"                    "  2,100"  100,200       3.20
 "South Australia"               " 19,700"  117,900       5.00
 "Western Australia"             "  5,300"  " 13,200"     1.60
 "Tasmania"                      " -8,900"  " 24,300"     1.90
 "Northern Territory*"           "   -400"  "  5,700"     2.40
 "Australian Capital Territory*" "    300"  " -4,300"    -3.10
 "Australia**"                   "    800"  " 10,600"     4.80', header = T)

t1 <- kable(df1, format = 'latex', booktabs = T, align = 'r', escape = F) %>%
  kable_styling(full_width = F, font_size = 8) %>%
  row_spec(row = 9, bold = TRUE) %>%
  add_header_above(c(" ", first = 1, "TTY (persons)" = 1, "TTY (% chg.)" = 1))

t2 <- kable(df2, format = 'latex', booktabs = T, align = 'r', escape = F) %>% 
  kable_styling(full_width = F, font_size = 8)

# kable(list(t1, t2)) <- not run

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}"
))

```

0 个答案:

没有答案