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 | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 36 | #' @param ... extra options for HTML or LaTeX |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 37 | #' |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 38 | #' @examples x_html <- knitr::kable(head(mtcars), "html") |
| 39 | #' kable_styling(x_html, "striped", position = "left", font_size = 7) |
| 40 | #' |
| 41 | #' x_latex <- knitr::kable(head(mtcars), "latex") |
| 42 | #' kable_styling(x_latex, latex_options = "striped", position = "float_left") |
| 43 | #' |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 44 | #' @export |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 45 | kable_styling <- function(kable_input, |
| 46 | bootstrap_options = "basic", |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 47 | latex_options = "basic", |
| 48 | full_width = NULL, |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 49 | position = "center", |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 50 | font_size = NULL, |
| 51 | ...) { |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 52 | |
| 53 | if (length(bootstrap_options) == 1 && bootstrap_options == "basic") { |
| 54 | bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic") |
| 55 | } |
| 56 | if (length(latex_options) == 1 && latex_options == "basic") { |
| 57 | latex_options <- getOption("kable_styling_latex_options", "basic") |
| 58 | } |
| 59 | if (position == "center") { |
| 60 | position <- getOption("kable_styling_position", "center") |
| 61 | } |
| 62 | position <- match.arg(position, |
| 63 | c("center", "left", "right", "float_left", "float_right")) |
| 64 | if (is.null(font_size)) { |
| 65 | font_size <- getOption("kable_styling_font_size", NULL) |
| 66 | } |
| 67 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 68 | kable_format <- attr(kable_input, "format") |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 69 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 70 | if (!kable_format %in% c("html", "latex")) { |
guangchuang yu | 9c40539 | 2017-04-29 12:34:04 +0800 | [diff] [blame] | 71 | message("Currently generic markdown table using pandoc is not supported.") |
| 72 | return(kable_input) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 73 | } |
| 74 | if (kable_format == "html") { |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 75 | if (is.null(full_width)) { |
| 76 | full_width <- getOption("kable_styling_full_width", T) |
| 77 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 78 | return(htmlTable_styling(kable_input, |
| 79 | bootstrap_options = bootstrap_options, |
| 80 | full_width = full_width, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 81 | position = position, |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 82 | font_size = font_size, ...)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 83 | } |
| 84 | if (kable_format == "latex") { |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 85 | if (is.null(full_width)) { |
| 86 | full_width <- getOption("kable_styling_full_width", F) |
| 87 | } |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 88 | return(pdfTable_styling(kable_input, |
| 89 | latex_options = latex_options, |
| 90 | full_width = full_width, |
| 91 | position = position, |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 92 | font_size = font_size, ...)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | # htmlTable Styling ------------ |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 97 | htmlTable_styling <- function(kable_input, |
| 98 | bootstrap_options = "basic", |
| 99 | full_width = T, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 100 | position = c("center", "left", "right", |
| 101 | "float_left", "float_right"), |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 102 | font_size = NULL) { |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 103 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 9bab153 | 2017-07-24 15:08:41 -0400 | [diff] [blame] | 104 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 105 | |
| 106 | # Modify class |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 107 | bootstrap_options <- match.arg( |
| 108 | bootstrap_options, |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 109 | c("basic", "striped", "bordered", "hover", "condensed", "responsive"), |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 110 | several.ok = T |
| 111 | ) |
| 112 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 113 | kable_xml_class <- NULL |
| 114 | if (xml_has_attr(kable_xml, "class")) { |
| 115 | kable_xml_class <- xml_attr(kable_xml, "class") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 116 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 117 | if (length(bootstrap_options) == 1 && bootstrap_options == "basic") { |
| 118 | bootstrap_options <- "table" |
| 119 | } else { |
| 120 | bootstrap_options <- bootstrap_options[bootstrap_options != "basic"] |
| 121 | bootstrap_options <- paste0("table-", bootstrap_options) |
| 122 | bootstrap_options <- c("table", bootstrap_options) |
| 123 | } |
| 124 | xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options), |
| 125 | collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 126 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 127 | # Modify style |
| 128 | kable_xml_style <- NULL |
| 129 | if (xml_has_attr(kable_xml, "style")) { |
| 130 | kable_xml_style <- xml_attr(kable_xml, "style") |
| 131 | } |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 132 | if (!is.null(font_size)) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 133 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 134 | paste0("font-size: ", font_size, "px;")) |
Hao Zhu | fc14c9b | 2017-05-22 14:03:22 -0400 | [diff] [blame] | 135 | kable_caption_node <- xml_tpart(kable_xml, "caption") |
| 136 | if (!is.null(kable_caption_node)) { |
| 137 | xml_attr(kable_caption_node, "style") <- "font-size: initial !important;" |
| 138 | } |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 139 | } |
| 140 | if (!full_width) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 141 | kable_xml_style <- c(kable_xml_style, "width: auto !important;") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 142 | } |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 143 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 144 | position <- match.arg(position) |
| 145 | position_style <- switch( |
| 146 | position, |
| 147 | center = "margin-left: auto; margin-right: auto;", |
| 148 | left = "text-align: right;", |
| 149 | right = "margin-right: 0; margin-left: auto", |
| 150 | float_left = "float: left; margin-right: 10px;", |
| 151 | float_right = "float: right; margin-left: 10px;" |
| 152 | ) |
| 153 | kable_xml_style <- c(kable_xml_style, position_style) |
| 154 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 155 | if (length(kable_xml_style) != 0) { |
| 156 | xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 157 | } |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 158 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 159 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 160 | attributes(out) <- kable_attrs |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 161 | return(out) |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 162 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 163 | |
| 164 | # LaTeX table style |
| 165 | pdfTable_styling <- function(kable_input, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 166 | latex_options = "basic", |
| 167 | full_width = F, |
| 168 | position = c("center", "left", "right", |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 169 | "float_left", "float_right"), |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 170 | font_size = NULL, |
Hao Zhu | 838f6ed | 2017-06-08 17:22:45 -0400 | [diff] [blame] | 171 | repeat_header_text = "\\textit{(continued)}") { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 172 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 173 | latex_options <- match.arg( |
| 174 | latex_options, |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 175 | c("basic", "striped", "hold_position", "scale_down", "repeat_header"), |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 176 | several.ok = T |
| 177 | ) |
| 178 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 179 | out <- NULL |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 180 | out <- as.character(kable_input) |
| 181 | table_info <- magic_mirror(kable_input) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 182 | |
| 183 | if ("striped" %in% latex_options) { |
Hao Zhu | 7d4a3e3 | 2017-06-08 21:29:13 -0400 | [diff] [blame] | 184 | out <- styling_latex_striped(out, table_info) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | # hold_position is only meaningful in a table environment |
| 188 | if ("hold_position" %in% latex_options & table_info$table_env) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 189 | out <- styling_latex_hold_position(out) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 192 | if ("scale_down" %in% latex_options) { |
| 193 | out <- styling_latex_scale_down(out, table_info) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 196 | if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") { |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 197 | out <- styling_latex_repeat_header(out, table_info, repeat_header_text) |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 200 | if (full_width) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 201 | out <- styling_latex_full_width(out, table_info) |
| 202 | } |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 203 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 204 | if (!is.null(font_size)) { |
| 205 | out <- styling_latex_font_size(out, table_info, font_size) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | position <- match.arg(position) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 209 | out <- styling_latex_position(out, table_info, position, latex_options) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 210 | |
| 211 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 212 | attr(out, "kable_meta") <- table_info |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 213 | return(out) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 214 | } |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 215 | |
Hao Zhu | 7d4a3e3 | 2017-06-08 21:29:13 -0400 | [diff] [blame] | 216 | styling_latex_striped <- function(x, table_info) { |
Hao Zhu | 7d4a3e3 | 2017-06-08 21:29:13 -0400 | [diff] [blame] | 217 | # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document |
| 218 | if (table_info$tabular == "longtable" & !is.na(table_info$caption)) { |
| 219 | row_color <- "\\rowcolors{2}{white}{gray!6}\n" |
| 220 | } else { |
| 221 | row_color <- "\\rowcolors{2}{gray!6}{white}\n" |
| 222 | } |
| 223 | return(paste0(row_color, x, "\n\\rowcolors{2}{white}{white}")) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | styling_latex_hold_position <- function(x) { |
| 227 | sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x) |
| 228 | } |
| 229 | |
| 230 | styling_latex_scale_down <- function(x, table_info) { |
| 231 | # You cannot put longtable in a resizebox |
| 232 | # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised |
| 233 | if (table_info$tabular == "longtable") { |
| 234 | warning("Longtable cannot be resized.") |
| 235 | return(x) |
| 236 | } |
| 237 | x <- sub(table_info$begin_tabular, |
Hao Zhu | cc21dc7 | 2017-05-20 01:15:25 -0400 | [diff] [blame] | 238 | paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{", |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 239 | table_info$begin_tabular), |
| 240 | x) |
| 241 | sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x) |
| 242 | } |
| 243 | |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 244 | styling_latex_repeat_header <- function(x, table_info, repeat_header_text) { |
| 245 | x <- read_lines(x) |
| 246 | if (table_info$booktabs) { |
| 247 | header_rows_start <- which(x == "\\toprule")[1] |
| 248 | header_rows_end <- which(x == "\\midrule")[1] |
| 249 | } else { |
| 250 | header_rows_start <- which(x == "\\hline")[1] |
| 251 | header_rows_end <- which(x == "\\hline")[2] |
| 252 | } |
Hao Zhu | fdec184 | 2017-06-08 17:06:04 -0400 | [diff] [blame] | 253 | |
| 254 | if (is.na(table_info$caption)) { |
| 255 | continue_line <- paste0( |
| 256 | "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text, |
| 257 | "}\\\\" |
| 258 | ) |
| 259 | } else { |
| 260 | continue_line <- paste0( |
| 261 | "\\caption{", table_info$caption, " ", repeat_header_text, |
| 262 | "}\\\\" |
| 263 | ) |
| 264 | } |
| 265 | |
Hao Zhu | a31e97f | 2017-06-08 14:55:41 -0400 | [diff] [blame] | 266 | x <- c( |
| 267 | x[1:header_rows_end], |
| 268 | "\\endfirsthead", |
| 269 | continue_line, |
| 270 | x[header_rows_start:header_rows_end], |
| 271 | "\\endhead", |
| 272 | x[(header_rows_end + 1):length(x)] |
| 273 | ) |
| 274 | x <- paste0(x, collapse = "\n") |
| 275 | return(x) |
Hao Zhu | 971d89f | 2017-06-07 19:22:47 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 278 | styling_latex_full_width <- function(x, table_info) { |
| 279 | size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar) |
| 280 | col_max_length <- apply(size_matrix, 1, max) + 4 |
| 281 | col_ratio <- round(col_max_length / sum(col_max_length), 2) |
| 282 | col_align <- paste0("p{\\\\dimexpr", col_ratio, |
Hao Zhu | 6a07646 | 2017-03-01 12:59:01 -0500 | [diff] [blame] | 283 | "\\\\columnwidth-2\\\\tabcolsep}") |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 284 | col_align <- paste0("{", paste(col_align, collapse = ""), "}") |
| 285 | x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"), |
| 286 | table_info$begin_tabular, x) |
| 287 | sub(table_info$begin_tabular, |
| 288 | paste0(table_info$begin_tabular, col_align), x) |
| 289 | } |
| 290 | |
| 291 | styling_latex_position <- function(x, table_info, position, latex_options) { |
| 292 | hold_position <- "hold_position" %in% latex_options |
| 293 | switch( |
| 294 | position, |
| 295 | center = styling_latex_position_center(x, table_info, hold_position), |
| 296 | left = styling_latex_position_left(x, table_info), |
| 297 | right = styling_latex_position_right(x, table_info, hold_position), |
| 298 | float_left = styling_latex_position_float(x, table_info, "l"), |
| 299 | float_right = styling_latex_position_float(x, table_info, "r") |
| 300 | ) |
| 301 | } |
| 302 | |
| 303 | styling_latex_position_center <- function(x, table_info, hold_position) { |
| 304 | if (!table_info$table_env & table_info$tabular == "tabular") { |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 305 | return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}")) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 306 | } |
| 307 | return(x) |
| 308 | } |
| 309 | |
| 310 | styling_latex_position_left <- function(x, table_info) { |
| 311 | if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x)) |
| 312 | longtable_option <- "\\[l\\]" |
| 313 | sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2), |
| 314 | paste0("\\\\begin\\{longtable\\}", longtable_option), x) |
| 315 | } |
| 316 | |
| 317 | styling_latex_position_right <- function(x, table_info, hold_position) { |
| 318 | warning("Position = right is only supported for longtable in LaTeX. ", |
| 319 | "Setting back to center...") |
| 320 | styling_latex_position_center(x, table_info, hold_position) |
| 321 | } |
| 322 | |
| 323 | styling_latex_position_float <- function(x, table_info, option) { |
| 324 | if (table_info$tabular == "longtable") { |
| 325 | warning("wraptable is not supported for longtable.") |
| 326 | if (option == "l") return(styling_latex_position_left(x, table_info)) |
| 327 | if (option == "r") return(styling_latex_position_right(x, table_info, F)) |
| 328 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 329 | size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar) |
| 330 | col_max_length <- apply(size_matrix, 1, max) + 4 |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 331 | if (table_info$table_env) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 332 | option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option) |
| 333 | option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}") |
| 334 | x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x) |
| 335 | x <- sub("\\\\begin\\{table\\}", option, x) |
| 336 | x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x) |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 337 | } else { |
| 338 | option <- sprintf("\\begin{wraptable}{%s}", option) |
| 339 | option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}") |
| 340 | x <- paste0(option, x, "\\end{wraptable}") |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 341 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 342 | return(x) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | styling_latex_font_size <- function(x, table_info, font_size) { |
| 346 | row_height <- font_size + 2 |
| 347 | if (table_info$tabular == "tabular" & table_info$table_env) { |
| 348 | return(sub(table_info$begin_tabular, |
| 349 | paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height, |
| 350 | "\\}\\\\selectfont\n", table_info$begin_tabular), |
| 351 | x)) |
| 352 | } |
| 353 | # For longtable and tabular without table environment. Simple wrap around |
| 354 | # fontsize is good enough |
| 355 | return(paste0( |
| 356 | "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x, |
| 357 | "\\endgroup" |
| 358 | )) |
| 359 | } |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 360 | |
| 361 | |