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