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