Hao Zhu | 248bbef | 2018-04-02 18:25:14 -0400 | [diff] [blame] | 1 | #' Make linebreak in LaTeX Table cells |
| 2 | #' |
Hao Zhu | 5e4dd50 | 2018-04-05 12:01:58 -0400 | [diff] [blame] | 3 | #' @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 Zhu | 248bbef | 2018-04-02 18:25:14 -0400 | [diff] [blame] | 11 | #' @export |
| 12 | linebreak <- 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 Zhu | 5e4dd50 | 2018-04-05 12:01:58 -0400 | [diff] [blame] | 27 | |
| 28 | linebreak_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 | } |