add linebreak
diff --git a/NAMESPACE b/NAMESPACE
index 24da701..795899e 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -19,6 +19,7 @@
export(kable_as_image)
export(kable_styling)
export(landscape)
+export(linebreak)
export(magic_mirror)
export(rmd_format)
export(row_spec)
@@ -60,6 +61,7 @@
importFrom(stringr,str_split)
importFrom(stringr,str_sub)
importFrom(stringr,str_trim)
+importFrom(utils,head)
importFrom(utils,read.csv)
importFrom(viridisLite,viridis)
importFrom(xml2,"xml_attr<-")
diff --git a/R/add_header_above.R b/R/add_header_above.R
index b2be297..f5d857e 100644
--- a/R/add_header_above.R
+++ b/R/add_header_above.R
@@ -125,9 +125,13 @@
bold, italic, monospace, escape, line) {
table_info <- magic_mirror(kable_input)
header <- standardize_header_input(header)
+
if (escape) {
header$header <- escape_latex2(header$header)
+ header$header <- linebreak(header$header, align = "c", double_escape = TRUE)
}
+
+
hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule")
new_header_split <- pdfTable_new_header_generator(header, table_info$booktabs,
bold, italic, monospace)
diff --git a/R/kableExtra-package.R b/R/kableExtra-package.R
index c556076..7b4b72b 100644
--- a/R/kableExtra-package.R
+++ b/R/kableExtra-package.R
@@ -65,7 +65,7 @@
#' @importFrom rmarkdown latex_dependency html_dependency_bootstrap
#' html_dependency_jquery
#' @importFrom magrittr %>%
-#' @importFrom utils read.csv
+#' @importFrom utils read.csv head
#' @importFrom readr read_lines read_file
#' @importFrom scales rescale
#' @importFrom viridisLite viridis
diff --git a/R/linebreak.R b/R/linebreak.R
new file mode 100644
index 0000000..adf5e95
--- /dev/null
+++ b/R/linebreak.R
@@ -0,0 +1,18 @@
+#' Make linebreak in LaTeX Table cells
+#'
+#' @export
+linebreak <- function(x, align = c("l", "c", "r"), double_escape = F) {
+ if (is.numeric(x) | is.logical(x)) return(x)
+ align <- match.arg(align, c("l", "c", "r"))
+ if (double_escape) {
+ ifelse(str_detect(x, "\n"),
+ paste0("\\\\makecell[", align, "]{",
+ str_replace_all(x, "\n", "\\\\\\\\\\\\\\\\"), "}"),
+ x)
+ } else {
+ ifelse(str_detect(x, "\n"),
+ paste0("\\makecell[", align, "]{",
+ str_replace_all(x, "\n", "\\\\\\\\"), "}"),
+ x)
+ }
+}
diff --git a/R/zzz.R b/R/zzz.R
index a444d34..02f4487 100644
--- a/R/zzz.R
+++ b/R/zzz.R
@@ -14,5 +14,6 @@
usepackage_latex("threeparttable")
usepackage_latex("threeparttablex")
usepackage_latex("ulem", "normalem")
+ usepackage_latex("makecell")
}
}
diff --git a/docs/awesome_table_in_pdf.pdf b/docs/awesome_table_in_pdf.pdf
index 7b4bb42..0ef0e0b 100644
--- a/docs/awesome_table_in_pdf.pdf
+++ b/docs/awesome_table_in_pdf.pdf
Binary files differ
diff --git a/docs/best_practice_for_newline_in_latex_table.Rmd b/docs/best_practice_for_newline_in_latex_table.Rmd
new file mode 100644
index 0000000..52c3ea8
--- /dev/null
+++ b/docs/best_practice_for_newline_in_latex_table.Rmd
@@ -0,0 +1,61 @@
+---
+title: "Best Practice for newline in LaTeX table"
+author: "Hao"
+date: "`r Sys.Date()`"
+output: pdf_document
+---
+
+Since many people have asked me this question ([#157](https://github.com/haozhu233/kableExtra/issues/157), this [SO question](https://stackoverflow.com/questions/49546778/inserting-new-lines-into-kable-headers/49616173#49616173), etc.), I feel like I should document it out. :)
+
+Wrapping texts and make newlines may seem to be the same but they are actaully quite different. Thinking about when you "wrap texts" in your text editor, you have a fixed width window and the texts will be automatically wrapped. It's like a passive skill (in games :P). However, when you are trying to make newlines, you are inserting the linebreak by yourself and it's mostly like an active skill you need to cast. For these two tasks, LaTeX provides two totally different approaches.
+
+## Text wrapping
+If you are only trying stop your texts from "overflowing", you can get it done by setting a fixed width with `kableExtra::column_spec`. This is the most recommanded practice as it's fairly straightforward. The column width controls the width for both table header and table body.
+
+```{r, warning=F, message=F}
+library(kableExtra); library(tidyverse)
+dt <- tibble(
+ Items = c("Item 1", "Item 2", "Item 3"),
+ Text_1 = c("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. ","In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. ", "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. "),
+ Text_2 = c("Duis posuere placerat magna, ac aliquam lorem viverra non. Ut ultrices tempus eros, quis sodales libero commodo non. In non neque ut lacus vestibulum dictum a quis ipsum. ", "Aenean ut justo interdum, laoreet enim nec, viverra eros. Donec vel pharetra nunc. Suspendisse vel ipsum ac lectus semper aliquam ac a orci. Suspendisse libero mauris, egestas semper auctor sit amet, tempor et orci. ", "Phasellus quis neque aliquet, finibus nunc eget, lacinia neque. Sed auctor lectus vel ex scelerisque commodo. ")
+)
+```
+
+```{r}
+kable(dt, "latex", booktabs = T,
+ col.names = c("Item", "Short Title", "Very Very Very Very Very Very Long Title")) %>%
+ column_spec(2:3, width = "5cm")
+```
+
+## Insert linebreak in table
+In LaTeX, to make linebreaks in table cells, people usually use the `makecell` package. `kableExtra` 0.8.0 comes with a function called `linebreak` to facilitate that. Basically, this function will scan the existance of `\n`. If `\n` exists, it will put the texts in a `makecell` statement in a proper format. It works in a very similar way with `cell_spec`.
+
+```{r}
+linebreak("a\nb")
+```
+
+When you have `\n` in your data frame, you can either change the value manually or simply use it with `dplyr::mutate_all`.
+```{r}
+dt2 <- data.frame(
+ Item = c("Hello\nWorld", "This\nis a cat"),
+ Value = c(10, 100)
+)
+
+dt2 %>%
+ mutate_all(linebreak) %>%
+ kable("latex", booktabs = T, escape = F,
+ col.names = linebreak(c("Item\n(Name)", "Value\n(Number)"), align = "c"))
+```
+
+### Linebreak in kableExtra functions
+If you have a need to put a linebreak in `kableExtra` functions such as `add_header_above` and `group_rows`, just go ahead and use `\n` and it will be automatically converted. Note that this feature is controlled by the `escape` option in those functions.
+
+```{r}
+dt2 %>%
+ mutate_all(linebreak) %>%
+ kable("latex", booktabs = T, escape = F,
+ col.names = linebreak(c("Item\n(Name)", "Value\n(Number)"), align = "c")) %>%
+ add_header_above(c("Combined\nTitle" = 2))
+
+```
+
diff --git a/docs/best_practice_for_newline_in_latex_table.pdf b/docs/best_practice_for_newline_in_latex_table.pdf
new file mode 100644
index 0000000..518fa66
--- /dev/null
+++ b/docs/best_practice_for_newline_in_latex_table.pdf
Binary files differ
diff --git a/man/collapse_rows.Rd b/man/collapse_rows.Rd
index 677214a..49f9833 100644
--- a/man/collapse_rows.Rd
+++ b/man/collapse_rows.Rd
@@ -5,7 +5,9 @@
\title{Collapse repeated rows to multirow cell}
\usage{
collapse_rows(kable_input, columns = NULL, latex_hline = c("full", "major",
- "none"))
+ "none", "custom"), row_group_label_position = c("identity", "stack"),
+ custom_latex_hline = NULL, row_group_label_fonts = NULL,
+ headers_to_remove = NULL)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -13,7 +15,20 @@
\item{columns}{Numeric column positions where rows need to be collapsed.}
\item{latex_hline}{Option controlling the behavior of adding hlines to table.
-Choose from \code{full}, \code{major}, \code{none}.}
+Choose from \code{full}, \code{major}, \code{none}, \code{custom}.}
+
+\item{row_group_label_position}{Option controlling positions of row group
+labels. Choose from \code{identity}, \code{stack}.}
+
+\item{custom_latex_hline}{Numeric column positions whose collapsed rows will
+be separated by hlines.}
+
+\item{row_group_label_fonts}{A list of arguments that can be supplied to
+group_rows function to format the row group label when
+\code{row_group_label_position} is \code{stack}}
+
+\item{headers_to_remove}{Numeric column positions where headers should be
+removed when they are stacked.}
}
\description{
Collapse same values in columns into multirow cells. This
diff --git a/man/linebreak.Rd b/man/linebreak.Rd
new file mode 100644
index 0000000..7cb9d0c
--- /dev/null
+++ b/man/linebreak.Rd
@@ -0,0 +1,11 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/linebreak.R
+\name{linebreak}
+\alias{linebreak}
+\title{Make linebreak in LaTeX Table cells}
+\usage{
+linebreak(x, align = c("l", "c", "r"), double_escape = F)
+}
+\description{
+Make linebreak in LaTeX Table cells
+}