blob: cd996a8c6a077577bf6df9d1abdb30d5e40688b9 [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
Hao Zhue7c8f702017-10-10 13:22:59 -04004#' of HTML tables other than using the `table.attr` option in `knitr::kable()`. Note
5#' that those bootstrap options requires Twitter bootstrap theme, which is not avaiable
6#' in some customized template being loaded.
Hao Zhue10cfd32017-02-21 16:41:14 -05007#'
Hao Zhuf7994dd2017-02-27 16:58:42 -05008#' @param kable_input Output of `knitr::kable()` with `format` specified
9#' @param bootstrap_options A character vector for bootstrap table options.
Hao Zhu6a076462017-03-01 12:59:01 -050010#' Please see package vignette or visit the w3schools'
Hao Zhuf7994dd2017-02-27 16:58:42 -050011#' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page}
12#' for more information. Possible options include `basic`, `striped`,
13#' `bordered`, `hover`, `condensed` and `responsive`.
14#' @param latex_options A character vector for LaTeX table options. Please see
Hao Zhu6a076462017-03-01 12:59:01 -050015#' package vignette for more information. Possible options include
Rob Shepherdc59d15e2017-08-26 16:06:36 +010016#' `basic`, `striped`, `hold_position`, `HOLD_position`, `scale_down` & `repeat_header`.
Hao Zhu971d89f2017-06-07 19:22:47 -040017#' `striped` will add alternative row colors to the table. It will imports
18#' `LaTeX` package `xcolor` if enabled. `hold_position` will "hold" the floating
19#' table to the exact position. It is useful when the `LaTeX` table is contained
20#' in a `table` environment after you specified captions in `kable()`. It will
21#' force the table to stay in the position where it was created in the document.
Hao Zhu53e240f2017-09-04 20:04:29 -040022#' A stronger version: `HOLD_position` requires the `float` package and specifies `[H]`.
Hao Zhuf7994dd2017-02-27 16:58:42 -050023#' `scale_down` is useful for super wide table. It will automatically adjust
Hao Zhu971d89f2017-06-07 19:22:47 -040024#' the table to page width. `repeat_header` in only meaningful in a longtable
25#' environment. It will let the header row repeat on every page in that long
bsalzer74e2e222018-02-12 17:23:12 -050026#' table. `row_label_right` will right justify the row labels and `row_label_center`
27#' will center justify the row labels. Default justification is left.
Hao Zhu59f5fe02017-02-22 11:27:14 -050028#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhuf7994dd2017-02-27 16:58:42 -050029#' table should have 100\% width. Since HTML and pdf have different flavors on
30#' the preferable format for `full_width`. If not specified, a HTML table will
31#' have full width by default but this option will be set to `FALSE` for a
32#' LaTeX table
33#' @param position A character string determining how to position the table
34#' on a page. Possible values include `left`, `center`, `right`, `float_left`
35#' and `float_right`. Please see the package doc site for demonstrations. For
36#' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be
37#' imported.
Hao Zhu94956582017-02-21 18:18:29 -050038#' @param font_size A numeric input for table font size
Hao Zhue1be9602017-08-17 15:44:31 -040039#' @param ... extra options for HTML or LaTeX. See `details`.
40#'
41#' @details For LaTeX, extra options includes:
42#' - `repeat_header_method` can either be `append`(default) or `replace`
43#' - `repeat_header_text` is just a text string you want to append on or
44#' replace the caption.
Hao Zhu71a224f2017-08-18 11:06:22 -040045#' - `stripe_color` allows users to pick a different color for their strip lines.
Hao Zhu245931c2017-09-01 22:43:56 -040046#' - `latex_table_env` character string to define customized table environment
47#' such as tabu or tabularx.You shouldn't expect all features could be
48#' supported in self-defined environments.
Hao Zhue10cfd32017-02-21 16:41:14 -050049#'
Hao Zhu78e61222017-05-24 20:53:35 -040050#' @examples x_html <- knitr::kable(head(mtcars), "html")
51#' kable_styling(x_html, "striped", position = "left", font_size = 7)
52#'
53#' x_latex <- knitr::kable(head(mtcars), "latex")
54#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
55#'
Hao Zhue10cfd32017-02-21 16:41:14 -050056#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050057kable_styling <- function(kable_input,
58 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050059 latex_options = "basic",
60 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050061 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040062 font_size = NULL,
63 ...) {
Hao Zhu9b45a182017-02-27 18:17:46 -050064
65 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
66 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
67 }
68 if (length(latex_options) == 1 && latex_options == "basic") {
69 latex_options <- getOption("kable_styling_latex_options", "basic")
70 }
71 if (position == "center") {
72 position <- getOption("kable_styling_position", "center")
73 }
74 position <- match.arg(position,
75 c("center", "left", "right", "float_left", "float_right"))
76 if (is.null(font_size)) {
77 font_size <- getOption("kable_styling_font_size", NULL)
78 }
79
Hao Zhuc1f38412017-02-23 12:13:48 -050080 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050081
Hao Zhuc1f38412017-02-23 12:13:48 -050082 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050083 warning("Please specify format in kable. kableExtra can customize either ",
84 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
85 "for details.")
86 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050087 }
88 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050089 if (is.null(full_width)) {
90 full_width <- getOption("kable_styling_full_width", T)
91 }
Hao Zhuc1f38412017-02-23 12:13:48 -050092 return(htmlTable_styling(kable_input,
93 bootstrap_options = bootstrap_options,
94 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050095 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -040096 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -050097 }
98 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -050099 if (is.null(full_width)) {
100 full_width <- getOption("kable_styling_full_width", F)
101 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500102 return(pdfTable_styling(kable_input,
103 latex_options = latex_options,
104 full_width = full_width,
105 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -0400106 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -0500107 }
108}
109
110# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500111htmlTable_styling <- function(kable_input,
112 bootstrap_options = "basic",
113 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500114 position = c("center", "left", "right",
115 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -0500116 font_size = NULL) {
Hao Zhu909ea382017-06-12 15:43:47 -0400117 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400118 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500119
120 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500121 bootstrap_options <- match.arg(
122 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500123 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500124 several.ok = T
125 )
126
Hao Zhu26234122017-02-22 15:34:33 -0500127 kable_xml_class <- NULL
128 if (xml_has_attr(kable_xml, "class")) {
129 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500130 }
Hao Zhu26234122017-02-22 15:34:33 -0500131 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
132 bootstrap_options <- "table"
133 } else {
134 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
135 bootstrap_options <- paste0("table-", bootstrap_options)
136 bootstrap_options <- c("table", bootstrap_options)
137 }
138 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
139 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500140
Hao Zhu26234122017-02-22 15:34:33 -0500141 # Modify style
142 kable_xml_style <- NULL
143 if (xml_has_attr(kable_xml, "style")) {
144 kable_xml_style <- xml_attr(kable_xml, "style")
145 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500146 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500147 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500148 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400149 kable_caption_node <- xml_tpart(kable_xml, "caption")
150 if (!is.null(kable_caption_node)) {
151 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
152 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500153 }
154 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500155 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500156 }
Hao Zhu94956582017-02-21 18:18:29 -0500157
Hao Zhuc05e1812017-02-25 01:45:35 -0500158 position <- match.arg(position)
159 position_style <- switch(
160 position,
161 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400162 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500163 right = "margin-right: 0; margin-left: auto",
164 float_left = "float: left; margin-right: 10px;",
165 float_right = "float: right; margin-left: 10px;"
166 )
167 kable_xml_style <- c(kable_xml_style, position_style)
168
Hao Zhu26234122017-02-22 15:34:33 -0500169 if (length(kable_xml_style) != 0) {
170 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500171 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500172
Hao Zhuf2dfd142017-07-24 14:43:28 -0400173 out <- as_kable_xml(kable_xml)
Hao Zhu909ea382017-06-12 15:43:47 -0400174 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500175 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu9b45a182017-02-27 18:17:46 -0500176 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500177}
Hao Zhuc1f38412017-02-23 12:13:48 -0500178
179# LaTeX table style
180pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500181 latex_options = "basic",
182 full_width = F,
183 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500184 "float_left", "float_right"),
Hao Zhua31e97f2017-06-08 14:55:41 -0400185 font_size = NULL,
Hao Zhuca211db2017-07-25 00:06:43 -0400186 repeat_header_text = "\\textit{(continued)}",
Hao Zhue1be9602017-08-17 15:44:31 -0400187 repeat_header_method = c("append", "replace"),
Hao Zhu6c9714a2017-10-02 14:52:45 -0400188 repeat_header_continued = FALSE,
Hao Zhu245931c2017-09-01 22:43:56 -0400189 stripe_color = "gray!6",
190 latex_table_env = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500191
Hao Zhuc05e1812017-02-25 01:45:35 -0500192 latex_options <- match.arg(
193 latex_options,
bsalzer74e2e222018-02-12 17:23:12 -0500194 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header",
195 "row_label_right","row_label_center"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500196 several.ok = T
197 )
198
Hao Zhuca211db2017-07-25 00:06:43 -0400199 repeat_header_method <- match.arg(repeat_header_method)
200
Hao Zhua3fc0c42017-02-27 12:04:59 -0500201 out <- NULL
Hao Zhud2c0f732017-08-26 10:40:14 -0400202 out <- enc2utf8(as.character(kable_input))
Hao Zhuc05e1812017-02-25 01:45:35 -0500203 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500204
205 if ("striped" %in% latex_options) {
Hao Zhu71a224f2017-08-18 11:06:22 -0400206 out <- styling_latex_striped(out, table_info, stripe_color)
Hao Zhuc05e1812017-02-25 01:45:35 -0500207 }
208
209 # hold_position is only meaningful in a table environment
210 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500211 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500212 }
213
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100214 # HOLD_position is only meaningful in a table environment
215 if ("HOLD_position" %in% latex_options & table_info$table_env) {
216 out <- styling_latex_HOLD_position(out)
217 }
218
Hao Zhua3fc0c42017-02-27 12:04:59 -0500219 if ("scale_down" %in% latex_options) {
220 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500221 }
222
Hao Zhu971d89f2017-06-07 19:22:47 -0400223 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400224 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400225 repeat_header_method, repeat_header_continued)
Hao Zhu971d89f2017-06-07 19:22:47 -0400226 }
227
Hao Zhuc05e1812017-02-25 01:45:35 -0500228 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400229 latex_table_env <- ifelse(table_info$tabular == "longtable",
230 "longtabu", "tabu")
231 full_width_return <- styling_latex_full_width(out, table_info)
232 out <- full_width_return[[1]]
233 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500234 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500235
Hao Zhua3fc0c42017-02-27 12:04:59 -0500236 if (!is.null(font_size)) {
237 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500238 }
239
Hao Zhu245931c2017-09-01 22:43:56 -0400240 if (!is.null(latex_table_env)) {
241 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
242 table_info$tabular <- latex_table_env
243 table_info$begin_tabular <- sub("tabular", latex_table_env,
244 table_info$begin_tabular)
245 table_info$end_tabular <- sub("tabular", latex_table_env,
246 table_info$end_tabular)
247 }
248
Hao Zhuc05e1812017-02-25 01:45:35 -0500249 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500250 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500251
252 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400253 attr(out, "kable_meta") <- table_info
bsalzer74e2e222018-02-12 17:23:12 -0500254
255 if ("row_label_right" %in% latex_options | "row_label_center" %in% latex_options) {
256 if ( "row_label_right" %in% latex_options ) {
257 row_label_orientation <- "r"
258 } else {
259 row_label_orientation <- "c"
260 }
261
262 if(table_info$tabular=="longtable") {
263 out <- sub("\\\\begin\\{longtable\\}\\{l",
264 paste0("\\begin\\{longtable\\}\\{",
265 row_label_orientation),
266 out)
267 } else {
268 out <- sub("\\\\begin\\{tabular\\}\\{l",
269 paste0("\\begin\\{tabular\\}\\{",
270 row_label_orientation),
271 out)
272 }
273 }
274
Hao Zhuc05e1812017-02-25 01:45:35 -0500275 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500276}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500277
Hao Zhue1be9602017-08-17 15:44:31 -0400278styling_latex_striped <- function(x, table_info, color) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400279 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
280 if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
Hao Zhue1be9602017-08-17 15:44:31 -0400281 row_color <- sprintf("\\rowcolors{2}{white}{%s}", color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400282 } else {
Hao Zhue1be9602017-08-17 15:44:31 -0400283 row_color <- sprintf("\\rowcolors{2}{%s}{white}", color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400284 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400285
286 x <- read_lines(x)
287 if (table_info$booktabs) {
288 header_rows_start <- which(x == "\\toprule")[1]
289 header_rows_end <- which(x == "\\midrule")[1]
290 } else {
291 header_rows_start <- which(x == "\\hline")[1]
292 header_rows_end <- which(x == "\\hline")[2]
293 }
294
295 x <- c(
296 row_color,
297 x[1:(header_rows_start - 1)],
298 "\\hiderowcolors",
299 x[header_rows_start:header_rows_end],
300 "\\showrowcolors",
301 x[(header_rows_end + 1):length(x)],
302 "\\rowcolors{2}{white}{white}"
303 )
304 x <- paste0(x, collapse = "\n")
305 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500306}
307
308styling_latex_hold_position <- function(x) {
309 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
310}
311
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100312styling_latex_HOLD_position <- function(x) {
313 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]", x)
314}
315
Hao Zhua3fc0c42017-02-27 12:04:59 -0500316styling_latex_scale_down <- function(x, table_info) {
317 # You cannot put longtable in a resizebox
318 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
319 if (table_info$tabular == "longtable") {
320 warning("Longtable cannot be resized.")
321 return(x)
322 }
323 x <- sub(table_info$begin_tabular,
Hao Zhucc21dc72017-05-20 01:15:25 -0400324 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500325 table_info$begin_tabular),
326 x)
327 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
328}
329
Hao Zhuca211db2017-07-25 00:06:43 -0400330styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400331 repeat_header_method,
332 repeat_header_continued) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400333 x <- read_lines(x)
334 if (table_info$booktabs) {
335 header_rows_start <- which(x == "\\toprule")[1]
336 header_rows_end <- which(x == "\\midrule")[1]
337 } else {
338 header_rows_start <- which(x == "\\hline")[1]
339 header_rows_end <- which(x == "\\hline")[2]
340 }
Hao Zhufdec1842017-06-08 17:06:04 -0400341
342 if (is.na(table_info$caption)) {
343 continue_line <- paste0(
344 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
345 "}\\\\"
346 )
347 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400348 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400349 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400350 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400351 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400352 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400353 }
354
Hao Zhu6c9714a2017-10-02 14:52:45 -0400355 if (!table_info$booktabs) {
356 bottom_part <- NULL
357 } else {
358 index_bottomrule <- which(x == "\\bottomrule")
359 x <- x[-index_bottomrule]
Hao Zhu20784e72017-10-10 14:48:33 -0400360 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*")
Hao Zhu6c9714a2017-10-02 14:52:45 -0400361
362 if (repeat_header_continued == FALSE) {
Hao Zhu44f6c492017-10-20 23:05:37 -0400363 bottom_part <- "\\\n\\endfoot\n\\bottomrule\n\\endlastfoot"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400364 } else {
365 if (repeat_header_continued == TRUE) {
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400366 bottom_text <- "\\textit{(continued \\ldots)}"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400367 } else {
368 bottom_text <- repeat_header_continued
369 }
370 bottom_part <- paste0(
Hao Zhu44f6c492017-10-20 23:05:37 -0400371 "\\midrule\n",
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400372 "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400373 "\\endfoot\n",
Hao Zhuc83eb632017-10-10 14:11:08 -0400374 "\\bottomrule\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400375 "\\endlastfoot"
376 )
377 }
378 }
379
380 # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400381 x <- c(
382 x[1:header_rows_end],
383 "\\endfirsthead",
384 continue_line,
385 x[header_rows_start:header_rows_end],
386 "\\endhead",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400387 bottom_part,
Hao Zhua31e97f2017-06-08 14:55:41 -0400388 x[(header_rows_end + 1):length(x)]
389 )
390 x <- paste0(x, collapse = "\n")
391 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400392}
393
Hao Zhua3fc0c42017-02-27 12:04:59 -0500394styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400395 col_align <- as.character(factor(
396 table_info$align_vector, c("c", "l", "r"),
397 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
398 ))
399 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
400 col_align_vector <- col_align
401 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500402 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
403 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400404 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500405 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400406 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500407}
408
409styling_latex_position <- function(x, table_info, position, latex_options) {
newtuxd2867062017-09-29 00:19:27 -0400410 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhu064990d2017-10-17 18:08:42 -0400411 if (length(hold_position) == 0) hold_position <- ""
Hao Zhua3fc0c42017-02-27 12:04:59 -0500412 switch(
413 position,
414 center = styling_latex_position_center(x, table_info, hold_position),
415 left = styling_latex_position_left(x, table_info),
416 right = styling_latex_position_right(x, table_info, hold_position),
417 float_left = styling_latex_position_float(x, table_info, "l"),
418 float_right = styling_latex_position_float(x, table_info, "r")
419 )
420}
421
422styling_latex_position_center <- function(x, table_info, hold_position) {
423 if (!table_info$table_env & table_info$tabular == "tabular") {
newtuxd2867062017-09-29 00:19:27 -0400424 x <- paste0("\\begin{table}\n\\centering", x, "\n\\end{table}")
425 if (hold_position == "hold_position") {
426 x <- styling_latex_hold_position(x)
427 } else {
428 x <- styling_latex_HOLD_position(x)
429 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500430 }
431 return(x)
432}
433
434styling_latex_position_left <- function(x, table_info) {
435 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
436 longtable_option <- "\\[l\\]"
437 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
438 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
439}
440
441styling_latex_position_right <- function(x, table_info, hold_position) {
442 warning("Position = right is only supported for longtable in LaTeX. ",
443 "Setting back to center...")
444 styling_latex_position_center(x, table_info, hold_position)
445}
446
447styling_latex_position_float <- function(x, table_info, option) {
448 if (table_info$tabular == "longtable") {
449 warning("wraptable is not supported for longtable.")
450 if (option == "l") return(styling_latex_position_left(x, table_info))
451 if (option == "r") return(styling_latex_position_right(x, table_info, F))
452 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500453 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
454 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500455 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500456 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
457 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
458 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
459 x <- sub("\\\\begin\\{table\\}", option, x)
460 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500461 } else {
462 option <- sprintf("\\begin{wraptable}{%s}", option)
463 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
464 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500465 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500466 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500467}
468
469styling_latex_font_size <- function(x, table_info, font_size) {
470 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400471 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500472 return(sub(table_info$begin_tabular,
473 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
474 "\\}\\\\selectfont\n", table_info$begin_tabular),
475 x))
476 }
477 # For longtable and tabular without table environment. Simple wrap around
478 # fontsize is good enough
479 return(paste0(
480 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400481 "\\endgroup{}"
Hao Zhua3fc0c42017-02-27 12:04:59 -0500482 ))
483}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400484
Hao Zhu245931c2017-09-01 22:43:56 -0400485styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400486 x <- sub(
487 paste0("begin\\{", current_env, "\\}\\[t\\]"),
488 paste0("begin\\{", latex_table_env, "\\}"), x
489 )
490 x <- sub(
491 paste0("begin\\{", current_env, "\\}"),
492 paste0("begin\\{", latex_table_env, "\\}"), x
493 )
494 x <- sub(
495 paste0("end\\{", current_env, "\\}"),
496 paste0("end\\{", latex_table_env, "\\}"), x
497 )
498 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400499}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400500