blob: 5e00bd3b416504d6f38af7804e3a4e0d999b6b83 [file] [log] [blame]
Hao Zhue10cfd32017-02-21 16:41:14 -05001#' HTML table attributes
2#'
Hao Zhu26234122017-02-22 15:34:33 -05003#' @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 Zhue10cfd32017-02-21 16:41:14 -05006#'
Hao Zhuf7994dd2017-02-27 16:58:42 -05007#' @param kable_input Output of `knitr::kable()` with `format` specified
8#' @param bootstrap_options A character vector for bootstrap table options.
Hao Zhu6a076462017-03-01 12:59:01 -05009#' Please see package vignette or visit the w3schools'
Hao Zhuf7994dd2017-02-27 16:58:42 -050010#' \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 Zhu6a076462017-03-01 12:59:01 -050014#' package vignette for more information. Possible options include
Rob Shepherdc59d15e2017-08-26 16:06:36 +010015#' `basic`, `striped`, `hold_position`, `HOLD_position`, `scale_down` & `repeat_header`.
Hao Zhu971d89f2017-06-07 19:22:47 -040016#' `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 Zhu53e240f2017-09-04 20:04:29 -040021#' A stronger version: `HOLD_position` requires the `float` package and specifies `[H]`.
Hao Zhuf7994dd2017-02-27 16:58:42 -050022#' `scale_down` is useful for super wide table. It will automatically adjust
Hao Zhu971d89f2017-06-07 19:22:47 -040023#' the table to page width. `repeat_header` in only meaningful in a longtable
24#' environment. It will let the header row repeat on every page in that long
25#' table.
Hao Zhu59f5fe02017-02-22 11:27:14 -050026#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhuf7994dd2017-02-27 16:58:42 -050027#' table should have 100\% width. Since HTML and pdf have different flavors on
28#' the preferable format for `full_width`. If not specified, a HTML table will
29#' have full width by default but this option will be set to `FALSE` for a
30#' LaTeX table
31#' @param position A character string determining how to position the table
32#' on a page. Possible values include `left`, `center`, `right`, `float_left`
33#' and `float_right`. Please see the package doc site for demonstrations. For
34#' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be
35#' imported.
Hao Zhu94956582017-02-21 18:18:29 -050036#' @param font_size A numeric input for table font size
Hao Zhue1be9602017-08-17 15:44:31 -040037#' @param ... extra options for HTML or LaTeX. See `details`.
38#'
39#' @details For LaTeX, extra options includes:
40#' - `repeat_header_method` can either be `append`(default) or `replace`
41#' - `repeat_header_text` is just a text string you want to append on or
42#' replace the caption.
Hao Zhu71a224f2017-08-18 11:06:22 -040043#' - `stripe_color` allows users to pick a different color for their strip lines.
Hao Zhu245931c2017-09-01 22:43:56 -040044#' - `latex_table_env` character string to define customized table environment
45#' such as tabu or tabularx.You shouldn't expect all features could be
46#' supported in self-defined environments.
Hao Zhue10cfd32017-02-21 16:41:14 -050047#'
Hao Zhu78e61222017-05-24 20:53:35 -040048#' @examples x_html <- knitr::kable(head(mtcars), "html")
49#' kable_styling(x_html, "striped", position = "left", font_size = 7)
50#'
51#' x_latex <- knitr::kable(head(mtcars), "latex")
52#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
53#'
Hao Zhue10cfd32017-02-21 16:41:14 -050054#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050055kable_styling <- function(kable_input,
56 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050057 latex_options = "basic",
58 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050059 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040060 font_size = NULL,
61 ...) {
Hao Zhu9b45a182017-02-27 18:17:46 -050062
63 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
64 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
65 }
66 if (length(latex_options) == 1 && latex_options == "basic") {
67 latex_options <- getOption("kable_styling_latex_options", "basic")
68 }
69 if (position == "center") {
70 position <- getOption("kable_styling_position", "center")
71 }
72 position <- match.arg(position,
73 c("center", "left", "right", "float_left", "float_right"))
74 if (is.null(font_size)) {
75 font_size <- getOption("kable_styling_font_size", NULL)
76 }
77
Hao Zhuc1f38412017-02-23 12:13:48 -050078 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050079
Hao Zhuc1f38412017-02-23 12:13:48 -050080 if (!kable_format %in% c("html", "latex")) {
guangchuang yu9c405392017-04-29 12:34:04 +080081 message("Currently generic markdown table using pandoc is not supported.")
82 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050083 }
84 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050085 if (is.null(full_width)) {
86 full_width <- getOption("kable_styling_full_width", T)
87 }
Hao Zhuc1f38412017-02-23 12:13:48 -050088 return(htmlTable_styling(kable_input,
89 bootstrap_options = bootstrap_options,
90 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050091 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -040092 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -050093 }
94 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -050095 if (is.null(full_width)) {
96 full_width <- getOption("kable_styling_full_width", F)
97 }
Hao Zhuc05e1812017-02-25 01:45:35 -050098 return(pdfTable_styling(kable_input,
99 latex_options = latex_options,
100 full_width = full_width,
101 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -0400102 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -0500103 }
104}
105
106# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500107htmlTable_styling <- function(kable_input,
108 bootstrap_options = "basic",
109 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500110 position = c("center", "left", "right",
111 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -0500112 font_size = NULL) {
Hao Zhu909ea382017-06-12 15:43:47 -0400113 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400114 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500115
116 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500117 bootstrap_options <- match.arg(
118 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500119 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500120 several.ok = T
121 )
122
Hao Zhu26234122017-02-22 15:34:33 -0500123 kable_xml_class <- NULL
124 if (xml_has_attr(kable_xml, "class")) {
125 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500126 }
Hao Zhu26234122017-02-22 15:34:33 -0500127 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
128 bootstrap_options <- "table"
129 } else {
130 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
131 bootstrap_options <- paste0("table-", bootstrap_options)
132 bootstrap_options <- c("table", bootstrap_options)
133 }
134 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
135 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500136
Hao Zhu26234122017-02-22 15:34:33 -0500137 # Modify style
138 kable_xml_style <- NULL
139 if (xml_has_attr(kable_xml, "style")) {
140 kable_xml_style <- xml_attr(kable_xml, "style")
141 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500142 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500143 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500144 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400145 kable_caption_node <- xml_tpart(kable_xml, "caption")
146 if (!is.null(kable_caption_node)) {
147 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
148 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500149 }
150 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500151 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500152 }
Hao Zhu94956582017-02-21 18:18:29 -0500153
Hao Zhuc05e1812017-02-25 01:45:35 -0500154 position <- match.arg(position)
155 position_style <- switch(
156 position,
157 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400158 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500159 right = "margin-right: 0; margin-left: auto",
160 float_left = "float: left; margin-right: 10px;",
161 float_right = "float: right; margin-left: 10px;"
162 )
163 kable_xml_style <- c(kable_xml_style, position_style)
164
Hao Zhu26234122017-02-22 15:34:33 -0500165 if (length(kable_xml_style) != 0) {
166 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500167 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500168
Hao Zhuf2dfd142017-07-24 14:43:28 -0400169 out <- as_kable_xml(kable_xml)
Hao Zhu909ea382017-06-12 15:43:47 -0400170 attributes(out) <- kable_attrs
Hao Zhu9b45a182017-02-27 18:17:46 -0500171 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500172}
Hao Zhuc1f38412017-02-23 12:13:48 -0500173
174# LaTeX table style
175pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500176 latex_options = "basic",
177 full_width = F,
178 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500179 "float_left", "float_right"),
Hao Zhua31e97f2017-06-08 14:55:41 -0400180 font_size = NULL,
Hao Zhuca211db2017-07-25 00:06:43 -0400181 repeat_header_text = "\\textit{(continued)}",
Hao Zhue1be9602017-08-17 15:44:31 -0400182 repeat_header_method = c("append", "replace"),
Hao Zhu245931c2017-09-01 22:43:56 -0400183 stripe_color = "gray!6",
184 latex_table_env = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500185
Hao Zhuc05e1812017-02-25 01:45:35 -0500186 latex_options <- match.arg(
187 latex_options,
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100188 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500189 several.ok = T
190 )
191
Hao Zhuca211db2017-07-25 00:06:43 -0400192 repeat_header_method <- match.arg(repeat_header_method)
193
Hao Zhua3fc0c42017-02-27 12:04:59 -0500194 out <- NULL
Hao Zhud2c0f732017-08-26 10:40:14 -0400195 out <- enc2utf8(as.character(kable_input))
Hao Zhuc05e1812017-02-25 01:45:35 -0500196 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500197
198 if ("striped" %in% latex_options) {
Hao Zhu71a224f2017-08-18 11:06:22 -0400199 out <- styling_latex_striped(out, table_info, stripe_color)
Hao Zhuc05e1812017-02-25 01:45:35 -0500200 }
201
202 # hold_position is only meaningful in a table environment
203 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500204 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500205 }
206
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100207 # HOLD_position is only meaningful in a table environment
208 if ("HOLD_position" %in% latex_options & table_info$table_env) {
209 out <- styling_latex_HOLD_position(out)
210 }
211
Hao Zhua3fc0c42017-02-27 12:04:59 -0500212 if ("scale_down" %in% latex_options) {
213 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500214 }
215
Hao Zhu971d89f2017-06-07 19:22:47 -0400216 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400217 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
218 repeat_header_method)
Hao Zhu971d89f2017-06-07 19:22:47 -0400219 }
220
Hao Zhuc05e1812017-02-25 01:45:35 -0500221 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400222 latex_table_env <- ifelse(table_info$tabular == "longtable",
223 "longtabu", "tabu")
224 full_width_return <- styling_latex_full_width(out, table_info)
225 out <- full_width_return[[1]]
226 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500227 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500228
Hao Zhua3fc0c42017-02-27 12:04:59 -0500229 if (!is.null(font_size)) {
230 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500231 }
232
Hao Zhu245931c2017-09-01 22:43:56 -0400233 if (!is.null(latex_table_env)) {
234 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
235 table_info$tabular <- latex_table_env
236 table_info$begin_tabular <- sub("tabular", latex_table_env,
237 table_info$begin_tabular)
238 table_info$end_tabular <- sub("tabular", latex_table_env,
239 table_info$end_tabular)
240 }
241
Hao Zhuc05e1812017-02-25 01:45:35 -0500242 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500243 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500244
245 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400246 attr(out, "kable_meta") <- table_info
Hao Zhuc05e1812017-02-25 01:45:35 -0500247 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500248}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500249
Hao Zhue1be9602017-08-17 15:44:31 -0400250styling_latex_striped <- function(x, table_info, color) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400251 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
252 if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
Hao Zhue1be9602017-08-17 15:44:31 -0400253 row_color <- sprintf("\\rowcolors{2}{white}{%s}", color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400254 } else {
Hao Zhue1be9602017-08-17 15:44:31 -0400255 row_color <- sprintf("\\rowcolors{2}{%s}{white}", color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400256 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400257
258 x <- read_lines(x)
259 if (table_info$booktabs) {
260 header_rows_start <- which(x == "\\toprule")[1]
261 header_rows_end <- which(x == "\\midrule")[1]
262 } else {
263 header_rows_start <- which(x == "\\hline")[1]
264 header_rows_end <- which(x == "\\hline")[2]
265 }
266
267 x <- c(
268 row_color,
269 x[1:(header_rows_start - 1)],
270 "\\hiderowcolors",
271 x[header_rows_start:header_rows_end],
272 "\\showrowcolors",
273 x[(header_rows_end + 1):length(x)],
274 "\\rowcolors{2}{white}{white}"
275 )
276 x <- paste0(x, collapse = "\n")
277 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500278}
279
280styling_latex_hold_position <- function(x) {
281 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
282}
283
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100284styling_latex_HOLD_position <- function(x) {
285 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]", x)
286}
287
Hao Zhua3fc0c42017-02-27 12:04:59 -0500288styling_latex_scale_down <- function(x, table_info) {
289 # You cannot put longtable in a resizebox
290 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
291 if (table_info$tabular == "longtable") {
292 warning("Longtable cannot be resized.")
293 return(x)
294 }
295 x <- sub(table_info$begin_tabular,
Hao Zhucc21dc72017-05-20 01:15:25 -0400296 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500297 table_info$begin_tabular),
298 x)
299 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
300}
301
Hao Zhuca211db2017-07-25 00:06:43 -0400302styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
303 repeat_header_method) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400304 x <- read_lines(x)
305 if (table_info$booktabs) {
306 header_rows_start <- which(x == "\\toprule")[1]
307 header_rows_end <- which(x == "\\midrule")[1]
308 } else {
309 header_rows_start <- which(x == "\\hline")[1]
310 header_rows_end <- which(x == "\\hline")[2]
311 }
Hao Zhufdec1842017-06-08 17:06:04 -0400312
313 if (is.na(table_info$caption)) {
314 continue_line <- paste0(
315 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
316 "}\\\\"
317 )
318 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400319 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400320 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400321 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400322 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400323 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400324 }
325
Stéphane Laurent316fd742017-08-12 21:02:52 +0200326 index_bottomrule <- which(x == "\\bottomrule")
327 x <- x[-index_bottomrule]
328 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400329 x <- c(
330 x[1:header_rows_end],
331 "\\endfirsthead",
332 continue_line,
333 x[header_rows_start:header_rows_end],
334 "\\endhead",
335 x[(header_rows_end + 1):length(x)]
336 )
337 x <- paste0(x, collapse = "\n")
338 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400339}
340
Hao Zhua3fc0c42017-02-27 12:04:59 -0500341styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400342 col_align <- as.character(factor(
343 table_info$align_vector, c("c", "l", "r"),
344 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
345 ))
346 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
347 col_align_vector <- col_align
348 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500349 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
350 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400351 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500352 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400353 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500354}
355
356styling_latex_position <- function(x, table_info, position, latex_options) {
newtuxd2867062017-09-29 00:19:27 -0400357 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500358 switch(
359 position,
360 center = styling_latex_position_center(x, table_info, hold_position),
361 left = styling_latex_position_left(x, table_info),
362 right = styling_latex_position_right(x, table_info, hold_position),
363 float_left = styling_latex_position_float(x, table_info, "l"),
364 float_right = styling_latex_position_float(x, table_info, "r")
365 )
366}
367
368styling_latex_position_center <- function(x, table_info, hold_position) {
369 if (!table_info$table_env & table_info$tabular == "tabular") {
newtuxd2867062017-09-29 00:19:27 -0400370 x <- paste0("\\begin{table}\n\\centering", x, "\n\\end{table}")
371 if (hold_position == "hold_position") {
372 x <- styling_latex_hold_position(x)
373 } else {
374 x <- styling_latex_HOLD_position(x)
375 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500376 }
377 return(x)
378}
379
380styling_latex_position_left <- function(x, table_info) {
381 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
382 longtable_option <- "\\[l\\]"
383 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
384 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
385}
386
387styling_latex_position_right <- function(x, table_info, hold_position) {
388 warning("Position = right is only supported for longtable in LaTeX. ",
389 "Setting back to center...")
390 styling_latex_position_center(x, table_info, hold_position)
391}
392
393styling_latex_position_float <- function(x, table_info, option) {
394 if (table_info$tabular == "longtable") {
395 warning("wraptable is not supported for longtable.")
396 if (option == "l") return(styling_latex_position_left(x, table_info))
397 if (option == "r") return(styling_latex_position_right(x, table_info, F))
398 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500399 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
400 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500401 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500402 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
403 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
404 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
405 x <- sub("\\\\begin\\{table\\}", option, x)
406 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500407 } else {
408 option <- sprintf("\\begin{wraptable}{%s}", option)
409 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
410 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500411 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500412 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500413}
414
415styling_latex_font_size <- function(x, table_info, font_size) {
416 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400417 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500418 return(sub(table_info$begin_tabular,
419 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
420 "\\}\\\\selectfont\n", table_info$begin_tabular),
421 x))
422 }
423 # For longtable and tabular without table environment. Simple wrap around
424 # fontsize is good enough
425 return(paste0(
426 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
427 "\\endgroup"
428 ))
429}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400430
Hao Zhu245931c2017-09-01 22:43:56 -0400431styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400432 x <- sub(
433 paste0("begin\\{", current_env, "\\}\\[t\\]"),
434 paste0("begin\\{", latex_table_env, "\\}"), x
435 )
436 x <- sub(
437 paste0("begin\\{", current_env, "\\}"),
438 paste0("begin\\{", latex_table_env, "\\}"), x
439 )
440 x <- sub(
441 paste0("end\\{", current_env, "\\}"),
442 paste0("end\\{", latex_table_env, "\\}"), x
443 )
444 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400445}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400446