| 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 | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 15 | #' `basic`, `striped`, `hold_position`, `scale_down`. `striped` will add | 
 | 16 | #' alternative row colors to the table. It will imports `LaTeX` package `xcolor` | 
 | 17 | #' if enabled. `hold_position` will "hold" the floating table to the exact | 
 | 18 | #' position. It is useful when the `LaTeX` table is contained in a `table` | 
 | 19 | #' environment after you specified captions in `kable()`. It will force the | 
 | 20 | #' table to stay in the position where it was created in the document. | 
 | 21 | #' `scale_down` is useful for super wide table. It will automatically adjust | 
 | 22 | #' the table to page width. | 
| Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame] | 23 | #' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML | 
| Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 24 | #' table should have 100\% width. Since HTML and pdf have different flavors on | 
 | 25 | #' the preferable format for `full_width`. If not specified, a HTML table will | 
 | 26 | #' have full width by default but this option will be set to `FALSE` for a | 
 | 27 | #' LaTeX table | 
 | 28 | #' @param position A character string determining how to position the table | 
 | 29 | #' on a page. Possible values include `left`, `center`, `right`, `float_left` | 
 | 30 | #' and `float_right`. Please see the package doc site for demonstrations. For | 
 | 31 | #' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be | 
 | 32 | #' imported. | 
| Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 33 | #' @param font_size A numeric input for table font size | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 34 | #' | 
 | 35 | #' @export | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 36 | kable_styling <- function(kable_input, | 
 | 37 |                           bootstrap_options = "basic", | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 38 |                           latex_options = "basic", | 
 | 39 |                           full_width = NULL, | 
| Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 40 |                           position = "center", | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 41 |                           font_size = NULL) { | 
| Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 42 |  | 
 | 43 |   if (length(bootstrap_options) == 1 && bootstrap_options == "basic") { | 
 | 44 |     bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic") | 
 | 45 |   } | 
 | 46 |   if (length(latex_options) == 1 && latex_options == "basic") { | 
 | 47 |     latex_options <- getOption("kable_styling_latex_options", "basic") | 
 | 48 |   } | 
 | 49 |   if (position == "center") { | 
 | 50 |     position <- getOption("kable_styling_position", "center") | 
 | 51 |   } | 
 | 52 |   position <- match.arg(position, | 
 | 53 |                         c("center", "left", "right", "float_left", "float_right")) | 
 | 54 |   if (is.null(font_size)) { | 
 | 55 |     font_size <- getOption("kable_styling_font_size", NULL) | 
 | 56 |   } | 
 | 57 |  | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 58 |   kable_format <- attr(kable_input, "format") | 
| Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 59 |  | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 60 |   if (!kable_format %in% c("html", "latex")) { | 
| guangchuang yu | 9c40539 | 2017-04-29 12:34:04 +0800 | [diff] [blame] | 61 |       message("Currently generic markdown table using pandoc is not supported.") | 
 | 62 |       return(kable_input) | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 63 |   } | 
 | 64 |   if (kable_format == "html") { | 
| Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 65 |     if (is.null(full_width)) { | 
 | 66 |       full_width <- getOption("kable_styling_full_width", T) | 
 | 67 |     } | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 68 |     return(htmlTable_styling(kable_input, | 
 | 69 |                              bootstrap_options = bootstrap_options, | 
 | 70 |                              full_width = full_width, | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 71 |                              position = position, | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 72 |                              font_size = font_size)) | 
 | 73 |   } | 
 | 74 |   if (kable_format == "latex") { | 
| 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", F) | 
 | 77 |     } | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 78 |     return(pdfTable_styling(kable_input, | 
 | 79 |                             latex_options = latex_options, | 
 | 80 |                             full_width = full_width, | 
 | 81 |                             position = position, | 
 | 82 |                             font_size = font_size)) | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 83 |   } | 
 | 84 | } | 
 | 85 |  | 
 | 86 | # htmlTable Styling ------------ | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 87 | htmlTable_styling <- function(kable_input, | 
 | 88 |                               bootstrap_options = "basic", | 
 | 89 |                               full_width = T, | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 90 |                               position = c("center", "left", "right", | 
 | 91 |                                            "float_left", "float_right"), | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 92 |                               font_size = NULL) { | 
| Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 93 |   table_info <- magic_mirror(kable_input) | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 94 |   kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT")) | 
 | 95 |  | 
 | 96 |   # Modify class | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 97 |   bootstrap_options <- match.arg( | 
 | 98 |     bootstrap_options, | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 99 |     c("basic", "striped", "bordered", "hover", "condensed", "responsive"), | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 100 |     several.ok = T | 
 | 101 |   ) | 
 | 102 |  | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 103 |   kable_xml_class <- NULL | 
 | 104 |   if (xml_has_attr(kable_xml, "class")) { | 
 | 105 |     kable_xml_class <- xml_attr(kable_xml, "class") | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 106 |   } | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 107 |   if (length(bootstrap_options) == 1 && bootstrap_options == "basic") { | 
 | 108 |     bootstrap_options <- "table" | 
 | 109 |   } else { | 
 | 110 |     bootstrap_options <- bootstrap_options[bootstrap_options != "basic"] | 
 | 111 |     bootstrap_options <- paste0("table-", bootstrap_options) | 
 | 112 |     bootstrap_options <- c("table", bootstrap_options) | 
 | 113 |   } | 
 | 114 |   xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options), | 
 | 115 |                                         collapse = " ") | 
| 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 |   # Modify style | 
 | 118 |   kable_xml_style <- NULL | 
 | 119 |   if (xml_has_attr(kable_xml, "style")) { | 
 | 120 |     kable_xml_style <- xml_attr(kable_xml, "style") | 
 | 121 |   } | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 122 |   if (!is.null(font_size)) { | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 123 |     kable_xml_style <- c(kable_xml_style, | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 124 |                          paste0("font-size: ", font_size, "px;")) | 
| Hao Zhu | fc14c9b | 2017-05-22 14:03:22 -0400 | [diff] [blame] | 125 |     kable_caption_node <- xml_tpart(kable_xml, "caption") | 
 | 126 |     if (!is.null(kable_caption_node)) { | 
 | 127 |       xml_attr(kable_caption_node, "style") <- "font-size: initial !important;" | 
 | 128 |     } | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 129 |   } | 
 | 130 |   if (!full_width) { | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 131 |     kable_xml_style <- c(kable_xml_style, "width: auto !important;") | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 132 |   } | 
| Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 133 |  | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 134 |   position <- match.arg(position) | 
 | 135 |   position_style <- switch( | 
 | 136 |     position, | 
 | 137 |     center = "margin-left: auto; margin-right: auto;", | 
 | 138 |     left = "text-align: right;", | 
 | 139 |     right = "margin-right: 0; margin-left: auto", | 
 | 140 |     float_left = "float: left; margin-right: 10px;", | 
 | 141 |     float_right = "float: right; margin-left: 10px;" | 
 | 142 |   ) | 
 | 143 |   kable_xml_style <- c(kable_xml_style, position_style) | 
 | 144 |  | 
| Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 145 |   if (length(kable_xml_style) != 0) { | 
 | 146 |     xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ") | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 147 |   } | 
| Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 148 |  | 
 | 149 |   out <- structure(as.character(kable_xml), format = "html", | 
 | 150 |                    class = "knitr_kable") | 
 | 151 |   attr(out, "original_kable_meta") <- table_info | 
 | 152 |   return(out) | 
| Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 153 | } | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 154 |  | 
 | 155 | # LaTeX table style | 
 | 156 | pdfTable_styling <- function(kable_input, | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 157 |                              latex_options = "basic", | 
 | 158 |                              full_width = F, | 
 | 159 |                              position = c("center", "left", "right", | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 160 |                                           "float_left", "float_right"), | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 161 |                              font_size = NULL) { | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 162 |  | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 163 |   latex_options <- match.arg( | 
 | 164 |     latex_options, | 
 | 165 |     c("basic", "striped", "hold_position", "scale_down"), | 
 | 166 |     several.ok = T | 
 | 167 |   ) | 
 | 168 |  | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 169 |   out <- NULL | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 170 |   out <- as.character(kable_input) | 
 | 171 |   table_info <- magic_mirror(kable_input) | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 172 |  | 
 | 173 |   if ("striped" %in% latex_options) { | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 174 |     out <- styling_latex_striped(out) | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 175 |   } | 
 | 176 |  | 
 | 177 |   # hold_position is only meaningful in a table environment | 
 | 178 |   if ("hold_position" %in% latex_options & table_info$table_env) { | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 179 |     out <- styling_latex_hold_position(out) | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 180 |   } | 
 | 181 |  | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 182 |   if ("scale_down" %in% latex_options) { | 
 | 183 |     out <- styling_latex_scale_down(out, table_info) | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 184 |   } | 
 | 185 |  | 
 | 186 |   if (full_width) { | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 187 |     out <- styling_latex_full_width(out, table_info) | 
 | 188 |   } | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 189 |  | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 190 |   if (!is.null(font_size)) { | 
 | 191 |     out <- styling_latex_font_size(out, table_info, font_size) | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 192 |   } | 
 | 193 |  | 
 | 194 |   position <- match.arg(position) | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 195 |   out <- styling_latex_position(out, table_info, position, latex_options) | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 196 |  | 
 | 197 |   out <- structure(out, format = "latex", class = "knitr_kable") | 
| Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 198 |   attr(out, "original_kable_meta") <- table_info | 
| Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 199 |   return(out) | 
| Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 200 | } | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 201 |  | 
 | 202 | styling_latex_striped <- function(x) { | 
 | 203 |   usepackage_latex("xcolor", "table") | 
 | 204 |   paste0( | 
 | 205 |     # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document | 
 | 206 |     "\\rowcolors{2}{gray!6}{white}\n", x, "\n\\rowcolors{2}{white}{white}") | 
 | 207 | } | 
 | 208 |  | 
 | 209 | styling_latex_hold_position <- function(x) { | 
 | 210 |   sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x) | 
 | 211 | } | 
 | 212 |  | 
 | 213 | styling_latex_scale_down <- function(x, table_info) { | 
 | 214 |   # You cannot put longtable in a resizebox | 
 | 215 |   # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised | 
 | 216 |   if (table_info$tabular == "longtable") { | 
 | 217 |     warning("Longtable cannot be resized.") | 
 | 218 |     return(x) | 
 | 219 |   } | 
 | 220 |   x <- sub(table_info$begin_tabular, | 
| Hao Zhu | cc21dc7 | 2017-05-20 01:15:25 -0400 | [diff] [blame] | 221 |            paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{", | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 222 |                   table_info$begin_tabular), | 
 | 223 |            x) | 
 | 224 |   sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x) | 
 | 225 | } | 
 | 226 |  | 
 | 227 | styling_latex_full_width <- function(x, table_info) { | 
 | 228 |   size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar) | 
 | 229 |   col_max_length <- apply(size_matrix, 1, max) + 4 | 
 | 230 |   col_ratio <- round(col_max_length / sum(col_max_length), 2) | 
 | 231 |   col_align <- paste0("p{\\\\dimexpr", col_ratio, | 
| Hao Zhu | 6a07646 | 2017-03-01 12:59:01 -0500 | [diff] [blame] | 232 |                       "\\\\columnwidth-2\\\\tabcolsep}") | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 233 |   col_align <- paste0("{", paste(col_align, collapse = ""), "}") | 
 | 234 |   x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"), | 
 | 235 |            table_info$begin_tabular, x) | 
 | 236 |   sub(table_info$begin_tabular, | 
 | 237 |       paste0(table_info$begin_tabular, col_align), x) | 
 | 238 | } | 
 | 239 |  | 
 | 240 | styling_latex_position <- function(x, table_info, position, latex_options) { | 
 | 241 |   hold_position <- "hold_position" %in% latex_options | 
 | 242 |   switch( | 
 | 243 |     position, | 
 | 244 |     center = styling_latex_position_center(x, table_info, hold_position), | 
 | 245 |     left = styling_latex_position_left(x, table_info), | 
 | 246 |     right = styling_latex_position_right(x, table_info, hold_position), | 
 | 247 |     float_left = styling_latex_position_float(x, table_info, "l"), | 
 | 248 |     float_right = styling_latex_position_float(x, table_info, "r") | 
 | 249 |   ) | 
 | 250 | } | 
 | 251 |  | 
 | 252 | styling_latex_position_center <- function(x, table_info, hold_position) { | 
 | 253 |   if (!table_info$table_env & table_info$tabular == "tabular") { | 
| Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 254 |     return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}")) | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 255 |   } | 
 | 256 |   return(x) | 
 | 257 | } | 
 | 258 |  | 
 | 259 | styling_latex_position_left <- function(x, table_info) { | 
 | 260 |   if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x)) | 
 | 261 |   longtable_option <- "\\[l\\]" | 
 | 262 |   sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2), | 
 | 263 |       paste0("\\\\begin\\{longtable\\}", longtable_option), x) | 
 | 264 | } | 
 | 265 |  | 
 | 266 | styling_latex_position_right <- function(x, table_info, hold_position) { | 
 | 267 |   warning("Position = right is only supported for longtable in LaTeX. ", | 
 | 268 |           "Setting back to center...") | 
 | 269 |   styling_latex_position_center(x, table_info, hold_position) | 
 | 270 | } | 
 | 271 |  | 
 | 272 | styling_latex_position_float <- function(x, table_info, option) { | 
 | 273 |   if (table_info$tabular == "longtable") { | 
 | 274 |     warning("wraptable is not supported for longtable.") | 
 | 275 |     if (option == "l") return(styling_latex_position_left(x, table_info)) | 
 | 276 |     if (option == "r") return(styling_latex_position_right(x, table_info, F)) | 
 | 277 |   } | 
| Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 278 |   usepackage_latex("wrapfig") | 
 | 279 |   size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar) | 
 | 280 |   col_max_length <- apply(size_matrix, 1, max) + 4 | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 281 |   if (table_info$table_env) { | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 282 |     option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option) | 
 | 283 |     option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}") | 
 | 284 |     x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x) | 
 | 285 |     x <- sub("\\\\begin\\{table\\}", option, x) | 
 | 286 |     x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x) | 
| Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 287 |   } else { | 
 | 288 |     option <- sprintf("\\begin{wraptable}{%s}", option) | 
 | 289 |     option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}") | 
 | 290 |     x <- paste0(option, x, "\\end{wraptable}") | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 291 |   } | 
| Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 292 |   return(x) | 
| Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 293 | } | 
 | 294 |  | 
 | 295 | styling_latex_font_size <- function(x, table_info, font_size) { | 
 | 296 |   row_height <- font_size + 2 | 
 | 297 |   if (table_info$tabular == "tabular" & table_info$table_env) { | 
 | 298 |     return(sub(table_info$begin_tabular, | 
 | 299 |                paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height, | 
 | 300 |                       "\\}\\\\selectfont\n", table_info$begin_tabular), | 
 | 301 |                x)) | 
 | 302 |   } | 
 | 303 |   # For longtable and tabular without table environment. Simple wrap around | 
 | 304 |   # fontsize is good enough | 
 | 305 |   return(paste0( | 
 | 306 |     "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x, | 
 | 307 |     "\\endgroup" | 
 | 308 |   )) | 
 | 309 | } |