blob: df4ee91419c18f2d5300116276fb22de789dfc92 [file] [log] [blame]
Hao Zhu248bbef2018-04-02 18:25:14 -04001#' Make linebreak in LaTeX Table cells
2#'
Hao Zhu5e4dd502018-04-05 12:01:58 -04003#' @description This function generate LaTeX code of `makecell` so that users
4#' can have linebreaks in their table
5#'
6#' @param x A character vector
7#' @param align Choose from "l", "c" or "r"
8#' @param double_escape Whether special character should be double escaped.
9#' Default is FALSE.
10#'
Hao Zhu248bbef2018-04-02 18:25:14 -040011#' @export
12linebreak <- function(x, align = c("l", "c", "r"), double_escape = F) {
13 if (is.numeric(x) | is.logical(x)) return(x)
14 align <- match.arg(align, c("l", "c", "r"))
15 if (double_escape) {
16 ifelse(str_detect(x, "\n"),
17 paste0("\\\\makecell[", align, "]{",
18 str_replace_all(x, "\n", "\\\\\\\\\\\\\\\\"), "}"),
19 x)
20 } else {
21 ifelse(str_detect(x, "\n"),
22 paste0("\\makecell[", align, "]{",
23 str_replace_all(x, "\n", "\\\\\\\\"), "}"),
24 x)
25 }
26}
Hao Zhu5e4dd502018-04-05 12:01:58 -040027
28linebreak_html <- function(x) {
29 if (is.numeric(x) | is.logical(x)) return(x)
30 ifelse(str_detect(x, "\n"), str_replace_all(x, "\n", "<br />"), x)
31}