blob: 10cbba7916667786a9b7fca611e5fd005b4cc480 [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
Salzerc53f0cd2018-02-23 06:38:34 -050026#' table.
Hao Zhu59f5fe02017-02-22 11:27:14 -050027#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhuf7994dd2017-02-27 16:58:42 -050028#' table should have 100\% width. Since HTML and pdf have different flavors on
29#' the preferable format for `full_width`. If not specified, a HTML table will
30#' have full width by default but this option will be set to `FALSE` for a
31#' LaTeX table
32#' @param position A character string determining how to position the table
33#' on a page. Possible values include `left`, `center`, `right`, `float_left`
34#' and `float_right`. Please see the package doc site for demonstrations. For
35#' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be
36#' imported.
Hao Zhu94956582017-02-21 18:18:29 -050037#' @param font_size A numeric input for table font size
Hao Zhu81eb9342018-12-11 10:38:52 -050038#' @param row_label_position A character string determining the justification
39#' of the row labels in a table. Possible values inclued `l` for left, `c` for
40#' center, and `r` for right. The default value is `l` for left justifcation.
41#' @param repeat_header_method LaTeX option, can either be `append`(default) or
42#' `replace`
43#' @param repeat_header_text LaTeX option. A text string you want to append on
44#' or replace the caption.
45#' @param stripe_color LaTeX option allowing users to pick a different color
46#' for their strip lines. This option is not available in HTML
47#' @param latex_table_env LaTeX option. A character string to define customized
48#' table environment such as tabu or tabularx.You shouldn't expect all features
49#' could be supported in self-defined environments.
Hao Zhue10cfd32017-02-21 16:41:14 -050050#'
antaldanielcf11dac2018-05-30 21:54:37 +020051#' @details For LaTeX, if you use other than English environment
52#' - all tables are converted to 'UTF-8'. If you use, for example, Hungarian
53#' characters on a Windows machine, make sure to use
54#' Sys.setlocale("LC_ALL","Hungarian") to avoid unexpected conversions.
55#'
Hao Zhu78e61222017-05-24 20:53:35 -040056#' @examples x_html <- knitr::kable(head(mtcars), "html")
57#' kable_styling(x_html, "striped", position = "left", font_size = 7)
58#'
59#' x_latex <- knitr::kable(head(mtcars), "latex")
60#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
61#'
Hao Zhue10cfd32017-02-21 16:41:14 -050062#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050063kable_styling <- function(kable_input,
64 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050065 latex_options = "basic",
66 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050067 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040068 font_size = NULL,
Salzerc53f0cd2018-02-23 06:38:34 -050069 row_label_position = "l",
Hao Zhu81eb9342018-12-11 10:38:52 -050070 repeat_header_text = "\\textit{(continued)}",
71 repeat_header_method = c("append", "replace"),
72 repeat_header_continued = FALSE,
73 stripe_color = "gray!6",
74 latex_table_env = NULL) {
Hao Zhu9b45a182017-02-27 18:17:46 -050075
76 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
77 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
78 }
79 if (length(latex_options) == 1 && latex_options == "basic") {
80 latex_options <- getOption("kable_styling_latex_options", "basic")
81 }
82 if (position == "center") {
83 position <- getOption("kable_styling_position", "center")
84 }
85 position <- match.arg(position,
86 c("center", "left", "right", "float_left", "float_right"))
87 if (is.null(font_size)) {
88 font_size <- getOption("kable_styling_font_size", NULL)
89 }
90
Hao Zhuc1f38412017-02-23 12:13:48 -050091 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050092
Hao Zhuc1f38412017-02-23 12:13:48 -050093 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050094 warning("Please specify format in kable. kableExtra can customize either ",
95 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
96 "for details.")
97 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050098 }
99 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500100 if (is.null(full_width)) {
101 full_width <- getOption("kable_styling_full_width", T)
102 }
Hao Zhuc1f38412017-02-23 12:13:48 -0500103 return(htmlTable_styling(kable_input,
104 bootstrap_options = bootstrap_options,
105 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -0500106 position = position,
Hao Zhu81eb9342018-12-11 10:38:52 -0500107 font_size = font_size))
Hao Zhuc1f38412017-02-23 12:13:48 -0500108 }
109 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500110 if (is.null(full_width)) {
111 full_width <- getOption("kable_styling_full_width", F)
112 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500113 return(pdfTable_styling(kable_input,
114 latex_options = latex_options,
115 full_width = full_width,
116 position = position,
Salzerc53f0cd2018-02-23 06:38:34 -0500117 font_size = font_size,
118 row_label_position = row_label_position,
Hao Zhu81eb9342018-12-11 10:38:52 -0500119 repeat_header_text = repeat_header_text,
120 repeat_header_method = repeat_header_method,
121 repeat_header_continued = repeat_header_continued,
122 stripe_color = stripe_color,
123 latex_table_env = latex_table_env))
Hao Zhuc1f38412017-02-23 12:13:48 -0500124 }
125}
126
127# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500128htmlTable_styling <- function(kable_input,
129 bootstrap_options = "basic",
130 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500131 position = c("center", "left", "right",
132 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -0500133 font_size = NULL) {
Hao Zhu909ea382017-06-12 15:43:47 -0400134 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400135 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500136
137 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500138 bootstrap_options <- match.arg(
139 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500140 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500141 several.ok = T
142 )
143
Hao Zhu26234122017-02-22 15:34:33 -0500144 kable_xml_class <- NULL
145 if (xml_has_attr(kable_xml, "class")) {
146 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500147 }
Hao Zhu26234122017-02-22 15:34:33 -0500148 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
149 bootstrap_options <- "table"
150 } else {
151 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
152 bootstrap_options <- paste0("table-", bootstrap_options)
153 bootstrap_options <- c("table", bootstrap_options)
154 }
155 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
156 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500157
Hao Zhu26234122017-02-22 15:34:33 -0500158 # Modify style
159 kable_xml_style <- NULL
160 if (xml_has_attr(kable_xml, "style")) {
161 kable_xml_style <- xml_attr(kable_xml, "style")
162 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500163 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500164 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500165 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400166 kable_caption_node <- xml_tpart(kable_xml, "caption")
167 if (!is.null(kable_caption_node)) {
168 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
169 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500170 }
171 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500172 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500173 }
Hao Zhu94956582017-02-21 18:18:29 -0500174
Hao Zhuc05e1812017-02-25 01:45:35 -0500175 position <- match.arg(position)
176 position_style <- switch(
177 position,
178 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400179 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500180 right = "margin-right: 0; margin-left: auto",
181 float_left = "float: left; margin-right: 10px;",
182 float_right = "float: right; margin-left: 10px;"
183 )
184 kable_xml_style <- c(kable_xml_style, position_style)
185
Hao Zhu26234122017-02-22 15:34:33 -0500186 if (length(kable_xml_style) != 0) {
187 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500188 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500189
Hao Zhuf2dfd142017-07-24 14:43:28 -0400190 out <- as_kable_xml(kable_xml)
Hao Zhu909ea382017-06-12 15:43:47 -0400191 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500192 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu9b45a182017-02-27 18:17:46 -0500193 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500194}
Hao Zhuc1f38412017-02-23 12:13:48 -0500195
196# LaTeX table style
197pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500198 latex_options = "basic",
199 full_width = F,
Hao Zhu81eb9342018-12-11 10:38:52 -0500200 position,
201 font_size,
202 row_label_position,
203 repeat_header_text,
204 repeat_header_method,
205 repeat_header_continued,
206 stripe_color,
207 latex_table_env) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500208
Hao Zhuc05e1812017-02-25 01:45:35 -0500209 latex_options <- match.arg(
210 latex_options,
Salzerc53f0cd2018-02-23 06:38:34 -0500211 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500212 several.ok = T
213 )
214
Hao Zhuca211db2017-07-25 00:06:43 -0400215 repeat_header_method <- match.arg(repeat_header_method)
216
Hao Zhua3fc0c42017-02-27 12:04:59 -0500217 out <- NULL
Hao Zhu3fc0e882018-04-03 16:06:41 -0400218 out <- solve_enc(kable_input)
219
Hao Zhuc05e1812017-02-25 01:45:35 -0500220 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500221
222 if ("striped" %in% latex_options) {
Hao Zhu71a224f2017-08-18 11:06:22 -0400223 out <- styling_latex_striped(out, table_info, stripe_color)
Hao Zhuc05e1812017-02-25 01:45:35 -0500224 }
225
226 # hold_position is only meaningful in a table environment
227 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500228 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500229 }
230
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100231 # HOLD_position is only meaningful in a table environment
232 if ("HOLD_position" %in% latex_options & table_info$table_env) {
233 out <- styling_latex_HOLD_position(out)
234 }
235
Hao Zhua3fc0c42017-02-27 12:04:59 -0500236 if ("scale_down" %in% latex_options) {
237 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500238 }
239
Hao Zhu971d89f2017-06-07 19:22:47 -0400240 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400241 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400242 repeat_header_method, repeat_header_continued)
Hao Zhu971d89f2017-06-07 19:22:47 -0400243 }
244
Hao Zhuc05e1812017-02-25 01:45:35 -0500245 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400246 latex_table_env <- ifelse(table_info$tabular == "longtable",
247 "longtabu", "tabu")
248 full_width_return <- styling_latex_full_width(out, table_info)
249 out <- full_width_return[[1]]
250 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500251 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500252
Hao Zhua3fc0c42017-02-27 12:04:59 -0500253 if (!is.null(font_size)) {
254 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500255 }
256
Hao Zhu245931c2017-09-01 22:43:56 -0400257 if (!is.null(latex_table_env)) {
258 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
259 table_info$tabular <- latex_table_env
260 table_info$begin_tabular <- sub("tabular", latex_table_env,
261 table_info$begin_tabular)
262 table_info$end_tabular <- sub("tabular", latex_table_env,
263 table_info$end_tabular)
264 }
265
Hao Zhuc05e1812017-02-25 01:45:35 -0500266 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500267 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500268
269 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400270 attr(out, "kable_meta") <- table_info
bsalzer74e2e222018-02-12 17:23:12 -0500271
Salzerc53f0cd2018-02-23 06:38:34 -0500272 if (row_label_position != "l") {
Hao Zhu81eb9342018-12-11 10:38:52 -0500273 if (table_info$tabular == "longtable") {
bsalzer74e2e222018-02-12 17:23:12 -0500274 out <- sub("\\\\begin\\{longtable\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500275 paste0("\\\\begin\\{longtable\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500276 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500277 out)
278 } else {
279 out <- sub("\\\\begin\\{tabular\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500280 paste0("\\\\begin\\{tabular\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500281 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500282 out)
283 }
284 }
285
Hao Zhuc05e1812017-02-25 01:45:35 -0500286 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500287}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500288
Hao Zhue1be9602017-08-17 15:44:31 -0400289styling_latex_striped <- function(x, table_info, color) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400290 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
291 if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400292 row_color <- sprintf("\\rowcolors{%s}{white}{%s}",
293 1 + table_info$position_offset, color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400294 } else {
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400295 if (table_info$position_offset == 0) {
Leo83f05132018-05-10 14:12:16 +0800296 row_color <- sprintf("\\rowcolors{1}{white}{%s}", color)
297 } else {
298 row_color <- sprintf("\\rowcolors{2}{%s}{white}", color)
299 }
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400300 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400301
302 x <- read_lines(x)
303 if (table_info$booktabs) {
Hao Zhu76f0eb62018-09-15 12:38:33 -0400304 header_rows_start <- which(trimws(x) == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800305 if (is.null(table_info$colnames)) {
306 header_rows_end <- header_rows_start
307 } else {
Hao Zhu76f0eb62018-09-15 12:38:33 -0400308 header_rows_end <- which(trimws(x) == "\\midrule")[1]
Leo83f05132018-05-10 14:12:16 +0800309 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400310 } else {
Hao Zhu76f0eb62018-09-15 12:38:33 -0400311 header_rows_start <- which(trimws(x) == "\\hline")[1]
312 header_rows_end <- which(trimws(x) == "\\hline")[2]
Hao Zhu00ba87c2017-08-01 12:42:58 -0400313 }
314
315 x <- c(
316 row_color,
317 x[1:(header_rows_start - 1)],
318 "\\hiderowcolors",
319 x[header_rows_start:header_rows_end],
320 "\\showrowcolors",
321 x[(header_rows_end + 1):length(x)],
322 "\\rowcolors{2}{white}{white}"
323 )
324 x <- paste0(x, collapse = "\n")
325 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500326}
327
328styling_latex_hold_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400329 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
330 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[!h]")
331 } else {
332 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]")
333 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500334}
335
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100336styling_latex_HOLD_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400337 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
338 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[H]")
339 } else {
340 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]")
341 }
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100342}
343
Hao Zhua3fc0c42017-02-27 12:04:59 -0500344styling_latex_scale_down <- function(x, table_info) {
345 # You cannot put longtable in a resizebox
346 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
347 if (table_info$tabular == "longtable") {
348 warning("Longtable cannot be resized.")
349 return(x)
350 }
351 x <- sub(table_info$begin_tabular,
Hao Zhuae80df42018-04-12 15:45:11 -0400352 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{\n",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500353 table_info$begin_tabular),
354 x)
355 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
356}
357
Hao Zhuca211db2017-07-25 00:06:43 -0400358styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400359 repeat_header_method,
360 repeat_header_continued) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400361 x <- read_lines(x)
362 if (table_info$booktabs) {
363 header_rows_start <- which(x == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800364 if (is.null(table_info$colnames)) {
365 header_rows_end <- header_rows_start
366 } else {
367 header_rows_end <- which(x == "\\midrule")[1]
368 }
Hao Zhua31e97f2017-06-08 14:55:41 -0400369 } else {
370 header_rows_start <- which(x == "\\hline")[1]
371 header_rows_end <- which(x == "\\hline")[2]
372 }
Hao Zhufdec1842017-06-08 17:06:04 -0400373
374 if (is.na(table_info$caption)) {
375 continue_line <- paste0(
376 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
377 "}\\\\"
378 )
379 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400380 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400381 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400382 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400383 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400384 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400385 }
386
Hao Zhu6c9714a2017-10-02 14:52:45 -0400387 if (!table_info$booktabs) {
388 bottom_part <- NULL
389 } else {
390 index_bottomrule <- which(x == "\\bottomrule")
391 x <- x[-index_bottomrule]
Hao Zhu20784e72017-10-10 14:48:33 -0400392 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*")
Hao Zhu6c9714a2017-10-02 14:52:45 -0400393
394 if (repeat_header_continued == FALSE) {
Hao Zhu44f6c492017-10-20 23:05:37 -0400395 bottom_part <- "\\\n\\endfoot\n\\bottomrule\n\\endlastfoot"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400396 } else {
397 if (repeat_header_continued == TRUE) {
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400398 bottom_text <- "\\textit{(continued \\ldots)}"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400399 } else {
400 bottom_text <- repeat_header_continued
401 }
402 bottom_part <- paste0(
Hao Zhu44f6c492017-10-20 23:05:37 -0400403 "\\midrule\n",
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400404 "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400405 "\\endfoot\n",
Hao Zhuc83eb632017-10-10 14:11:08 -0400406 "\\bottomrule\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400407 "\\endlastfoot"
408 )
409 }
410 }
411
412 # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400413 x <- c(
414 x[1:header_rows_end],
415 "\\endfirsthead",
416 continue_line,
417 x[header_rows_start:header_rows_end],
418 "\\endhead",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400419 bottom_part,
Hao Zhua31e97f2017-06-08 14:55:41 -0400420 x[(header_rows_end + 1):length(x)]
421 )
422 x <- paste0(x, collapse = "\n")
423 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400424}
425
Hao Zhua3fc0c42017-02-27 12:04:59 -0500426styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400427 col_align <- as.character(factor(
428 table_info$align_vector, c("c", "l", "r"),
429 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
430 ))
431 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
432 col_align_vector <- col_align
433 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500434 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
435 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400436 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500437 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400438 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500439}
440
441styling_latex_position <- function(x, table_info, position, latex_options) {
newtuxd2867062017-09-29 00:19:27 -0400442 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhu064990d2017-10-17 18:08:42 -0400443 if (length(hold_position) == 0) hold_position <- ""
Hao Zhua3fc0c42017-02-27 12:04:59 -0500444 switch(
445 position,
446 center = styling_latex_position_center(x, table_info, hold_position),
447 left = styling_latex_position_left(x, table_info),
448 right = styling_latex_position_right(x, table_info, hold_position),
449 float_left = styling_latex_position_float(x, table_info, "l"),
450 float_right = styling_latex_position_float(x, table_info, "r")
451 )
452}
453
454styling_latex_position_center <- function(x, table_info, hold_position) {
455 if (!table_info$table_env & table_info$tabular == "tabular") {
newtuxd2867062017-09-29 00:19:27 -0400456 x <- paste0("\\begin{table}\n\\centering", x, "\n\\end{table}")
457 if (hold_position == "hold_position") {
458 x <- styling_latex_hold_position(x)
459 } else {
460 x <- styling_latex_HOLD_position(x)
461 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500462 }
463 return(x)
464}
465
466styling_latex_position_left <- function(x, table_info) {
467 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
468 longtable_option <- "\\[l\\]"
469 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
470 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
471}
472
473styling_latex_position_right <- function(x, table_info, hold_position) {
474 warning("Position = right is only supported for longtable in LaTeX. ",
475 "Setting back to center...")
476 styling_latex_position_center(x, table_info, hold_position)
477}
478
479styling_latex_position_float <- function(x, table_info, option) {
480 if (table_info$tabular == "longtable") {
481 warning("wraptable is not supported for longtable.")
482 if (option == "l") return(styling_latex_position_left(x, table_info))
483 if (option == "r") return(styling_latex_position_right(x, table_info, F))
484 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500485 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
486 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500487 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500488 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400489 option <- paste0(option, "\\{0pt\\}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500490 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
491 x <- sub("\\\\begin\\{table\\}", option, x)
492 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500493 } else {
494 option <- sprintf("\\begin{wraptable}{%s}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400495 option <- paste0(option, "{0pt}")
Hao Zhuf7994dd2017-02-27 16:58:42 -0500496 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500497 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500498 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500499}
500
501styling_latex_font_size <- function(x, table_info, font_size) {
502 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400503 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500504 return(sub(table_info$begin_tabular,
505 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
506 "\\}\\\\selectfont\n", table_info$begin_tabular),
507 x))
508 }
509 # For longtable and tabular without table environment. Simple wrap around
510 # fontsize is good enough
511 return(paste0(
512 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
Hao Zhu76762d82018-07-25 20:56:52 -0400513 "\n\\endgroup{}"
Hao Zhua3fc0c42017-02-27 12:04:59 -0500514 ))
515}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400516
Hao Zhu245931c2017-09-01 22:43:56 -0400517styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400518 x <- sub(
519 paste0("begin\\{", current_env, "\\}\\[t\\]"),
520 paste0("begin\\{", latex_table_env, "\\}"), x
521 )
522 x <- sub(
523 paste0("begin\\{", current_env, "\\}"),
524 paste0("begin\\{", latex_table_env, "\\}"), x
525 )
526 x <- sub(
527 paste0("end\\{", current_env, "\\}"),
528 paste0("end\\{", latex_table_env, "\\}"), x
529 )
530 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400531}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400532