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