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