R kableExtra乳胶:在换行的标题行中添加换行符

时间:2018-08-27 09:51:14

标签: r latex knitr kableextra

我正在尝试将表格生成为Latex-pdf文档。我在R中使用kableExtra和knitr。

我有一个表,该表的列名很长。当我将标题行旋转90度时,换行符将不起作用。有谁知道我如何实现轮换行和换行符?

我的示例与Hao's Best Practice for newline in Latex table中的示例相同,但是我在代码末尾添加了管道 row_spec

\documentclass[10pt,a4paper]{article}
\usepackage[table]{xcolor}

\begin{document}
\begin{table}
<<global_options, echo=FALSE>>=
library(kableExtra)
library("dplyr")
dt_lb <- data.frame(
  Item = c("Hello\nWorld", "This\nis a cat"),
  Value = c(10, 100)
)
dt_lb %>%
  mutate_all(linebreak) %>%
  kable("latex", booktabs = T, escape = F,
        col.names = linebreak(c("Item\n(Name)", "Value\n(Number)"))) %>%
  row_spec(0, angle = 90, align='l', monospace=T)

@

\end{table}
\end{document}

我得到的是这个,但是 [l] 标签提示标签也有其他问题:

enter image description here

在StackExchange TEX上,我发现了有关旋转和换行符的问题,这是我正在尝试实现的目标:https://tex.stackexchange.com/questions/14730/big-table-with-rotated-column-labels-using-booktabs

3 个答案:

答案 0 :(得分:1)

您可以使用tableHTML

library(tableHTML)

"\n"标签HTML替换换行符<br>

headers <- c("Item\n(Name)", "Value\n(Number)") %>% 
  stringr::str_replace_all(pattern = "\\n", replacement = "<br>")

创建一个tableHTML对象,并使用add_css_header()旋转标题:

dt_lb %>% 
  tableHTML(rownames = FALSE,
            headers = headers,
            escape = FALSE,
            widths = c(100, 100),
            theme = 'scientific') %>%
  add_css_header(css = list(c('transform', 'height', 'text-align'), 
                            c('rotate(-45deg)', '70px', 'center')),
                 headers = 1:2)

结果如下:

enter image description here

注意:您可以添加更多CSS或默认主题。查看vignettes以获得更多详细信息:

如果要创建pdf文档,可以使用RMarkdown

---
title: "tableHTML2pdf"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
\

```{r}

library(tableHTML)

dt_lb <- data.frame(
  Item = c("Hello\nWorld", "This\nis a cat"),
  Value = c(10, 100)
)

headers <- c("Item\n(Name)", "Value\n(Number)") %>% 
  stringr::str_replace_all(pattern = "\\n", replacement = "<br>")


dt_lb %>% 
  tableHTML(rownames = FALSE,
            headers = headers,
            escape = FALSE,
            widths = c(100, 100),
            theme = 'scientific') %>%
  add_css_header(css = list(c('transform', 'height', 'text-align'), 
                            c('rotate(-45deg)', '70px', 'center')),
                 headers = 1:2)
```

\

然后它将创建一个HTML文件。然后可以使用wkhtmltopdf将此文件转换为PDF

答案 1 :(得分:0)

我认为您可能已将rmarkdown文档呈现为HTML ...在rmarkdown yaml标头中,它现在说html_document吗?如果是这样,您可以尝试将其更改为pdf_document .....

我用完全相同的代码渲染了pdf_document,我想我得到了您想要的...

enter image description here

---
title: "Table Sample"
output: pdf_document
---

``` {r, include = FALSE}
library(tidyverse)

library(kableExtra)
```

```{r}
dt_lb <- data.frame(
  Item = c("Hello\nWorld", "This\nis a cat"),
  Value = c(10, 100)
)
dt_lb %>%
  mutate_all(linebreak) %>%
  kable("latex", booktabs = T, escape = F,
    col.names = linebreak(c("Item\n(Name)", "Value\n(Number)"), align = "c")) %>%
 row_spec(0, angle = 90)
```

答案 2 :(得分:0)

缺少Latex软件包的makecell。

Hao指出,本手册的第3页列出了一些必要的Latex软件包:haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf