Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 1 | #' HTML table attributes |
| 2 | #' |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 3 | #' @description This function provides a cleaner approach to modify the style |
Hao Zhu | e7c8f70 | 2017-10-10 13:22:59 -0400 | [diff] [blame] | 4 | #' of HTML tables other than using the `table.attr` option in `knitr::kable()`. Note |
| 5 | #' that those bootstrap options requires Twitter bootstrap theme, which is not avaiable |
| 6 | #' in some customized template being loaded. |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 7 | #' |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 8 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
| 9 | #' @param bootstrap_options A character vector for bootstrap table options. |
Hao Zhu | 6a07646 | 2017-03-01 12:59:01 -0500 | [diff] [blame] | 10 | #' Please see package vignette or visit the w3schools' |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 11 | #' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page} |
| 12 | #' for more information. Possible options include `basic`, `striped`, |
| 13 | #' `bordered`, `hover`, `condensed` and `responsive`. |
| 14 | #' @param latex_options A character vector for LaTeX table options. Please see |
Hao Zhu | 6a07646 | 2017-03-01 12:59:01 -0500 | [diff] [blame] | 15 | #' package vignette for more information. Possible options include |
Rob Shepherd | c59d15e | 2017-08-26 16:06:36 +0100 | [diff] [blame] | 16 | #' `basic`, `striped`, `hold_position`, `HOLD_position`, `scale_down` & `repeat_header`. |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 17 | #' `striped` will add alternative row colors to the table. It will imports |
| 18 | #' `LaTeX` package `xcolor` if enabled. `hold_position` will "hold" the floating |
| 19 | #' table to the exact position. It is useful when the `LaTeX` table is contained |
| 20 | #' in a `table` environment after you specified captions in `kable()`. It will |
| 21 | #' force the table to stay in the position where it was created in the document. |
Hao Zhu | 53e240f | 2017-09-04 20:04:29 -0400 | [diff] [blame] | 22 | #' A stronger version: `HOLD_position` requires the `float` package and specifies `[H]`. |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 23 | #' `scale_down` is useful for super wide table. It will automatically adjust |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 24 | #' the table to page width. `repeat_header` in only meaningful in a longtable |
| 25 | #' environment. It will let the header row repeat on every page in that long |
| 26 | #' table. |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame] | 27 | #' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 28 | #' table should have 100\% width. Since HTML and pdf have different flavors on |
| 29 | #' the preferable format for `full_width`. If not specified, a HTML table will |
| 30 | #' have full width by default but this option will be set to `FALSE` for a |
| 31 | #' LaTeX table |
| 32 | #' @param position A character string determining how to position the table |
| 33 | #' on a page. Possible values include `left`, `center`, `right`, `float_left` |
| 34 | #' and `float_right`. Please see the package doc site for demonstrations. For |
| 35 | #' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be |
| 36 | #' imported. |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 37 | #' @param font_size A numeric input for table font size |
Hao Zhu | e1be960 | 2017-08-17 15:44:31 -0400 | [diff] [blame] | 38 | #' @param ... extra options for HTML or LaTeX. See `details`. |
| 39 | #' |
| 40 | #' @details For LaTeX, extra options includes: |
| 41 | #' - `repeat_header_method` can either be `append`(default) or `replace` |
| 42 | #' - `repeat_header_text` is just a text string you want to append on or |
| 43 | #' replace the caption. |
Hao Zhu | 71a224f | 2017-08-18 11:06:22 -0400 | [diff] [blame] | 44 | #' - `stripe_color` allows users to pick a different color for their strip lines. |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 45 | #' - `latex_table_env` character string to define customized table environment |
| 46 | #' such as tabu or tabularx.You shouldn't expect all features could be |
| 47 | #' supported in self-defined environments. |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 48 | #' |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 49 | #' @examples x_html <- knitr::kable(head(mtcars), "html") |
| 50 | #' kable_styling(x_html, "striped", position = "left", font_size = 7) |
| 51 | #' |
| 52 | #' x_latex <- knitr::kable(head(mtcars), "latex") |
| 53 | #' kable_styling(x_latex, latex_options = "striped", position = "float_left") |
| 54 | #' |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 55 | #' @export |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 56 | kable_styling <- function(kable_input, |
| 57 | bootstrap_options = "basic", |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 58 | latex_options = "basic", |
| 59 | full_width = NULL, |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 60 | position = "center", |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 61 | font_size = NULL, |
| 62 | ...) { |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 63 | |
| 64 | if (length(bootstrap_options) == 1 && bootstrap_options == "basic") { |
| 65 | bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic") |
| 66 | } |
| 67 | if (length(latex_options) == 1 && latex_options == "basic") { |
| 68 | latex_options <- getOption("kable_styling_latex_options", "basic") |
| 69 | } |
| 70 | if (position == "center") { |
| 71 | position <- getOption("kable_styling_position", "center") |
| 72 | } |
| 73 | position <- match.arg(position, |
| 74 | c("center", "left", "right", "float_left", "float_right")) |
| 75 | if (is.null(font_size)) { |
| 76 | font_size <- getOption("kable_styling_font_size", NULL) |
| 77 | } |
| 78 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 79 | kable_format <- attr(kable_input, "format") |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 80 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 81 | if (!kable_format %in% c("html", "latex")) { |
guangchuang yu | 9c40539 | 2017-04-29 12:34:04 +0800 | [diff] [blame] | 82 | message("Currently generic markdown table using pandoc is not supported.") |
| 83 | return(kable_input) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 84 | } |
| 85 | if (kable_format == "html") { |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 86 | if (is.null(full_width)) { |
| 87 | full_width <- getOption("kable_styling_full_width", T) |
| 88 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 89 | return(htmlTable_styling(kable_input, |
| 90 | bootstrap_options = bootstrap_options, |
| 91 | full_width = full_width, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 92 | position = position, |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 93 | font_size = font_size, ...)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 94 | } |
| 95 | if (kable_format == "latex") { |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 96 | if (is.null(full_width)) { |
| 97 | full_width <- getOption("kable_styling_full_width", F) |
| 98 | } |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 99 | return(pdfTable_styling(kable_input, |
| 100 | latex_options = latex_options, |
| 101 | full_width = full_width, |
| 102 | position = position, |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 103 | font_size = font_size, ...)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | # htmlTable Styling ------------ |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 108 | htmlTable_styling <- function(kable_input, |
| 109 | bootstrap_options = "basic", |
| 110 | full_width = T, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 111 | position = c("center", "left", "right", |
| 112 | "float_left", "float_right"), |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 113 | font_size = NULL) { |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 114 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 9bab153 | 2017-07-24 15:08:41 -0400 | [diff] [blame] | 115 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 116 | |
| 117 | # Modify class |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 118 | bootstrap_options <- match.arg( |
| 119 | bootstrap_options, |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 120 | c("basic", "striped", "bordered", "hover", "condensed", "responsive"), |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 121 | several.ok = T |
| 122 | ) |
| 123 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 124 | kable_xml_class <- NULL |
| 125 | if (xml_has_attr(kable_xml, "class")) { |
| 126 | kable_xml_class <- xml_attr(kable_xml, "class") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 127 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 128 | if (length(bootstrap_options) == 1 && bootstrap_options == "basic") { |
| 129 | bootstrap_options <- "table" |
| 130 | } else { |
| 131 | bootstrap_options <- bootstrap_options[bootstrap_options != "basic"] |
| 132 | bootstrap_options <- paste0("table-", bootstrap_options) |
| 133 | bootstrap_options <- c("table", bootstrap_options) |
| 134 | } |
| 135 | xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options), |
| 136 | collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 137 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 138 | # Modify style |
| 139 | kable_xml_style <- NULL |
| 140 | if (xml_has_attr(kable_xml, "style")) { |
| 141 | kable_xml_style <- xml_attr(kable_xml, "style") |
| 142 | } |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 143 | if (!is.null(font_size)) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 144 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 145 | paste0("font-size: ", font_size, "px;")) |
Hao Zhu | fc14c9b | 2017-05-22 14:03:22 -0400 | [diff] [blame] | 146 | kable_caption_node <- xml_tpart(kable_xml, "caption") |
| 147 | if (!is.null(kable_caption_node)) { |
| 148 | xml_attr(kable_caption_node, "style") <- "font-size: initial !important;" |
| 149 | } |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 150 | } |
| 151 | if (!full_width) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 152 | kable_xml_style <- c(kable_xml_style, "width: auto !important;") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 153 | } |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 154 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 155 | position <- match.arg(position) |
| 156 | position_style <- switch( |
| 157 | position, |
| 158 | center = "margin-left: auto; margin-right: auto;", |
Hao Zhu | fd516ba | 2017-07-28 14:30:25 -0400 | [diff] [blame] | 159 | left = "", |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 160 | right = "margin-right: 0; margin-left: auto", |
| 161 | float_left = "float: left; margin-right: 10px;", |
| 162 | float_right = "float: right; margin-left: 10px;" |
| 163 | ) |
| 164 | kable_xml_style <- c(kable_xml_style, position_style) |
| 165 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 166 | if (length(kable_xml_style) != 0) { |
| 167 | xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 168 | } |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 169 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 170 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 171 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame^] | 172 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 173 | return(out) |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 174 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 175 | |
| 176 | # LaTeX table style |
| 177 | pdfTable_styling <- function(kable_input, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 178 | latex_options = "basic", |
| 179 | full_width = F, |
| 180 | position = c("center", "left", "right", |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 181 | "float_left", "float_right"), |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 182 | font_size = NULL, |
Hao Zhu | ca211db | 2017-07-25 00:06:43 -0400 | [diff] [blame] | 183 | repeat_header_text = "\\textit{(continued)}", |
Hao Zhu | e1be960 | 2017-08-17 15:44:31 -0400 | [diff] [blame] | 184 | repeat_header_method = c("append", "replace"), |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 185 | repeat_header_continued = FALSE, |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 186 | stripe_color = "gray!6", |
| 187 | latex_table_env = NULL) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 188 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 189 | latex_options <- match.arg( |
| 190 | latex_options, |
Rob Shepherd | c59d15e | 2017-08-26 16:06:36 +0100 | [diff] [blame] | 191 | c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"), |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 192 | several.ok = T |
| 193 | ) |
| 194 | |
Hao Zhu | ca211db | 2017-07-25 00:06:43 -0400 | [diff] [blame] | 195 | repeat_header_method <- match.arg(repeat_header_method) |
| 196 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 197 | out <- NULL |
Hao Zhu | d2c0f73 | 2017-08-26 10:40:14 -0400 | [diff] [blame] | 198 | out <- enc2utf8(as.character(kable_input)) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 199 | table_info <- magic_mirror(kable_input) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 200 | |
| 201 | if ("striped" %in% latex_options) { |
Hao Zhu | 71a224f | 2017-08-18 11:06:22 -0400 | [diff] [blame] | 202 | out <- styling_latex_striped(out, table_info, stripe_color) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | # hold_position is only meaningful in a table environment |
| 206 | if ("hold_position" %in% latex_options & table_info$table_env) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 207 | out <- styling_latex_hold_position(out) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Rob Shepherd | c59d15e | 2017-08-26 16:06:36 +0100 | [diff] [blame] | 210 | # HOLD_position is only meaningful in a table environment |
| 211 | if ("HOLD_position" %in% latex_options & table_info$table_env) { |
| 212 | out <- styling_latex_HOLD_position(out) |
| 213 | } |
| 214 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 215 | if ("scale_down" %in% latex_options) { |
| 216 | out <- styling_latex_scale_down(out, table_info) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 219 | if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") { |
Hao Zhu | ca211db | 2017-07-25 00:06:43 -0400 | [diff] [blame] | 220 | out <- styling_latex_repeat_header(out, table_info, repeat_header_text, |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 221 | repeat_header_method, repeat_header_continued) |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 224 | if (full_width) { |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 225 | latex_table_env <- ifelse(table_info$tabular == "longtable", |
| 226 | "longtabu", "tabu") |
| 227 | full_width_return <- styling_latex_full_width(out, table_info) |
| 228 | out <- full_width_return[[1]] |
| 229 | table_info$align_vector <- full_width_return[[2]] |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 230 | } |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 231 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 232 | if (!is.null(font_size)) { |
| 233 | out <- styling_latex_font_size(out, table_info, font_size) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 236 | if (!is.null(latex_table_env)) { |
| 237 | out <- styling_latex_table_env(out, table_info$tabular, latex_table_env) |
| 238 | table_info$tabular <- latex_table_env |
| 239 | table_info$begin_tabular <- sub("tabular", latex_table_env, |
| 240 | table_info$begin_tabular) |
| 241 | table_info$end_tabular <- sub("tabular", latex_table_env, |
| 242 | table_info$end_tabular) |
| 243 | } |
| 244 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 245 | position <- match.arg(position) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 246 | out <- styling_latex_position(out, table_info, position, latex_options) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 247 | |
| 248 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 249 | attr(out, "kable_meta") <- table_info |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 250 | return(out) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 251 | } |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 252 | |
Hao Zhu | e1be960 | 2017-08-17 15:44:31 -0400 | [diff] [blame] | 253 | styling_latex_striped <- function(x, table_info, color) { |
Hao Zhu | 7d4a3e3 | 2017-06-08 21:29:13 -0400 | [diff] [blame] | 254 | # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document |
| 255 | if (table_info$tabular == "longtable" & !is.na(table_info$caption)) { |
Hao Zhu | e1be960 | 2017-08-17 15:44:31 -0400 | [diff] [blame] | 256 | row_color <- sprintf("\\rowcolors{2}{white}{%s}", color) |
Hao Zhu | 7d4a3e3 | 2017-06-08 21:29:13 -0400 | [diff] [blame] | 257 | } else { |
Hao Zhu | e1be960 | 2017-08-17 15:44:31 -0400 | [diff] [blame] | 258 | row_color <- sprintf("\\rowcolors{2}{%s}{white}", color) |
Hao Zhu | 7d4a3e3 | 2017-06-08 21:29:13 -0400 | [diff] [blame] | 259 | } |
Hao Zhu | 00ba87c | 2017-08-01 12:42:58 -0400 | [diff] [blame] | 260 | |
| 261 | x <- read_lines(x) |
| 262 | if (table_info$booktabs) { |
| 263 | header_rows_start <- which(x == "\\toprule")[1] |
| 264 | header_rows_end <- which(x == "\\midrule")[1] |
| 265 | } else { |
| 266 | header_rows_start <- which(x == "\\hline")[1] |
| 267 | header_rows_end <- which(x == "\\hline")[2] |
| 268 | } |
| 269 | |
| 270 | x <- c( |
| 271 | row_color, |
| 272 | x[1:(header_rows_start - 1)], |
| 273 | "\\hiderowcolors", |
| 274 | x[header_rows_start:header_rows_end], |
| 275 | "\\showrowcolors", |
| 276 | x[(header_rows_end + 1):length(x)], |
| 277 | "\\rowcolors{2}{white}{white}" |
| 278 | ) |
| 279 | x <- paste0(x, collapse = "\n") |
| 280 | return(x) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | styling_latex_hold_position <- function(x) { |
| 284 | sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x) |
| 285 | } |
| 286 | |
Rob Shepherd | c59d15e | 2017-08-26 16:06:36 +0100 | [diff] [blame] | 287 | styling_latex_HOLD_position <- function(x) { |
| 288 | sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]", x) |
| 289 | } |
| 290 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 291 | styling_latex_scale_down <- function(x, table_info) { |
| 292 | # You cannot put longtable in a resizebox |
| 293 | # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised |
| 294 | if (table_info$tabular == "longtable") { |
| 295 | warning("Longtable cannot be resized.") |
| 296 | return(x) |
| 297 | } |
| 298 | x <- sub(table_info$begin_tabular, |
Hao Zhu | cc21dc7 | 2017-05-20 01:15:25 -0400 | [diff] [blame] | 299 | paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{", |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 300 | table_info$begin_tabular), |
| 301 | x) |
| 302 | sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x) |
| 303 | } |
| 304 | |
Hao Zhu | ca211db | 2017-07-25 00:06:43 -0400 | [diff] [blame] | 305 | styling_latex_repeat_header <- function(x, table_info, repeat_header_text, |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 306 | repeat_header_method, |
| 307 | repeat_header_continued) { |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 308 | x <- read_lines(x) |
| 309 | if (table_info$booktabs) { |
| 310 | header_rows_start <- which(x == "\\toprule")[1] |
| 311 | header_rows_end <- which(x == "\\midrule")[1] |
| 312 | } else { |
| 313 | header_rows_start <- which(x == "\\hline")[1] |
| 314 | header_rows_end <- which(x == "\\hline")[2] |
| 315 | } |
Hao Zhu | fdec184 | 2017-06-08 17:06:04 -0400 | [diff] [blame] | 316 | |
| 317 | if (is.na(table_info$caption)) { |
| 318 | continue_line <- paste0( |
| 319 | "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text, |
| 320 | "}\\\\" |
| 321 | ) |
| 322 | } else { |
Hao Zhu | ca211db | 2017-07-25 00:06:43 -0400 | [diff] [blame] | 323 | if (repeat_header_method == "append") { |
Hao Zhu | fd2ef9b | 2017-07-25 13:14:18 -0400 | [diff] [blame] | 324 | caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption) |
Hao Zhu | 6f95ea4 | 2017-07-25 13:12:12 -0400 | [diff] [blame] | 325 | repeat_header_text <- paste(caption_without_lab, repeat_header_text) |
Hao Zhu | ca211db | 2017-07-25 00:06:43 -0400 | [diff] [blame] | 326 | } |
Hao Zhu | 68d4fe9 | 2017-07-25 14:07:50 -0400 | [diff] [blame] | 327 | continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\") |
Hao Zhu | fdec184 | 2017-06-08 17:06:04 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 330 | if (!table_info$booktabs) { |
| 331 | bottom_part <- NULL |
| 332 | } else { |
| 333 | index_bottomrule <- which(x == "\\bottomrule") |
| 334 | x <- x[-index_bottomrule] |
Hao Zhu | 20784e7 | 2017-10-10 14:48:33 -0400 | [diff] [blame] | 335 | x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*") |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 336 | |
| 337 | if (repeat_header_continued == FALSE) { |
Hao Zhu | 44f6c49 | 2017-10-20 23:05:37 -0400 | [diff] [blame] | 338 | bottom_part <- "\\\n\\endfoot\n\\bottomrule\n\\endlastfoot" |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 339 | } else { |
| 340 | if (repeat_header_continued == TRUE) { |
Hao Zhu | b7f4f4e | 2017-10-02 14:58:41 -0400 | [diff] [blame] | 341 | bottom_text <- "\\textit{(continued \\ldots)}" |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 342 | } else { |
| 343 | bottom_text <- repeat_header_continued |
| 344 | } |
| 345 | bottom_part <- paste0( |
Hao Zhu | 44f6c49 | 2017-10-20 23:05:37 -0400 | [diff] [blame] | 346 | "\\midrule\n", |
Hao Zhu | b7f4f4e | 2017-10-02 14:58:41 -0400 | [diff] [blame] | 347 | "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n", |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 348 | "\\endfoot\n", |
Hao Zhu | c83eb63 | 2017-10-10 14:11:08 -0400 | [diff] [blame] | 349 | "\\bottomrule\n", |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 350 | "\\endlastfoot" |
| 351 | ) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule") |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 356 | x <- c( |
| 357 | x[1:header_rows_end], |
| 358 | "\\endfirsthead", |
| 359 | continue_line, |
| 360 | x[header_rows_start:header_rows_end], |
| 361 | "\\endhead", |
Hao Zhu | 6c9714a | 2017-10-02 14:52:45 -0400 | [diff] [blame] | 362 | bottom_part, |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 363 | x[(header_rows_end + 1):length(x)] |
| 364 | ) |
| 365 | x <- paste0(x, collapse = "\n") |
| 366 | return(x) |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 369 | styling_latex_full_width <- function(x, table_info) { |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 370 | col_align <- as.character(factor( |
| 371 | table_info$align_vector, c("c", "l", "r"), |
| 372 | c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X") |
| 373 | )) |
| 374 | col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)] |
| 375 | col_align_vector <- col_align |
| 376 | col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}") |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 377 | x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"), |
| 378 | table_info$begin_tabular, x) |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 379 | x <- sub(table_info$begin_tabular, |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 380 | paste0(table_info$begin_tabular, col_align), x) |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 381 | return(list(x, col_align_vector)) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | styling_latex_position <- function(x, table_info, position, latex_options) { |
newtux | d286706 | 2017-09-29 00:19:27 -0400 | [diff] [blame] | 385 | hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 386 | if (length(hold_position) == 0) hold_position <- "" |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 387 | switch( |
| 388 | position, |
| 389 | center = styling_latex_position_center(x, table_info, hold_position), |
| 390 | left = styling_latex_position_left(x, table_info), |
| 391 | right = styling_latex_position_right(x, table_info, hold_position), |
| 392 | float_left = styling_latex_position_float(x, table_info, "l"), |
| 393 | float_right = styling_latex_position_float(x, table_info, "r") |
| 394 | ) |
| 395 | } |
| 396 | |
| 397 | styling_latex_position_center <- function(x, table_info, hold_position) { |
| 398 | if (!table_info$table_env & table_info$tabular == "tabular") { |
newtux | d286706 | 2017-09-29 00:19:27 -0400 | [diff] [blame] | 399 | x <- paste0("\\begin{table}\n\\centering", x, "\n\\end{table}") |
| 400 | if (hold_position == "hold_position") { |
| 401 | x <- styling_latex_hold_position(x) |
| 402 | } else { |
| 403 | x <- styling_latex_HOLD_position(x) |
| 404 | } |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 405 | } |
| 406 | return(x) |
| 407 | } |
| 408 | |
| 409 | styling_latex_position_left <- function(x, table_info) { |
| 410 | if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x)) |
| 411 | longtable_option <- "\\[l\\]" |
| 412 | sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2), |
| 413 | paste0("\\\\begin\\{longtable\\}", longtable_option), x) |
| 414 | } |
| 415 | |
| 416 | styling_latex_position_right <- function(x, table_info, hold_position) { |
| 417 | warning("Position = right is only supported for longtable in LaTeX. ", |
| 418 | "Setting back to center...") |
| 419 | styling_latex_position_center(x, table_info, hold_position) |
| 420 | } |
| 421 | |
| 422 | styling_latex_position_float <- function(x, table_info, option) { |
| 423 | if (table_info$tabular == "longtable") { |
| 424 | warning("wraptable is not supported for longtable.") |
| 425 | if (option == "l") return(styling_latex_position_left(x, table_info)) |
| 426 | if (option == "r") return(styling_latex_position_right(x, table_info, F)) |
| 427 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 428 | size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar) |
| 429 | col_max_length <- apply(size_matrix, 1, max) + 4 |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 430 | if (table_info$table_env) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 431 | option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option) |
| 432 | option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}") |
| 433 | x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x) |
| 434 | x <- sub("\\\\begin\\{table\\}", option, x) |
| 435 | x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x) |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 436 | } else { |
| 437 | option <- sprintf("\\begin{wraptable}{%s}", option) |
| 438 | option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}") |
| 439 | x <- paste0(option, x, "\\end{wraptable}") |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 440 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 441 | return(x) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | styling_latex_font_size <- function(x, table_info, font_size) { |
| 445 | row_height <- font_size + 2 |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 446 | if (table_info$tabular != "longtable" & table_info$table_env) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 447 | return(sub(table_info$begin_tabular, |
| 448 | paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height, |
| 449 | "\\}\\\\selectfont\n", table_info$begin_tabular), |
| 450 | x)) |
| 451 | } |
| 452 | # For longtable and tabular without table environment. Simple wrap around |
| 453 | # fontsize is good enough |
| 454 | return(paste0( |
| 455 | "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 456 | "\\endgroup{}" |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 457 | )) |
| 458 | } |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 459 | |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 460 | styling_latex_table_env <- function(x, current_env, latex_table_env) { |
Hao Zhu | 3af9f94 | 2017-09-07 12:30:21 -0400 | [diff] [blame] | 461 | x <- sub( |
| 462 | paste0("begin\\{", current_env, "\\}\\[t\\]"), |
| 463 | paste0("begin\\{", latex_table_env, "\\}"), x |
| 464 | ) |
| 465 | x <- sub( |
| 466 | paste0("begin\\{", current_env, "\\}"), |
| 467 | paste0("begin\\{", latex_table_env, "\\}"), x |
| 468 | ) |
| 469 | x <- sub( |
| 470 | paste0("end\\{", current_env, "\\}"), |
| 471 | paste0("end\\{", latex_table_env, "\\}"), x |
| 472 | ) |
| 473 | return(x) |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 474 | } |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 475 | |