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