blob: 0d41372cb6119c76ff041d7a6be6e5569da10c2b [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`,
Hao Zhuad4ea392020-08-10 01:24:50 -040013#' `bordered`, `hover`, `condensed`, `responsive` and `none`.
Hao Zhuf7994dd2017-02-27 16:58:42 -050014#' @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.
Hao Zhu81eb9342018-12-11 10:38:52 -050041#' @param repeat_header_text LaTeX option. A text string you want to append on
42#' or replace the caption.
Hao Zhu1c4917e2019-01-15 17:00:01 -060043#' @param repeat_header_method LaTeX option, can either be `append`(default) or
44#' `replace`
45#' @param repeat_header_continued T/F or a text string. Whether or not to put
46#' a continued mark on the second page of longtable. If you put in text, we will
47#' use this text as the "continued" mark.
Hao Zhu81eb9342018-12-11 10:38:52 -050048#' @param stripe_color LaTeX option allowing users to pick a different color
49#' for their strip lines. This option is not available in HTML
Hao Zhud8516932019-01-06 18:56:47 -050050#' @param stripe_index LaTeX option allowing users to customize which rows
51#' should have stripe color.
Hao Zhu81eb9342018-12-11 10:38:52 -050052#' @param latex_table_env LaTeX option. A character string to define customized
53#' table environment such as tabu or tabularx.You shouldn't expect all features
54#' could be supported in self-defined environments.
Duncan Murdoch911dca32019-01-05 16:39:32 -050055#' @param protect_latex If `TRUE`, LaTeX code embedded between dollar signs
Duncan Murdoch95c14372019-01-06 10:51:58 -050056#' will be protected from HTML escaping.
Hao Zhu9b051592019-01-07 18:13:51 -050057#' @param table.envir LaTeX floating table environment. `kable_style` will put
58#' a plain no-caption table in a `table` environment in order to center the
59#' table. You can specify this option to things like `table*` or `float*` based
60#' on your need.
Hao Zhu72917f92019-03-15 18:41:42 -040061#' @param fixed_thead HTML table option so table header row is fixed at top.
62#' Values can be either T/F or `list(enabled = T/F, background = "anycolor")`.
Hao Zhu3676fe22020-08-11 19:42:42 -040063#' @param htmltable_class Options to use the in-house lightable themes.
Hao Zhuad4ea392020-08-10 01:24:50 -040064#' Choices include `lightable-minimal`, `lightable-classic`,
Hao Zhu3676fe22020-08-11 19:42:42 -040065#' `lightable-classic-2`, `lightable-material`, `lightable-striped` and
66#' `lightable-hover`. If you have your customized style sheet loaded which
67#' defines your own table class, you can also load it here.
Hao Zhu33b865f2020-08-18 02:10:43 -040068#' @param html_font A string for HTML css font. For example,
69#' `html_font = '"Arial Narrow", arial, helvetica, sans-serif'`.
Hao Zhuf2f42022020-08-18 22:48:52 -040070#' @param wraptable_width Width of the wraptable area if you specify
71#' "float_left/right" for latex table. Default is "0pt" for automated
72#' determination but you may specify it manually.
Hao Zhue10cfd32017-02-21 16:41:14 -050073#'
antaldanielcf11dac2018-05-30 21:54:37 +020074#' @details For LaTeX, if you use other than English environment
75#' - all tables are converted to 'UTF-8'. If you use, for example, Hungarian
76#' characters on a Windows machine, make sure to use
77#' Sys.setlocale("LC_ALL","Hungarian") to avoid unexpected conversions.
Duncan Murdoch95c14372019-01-06 10:51:58 -050078#' - `protect_latex = TRUE` has no effect.
79#'
80#' For HTML,
81#' - `protect_latex = TRUE` is for including complicated math in HTML output.
82#' The LaTeX may not include dollar signs even if they are escaped.
83#' Pandoc's rules for recognizing embedded LaTeX are used.
antaldanielcf11dac2018-05-30 21:54:37 +020084#'
Hao Zhu9399dcc2020-08-26 17:27:38 -040085#' @examples
86#' \dontrun{
87#' x_html <- knitr::kable(head(mtcars), "html")
Hao Zhu78e61222017-05-24 20:53:35 -040088#' kable_styling(x_html, "striped", position = "left", font_size = 7)
89#'
90#' x_latex <- knitr::kable(head(mtcars), "latex")
91#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
Hao Zhu9399dcc2020-08-26 17:27:38 -040092#' }
Hao Zhu78e61222017-05-24 20:53:35 -040093#'
Hao Zhue10cfd32017-02-21 16:41:14 -050094#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050095kable_styling <- function(kable_input,
96 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050097 latex_options = "basic",
98 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050099 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -0400100 font_size = NULL,
Salzerc53f0cd2018-02-23 06:38:34 -0500101 row_label_position = "l",
Hao Zhu81eb9342018-12-11 10:38:52 -0500102 repeat_header_text = "\\textit{(continued)}",
103 repeat_header_method = c("append", "replace"),
104 repeat_header_continued = FALSE,
105 stripe_color = "gray!6",
Hao Zhud8516932019-01-06 18:56:47 -0500106 stripe_index = NULL,
Duncan Murdoch911dca32019-01-05 16:39:32 -0500107 latex_table_env = NULL,
Hao Zhu9b051592019-01-07 18:13:51 -0500108 protect_latex = TRUE,
Hao Zhu72917f92019-03-15 18:41:42 -0400109 table.envir = "table",
Hao Zhuad4ea392020-08-10 01:24:50 -0400110 fixed_thead = FALSE,
Hao Zhu33b865f2020-08-18 02:10:43 -0400111 htmltable_class = NULL,
Hao Zhuf2f42022020-08-18 22:48:52 -0400112 html_font = NULL,
113 wraptable_width = '0pt') {
Hao Zhu9b45a182017-02-27 18:17:46 -0500114
115 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
116 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
117 }
118 if (length(latex_options) == 1 && latex_options == "basic") {
119 latex_options <- getOption("kable_styling_latex_options", "basic")
120 }
121 if (position == "center") {
122 position <- getOption("kable_styling_position", "center")
123 }
124 position <- match.arg(position,
125 c("center", "left", "right", "float_left", "float_right"))
126 if (is.null(font_size)) {
127 font_size <- getOption("kable_styling_font_size", NULL)
128 }
129
Hao Zhuc1f38412017-02-23 12:13:48 -0500130 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -0500131
Hao Zhuc1f38412017-02-23 12:13:48 -0500132 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -0500133 warning("Please specify format in kable. kableExtra can customize either ",
134 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
135 "for details.")
136 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -0500137 }
138 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500139 if (is.null(full_width)) {
140 full_width <- getOption("kable_styling_full_width", T)
141 }
Hao Zhuc1f38412017-02-23 12:13:48 -0500142 return(htmlTable_styling(kable_input,
143 bootstrap_options = bootstrap_options,
144 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -0500145 position = position,
Duncan Murdoch911dca32019-01-05 16:39:32 -0500146 font_size = font_size,
Hao Zhu72917f92019-03-15 18:41:42 -0400147 protect_latex = protect_latex,
Hao Zhuad4ea392020-08-10 01:24:50 -0400148 fixed_thead = fixed_thead,
Hao Zhu33b865f2020-08-18 02:10:43 -0400149 htmltable_class = htmltable_class,
150 html_font = html_font))
Hao Zhuc1f38412017-02-23 12:13:48 -0500151 }
152 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500153 if (is.null(full_width)) {
154 full_width <- getOption("kable_styling_full_width", F)
155 }
Hao Zhube1e0552018-12-12 14:10:43 -0500156 repeat_header_method <- match.arg(repeat_header_method)
Hao Zhuc05e1812017-02-25 01:45:35 -0500157 return(pdfTable_styling(kable_input,
158 latex_options = latex_options,
159 full_width = full_width,
160 position = position,
Salzerc53f0cd2018-02-23 06:38:34 -0500161 font_size = font_size,
162 row_label_position = row_label_position,
Hao Zhu81eb9342018-12-11 10:38:52 -0500163 repeat_header_text = repeat_header_text,
164 repeat_header_method = repeat_header_method,
165 repeat_header_continued = repeat_header_continued,
166 stripe_color = stripe_color,
Hao Zhud8516932019-01-06 18:56:47 -0500167 stripe_index = stripe_index,
Hao Zhu9b051592019-01-07 18:13:51 -0500168 latex_table_env = latex_table_env,
Hao Zhuf2f42022020-08-18 22:48:52 -0400169 table.envir = table.envir,
170 wraptable_width = wraptable_width))
Hao Zhuc1f38412017-02-23 12:13:48 -0500171 }
172}
173
Duncan Murdoch911dca32019-01-05 16:39:32 -0500174extract_latex_from_kable <- function(kable_input) {
175 kable_attrs <- attributes(kable_input)
Duncan Murdoch95c14372019-01-06 10:51:58 -0500176 regexp <- paste0("(?<!\\e)", # Not escaped
177 "([$]{1}(?![ ])[^$]+(?<![$\\\\ ])[$]{1}", # $...$
178 "|[$]{2}(?![ ])[^$]+(?<![$\\\\ ])[$]{2})", # $$...$$
179 "(?!\\d)") # Not followed by digit
Duncan Murdoch911dca32019-01-05 16:39:32 -0500180 latex <- character()
Duncan Murdoch95c14372019-01-06 10:51:58 -0500181 while (str_detect(kable_input, regexp)) {
182 block <- str_extract(kable_input, regexp)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500183 name <- paste0("latex", digest(block))
184 latex[name] <- block
Duncan Murdoch95c14372019-01-06 10:51:58 -0500185 kable_input <- str_replace(kable_input, regexp, name)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500186 }
187 kable_attrs$extracted_latex <- latex
188 attributes(kable_input) <- kable_attrs
189 kable_input
190}
191
192replace_latex_in_kable <- function(kable_input, latex) {
193 kable_attrs <- attributes(kable_input)
194 for (n in names(latex)) {
195 kable_input <- str_replace_all(kable_input, fixed(n), latex[n])
196 }
197 attributes(kable_input) <- kable_attrs
198 kable_input
199}
200
Hao Zhuc1f38412017-02-23 12:13:48 -0500201# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500202htmlTable_styling <- function(kable_input,
203 bootstrap_options = "basic",
204 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500205 position = c("center", "left", "right",
206 "float_left", "float_right"),
Duncan Murdoch911dca32019-01-05 16:39:32 -0500207 font_size = NULL,
Hao Zhu72917f92019-03-15 18:41:42 -0400208 protect_latex = TRUE,
Hao Zhuad4ea392020-08-10 01:24:50 -0400209 fixed_thead = FALSE,
Hao Zhu33b865f2020-08-18 02:10:43 -0400210 htmltable_class = NULL,
211 html_font = NULL) {
Duncan Murdoch911dca32019-01-05 16:39:32 -0500212 if (protect_latex) {
213 kable_input <- extract_latex_from_kable(kable_input)
214 }
Hao Zhu909ea382017-06-12 15:43:47 -0400215 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400216 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500217
218 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500219 bootstrap_options <- match.arg(
220 bootstrap_options,
Hao Zhuad4ea392020-08-10 01:24:50 -0400221 c("basic", "striped", "bordered", "hover", "condensed", "responsive",
222 "none"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500223 several.ok = T
224 )
225
Hao Zhu26234122017-02-22 15:34:33 -0500226 kable_xml_class <- NULL
227 if (xml_has_attr(kable_xml, "class")) {
228 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500229 }
Hao Zhuad4ea392020-08-10 01:24:50 -0400230
Hao Zhu3676fe22020-08-11 19:42:42 -0400231 if (!is.null(htmltable_class)) {
Hao Zhuad4ea392020-08-10 01:24:50 -0400232 bootstrap_options <- "none"
Hao Zhu3676fe22020-08-11 19:42:42 -0400233 xml_attr(kable_xml, "class") <- paste(kable_xml_class, htmltable_class)
Hao Zhu26234122017-02-22 15:34:33 -0500234 }
Hao Zhuad4ea392020-08-10 01:24:50 -0400235
236 if (length(bootstrap_options) == 1 && bootstrap_options == "none") {
237 }else {
238 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
239 bootstrap_options <- "table"
240 } else {
241 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
242 bootstrap_options <- paste0("table-", bootstrap_options)
243 bootstrap_options <- c("table", bootstrap_options)
244 }
245 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
246 collapse = " ")
247 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500248
Hao Zhu26234122017-02-22 15:34:33 -0500249 # Modify style
250 kable_xml_style <- NULL
251 if (xml_has_attr(kable_xml, "style")) {
252 kable_xml_style <- xml_attr(kable_xml, "style")
253 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500254 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500255 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500256 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400257 kable_caption_node <- xml_tpart(kable_xml, "caption")
258 if (!is.null(kable_caption_node)) {
259 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
260 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500261 }
Hao Zhu33b865f2020-08-18 02:10:43 -0400262 if (!is.null(html_font)) {
263 kable_xml_style <- c(kable_xml_style, paste0(
264 'font-family: ', html_font, ';'
265 ))
266 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500267 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500268 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500269 }
Hao Zhu94956582017-02-21 18:18:29 -0500270
Hao Zhuc05e1812017-02-25 01:45:35 -0500271 position <- match.arg(position)
272 position_style <- switch(
273 position,
274 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400275 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500276 right = "margin-right: 0; margin-left: auto",
277 float_left = "float: left; margin-right: 10px;",
278 float_right = "float: right; margin-left: 10px;"
279 )
280 kable_xml_style <- c(kable_xml_style, position_style)
281
Hao Zhu26234122017-02-22 15:34:33 -0500282 if (length(kable_xml_style) != 0) {
283 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500284 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500285
Hao Zhu72917f92019-03-15 18:41:42 -0400286 fixed_thead <- get_fixed_thead(fixed_thead)
287 if (fixed_thead$enabled) {
288 all_header_cells <- xml2::xml_find_all(kable_xml, "//thead//th")
289 if (is.null(fixed_thead$background)) fixed_thead$background <- "#FFFFFF"
290 for (i in seq(length(all_header_cells))) {
291 xml_attr(all_header_cells[i], "style") <- paste0(
292 xml_attr(all_header_cells[i], "style"),
293 "position: sticky; top:0; background-color: ",
294 fixed_thead$background, ";"
295 )
296 }
297 }
298
Hao Zhuf2dfd142017-07-24 14:43:28 -0400299 out <- as_kable_xml(kable_xml)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500300 if (protect_latex) {
301 out <- replace_latex_in_kable(out, kable_attrs$extracted_latex)
302 kable_attrs$extracted_latex <- NULL
303 }
Hao Zhu909ea382017-06-12 15:43:47 -0400304 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500305 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu9b45a182017-02-27 18:17:46 -0500306 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500307}
Hao Zhuc1f38412017-02-23 12:13:48 -0500308
Hao Zhube1e0552018-12-12 14:10:43 -0500309# LaTeX table style ------------
Hao Zhuc1f38412017-02-23 12:13:48 -0500310pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500311 latex_options = "basic",
Hao Zhube1e0552018-12-12 14:10:43 -0500312 full_width = FALSE,
Hao Zhu81eb9342018-12-11 10:38:52 -0500313 position,
314 font_size,
315 row_label_position,
316 repeat_header_text,
317 repeat_header_method,
318 repeat_header_continued,
319 stripe_color,
Hao Zhud8516932019-01-06 18:56:47 -0500320 stripe_index,
Hao Zhu9b051592019-01-07 18:13:51 -0500321 latex_table_env,
Hao Zhuf2f42022020-08-18 22:48:52 -0400322 table.envir,
323 wraptable_width) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500324
Hao Zhuc05e1812017-02-25 01:45:35 -0500325 latex_options <- match.arg(
326 latex_options,
Salzerc53f0cd2018-02-23 06:38:34 -0500327 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500328 several.ok = T
329 )
330
Hao Zhua3fc0c42017-02-27 12:04:59 -0500331 out <- NULL
Hao Zhu3fc0e882018-04-03 16:06:41 -0400332 out <- solve_enc(kable_input)
333
Hao Zhuc05e1812017-02-25 01:45:35 -0500334 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500335
336 if ("striped" %in% latex_options) {
Hao Zhud8516932019-01-06 18:56:47 -0500337 out <- styling_latex_striped(out, table_info, stripe_color, stripe_index)
Hao Zhubdcffde2020-10-03 09:17:28 -0400338 table_info <- magic_mirror(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500339 }
340
341 # hold_position is only meaningful in a table environment
342 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500343 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500344 }
345
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100346 # HOLD_position is only meaningful in a table environment
347 if ("HOLD_position" %in% latex_options & table_info$table_env) {
348 out <- styling_latex_HOLD_position(out)
349 }
350
Hao Zhua3fc0c42017-02-27 12:04:59 -0500351 if ("scale_down" %in% latex_options) {
352 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500353 }
354
Hao Zhu971d89f2017-06-07 19:22:47 -0400355 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400356 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400357 repeat_header_method, repeat_header_continued)
Hao Zhu9c3007e2020-08-03 13:52:38 -0400358 table_info$repeat_header_latex <- TRUE
Hao Zhu971d89f2017-06-07 19:22:47 -0400359 }
360
Hao Zhuc05e1812017-02-25 01:45:35 -0500361 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400362 latex_table_env <- ifelse(table_info$tabular == "longtable",
363 "longtabu", "tabu")
364 full_width_return <- styling_latex_full_width(out, table_info)
365 out <- full_width_return[[1]]
366 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500367 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500368
Hao Zhua3fc0c42017-02-27 12:04:59 -0500369 if (!is.null(font_size)) {
370 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500371 }
372
Hao Zhu245931c2017-09-01 22:43:56 -0400373 if (!is.null(latex_table_env)) {
374 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
375 table_info$tabular <- latex_table_env
376 table_info$begin_tabular <- sub("tabular", latex_table_env,
377 table_info$begin_tabular)
378 table_info$end_tabular <- sub("tabular", latex_table_env,
379 table_info$end_tabular)
380 }
381
Hao Zhu9b051592019-01-07 18:13:51 -0500382 out <- styling_latex_position(out, table_info, position, latex_options,
Hao Zhuf2f42022020-08-18 22:48:52 -0400383 table.envir, wraptable_width)
Hao Zhuc05e1812017-02-25 01:45:35 -0500384
385 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400386 attr(out, "kable_meta") <- table_info
bsalzer74e2e222018-02-12 17:23:12 -0500387
Salzerc53f0cd2018-02-23 06:38:34 -0500388 if (row_label_position != "l") {
Hao Zhu81eb9342018-12-11 10:38:52 -0500389 if (table_info$tabular == "longtable") {
bsalzer74e2e222018-02-12 17:23:12 -0500390 out <- sub("\\\\begin\\{longtable\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500391 paste0("\\\\begin\\{longtable\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500392 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500393 out)
394 } else {
395 out <- sub("\\\\begin\\{tabular\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500396 paste0("\\\\begin\\{tabular\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500397 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500398 out)
399 }
400 }
401
Hao Zhuc05e1812017-02-25 01:45:35 -0500402 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500403}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500404
Hao Zhud8516932019-01-06 18:56:47 -0500405styling_latex_striped <- function(x, table_info, color, stripe_index) {
406 if (is.null(stripe_index)) {
Hao Zhu72917f92019-03-15 18:41:42 -0400407 stripe_index <- seq(1, table_info$nrow - table_info$position_offset, 2)
Hao Zhud8516932019-01-06 18:56:47 -0500408 }
Hao Zhu72917f92019-03-15 18:41:42 -0400409 row_spec(x, stripe_index, background = color)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500410}
411
412styling_latex_hold_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400413 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
414 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[!h]")
415 } else {
416 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]")
417 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500418}
419
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100420styling_latex_HOLD_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400421 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
422 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[H]")
423 } else {
424 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]")
425 }
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100426}
427
Hao Zhua3fc0c42017-02-27 12:04:59 -0500428styling_latex_scale_down <- function(x, table_info) {
429 # You cannot put longtable in a resizebox
430 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
431 if (table_info$tabular == "longtable") {
432 warning("Longtable cannot be resized.")
433 return(x)
434 }
435 x <- sub(table_info$begin_tabular,
Hao Zhuae80df42018-04-12 15:45:11 -0400436 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{\n",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500437 table_info$begin_tabular),
438 x)
439 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
440}
441
Hao Zhuca211db2017-07-25 00:06:43 -0400442styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400443 repeat_header_method,
444 repeat_header_continued) {
Hao Zhu16d66192020-08-19 10:15:34 -0400445 x <- str_split(x, "\n")[[1]]
Hao Zhua31e97f2017-06-08 14:55:41 -0400446 if (table_info$booktabs) {
447 header_rows_start <- which(x == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800448 if (is.null(table_info$colnames)) {
449 header_rows_end <- header_rows_start
450 } else {
451 header_rows_end <- which(x == "\\midrule")[1]
452 }
Hao Zhua31e97f2017-06-08 14:55:41 -0400453 } else {
454 header_rows_start <- which(x == "\\hline")[1]
455 header_rows_end <- which(x == "\\hline")[2]
456 }
Hao Zhufdec1842017-06-08 17:06:04 -0400457
458 if (is.na(table_info$caption)) {
459 continue_line <- paste0(
460 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
461 "}\\\\"
462 )
463 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400464 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400465 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400466 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400467 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400468 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400469 }
470
Hao Zhu6c9714a2017-10-02 14:52:45 -0400471 if (!table_info$booktabs) {
472 bottom_part <- NULL
473 } else {
474 index_bottomrule <- which(x == "\\bottomrule")
475 x <- x[-index_bottomrule]
Hao Zhu20784e72017-10-10 14:48:33 -0400476 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*")
Hao Zhu6c9714a2017-10-02 14:52:45 -0400477
478 if (repeat_header_continued == FALSE) {
Hao Zhu3078d8b2019-04-26 14:49:25 -0400479 bottom_part <- "\n\\endfoot\n\\bottomrule\n\\endlastfoot"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400480 } else {
481 if (repeat_header_continued == TRUE) {
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400482 bottom_text <- "\\textit{(continued \\ldots)}"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400483 } else {
484 bottom_text <- repeat_header_continued
485 }
486 bottom_part <- paste0(
Hao Zhu44f6c492017-10-20 23:05:37 -0400487 "\\midrule\n",
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400488 "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400489 "\\endfoot\n",
Hao Zhuc83eb632017-10-10 14:11:08 -0400490 "\\bottomrule\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400491 "\\endlastfoot"
492 )
493 }
494 }
495
496 # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400497 x <- c(
498 x[1:header_rows_end],
499 "\\endfirsthead",
500 continue_line,
501 x[header_rows_start:header_rows_end],
502 "\\endhead",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400503 bottom_part,
Hao Zhua31e97f2017-06-08 14:55:41 -0400504 x[(header_rows_end + 1):length(x)]
505 )
506 x <- paste0(x, collapse = "\n")
507 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400508}
509
Hao Zhua3fc0c42017-02-27 12:04:59 -0500510styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400511 col_align <- as.character(factor(
512 table_info$align_vector, c("c", "l", "r"),
513 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
514 ))
515 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
516 col_align_vector <- col_align
517 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500518 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
519 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400520 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500521 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400522 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500523}
524
Hao Zhu9b051592019-01-07 18:13:51 -0500525styling_latex_position <- function(x, table_info, position, latex_options,
Hao Zhuf2f42022020-08-18 22:48:52 -0400526 table.envir, wraptable_position) {
newtuxd2867062017-09-29 00:19:27 -0400527 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhu064990d2017-10-17 18:08:42 -0400528 if (length(hold_position) == 0) hold_position <- ""
Hao Zhua3fc0c42017-02-27 12:04:59 -0500529 switch(
530 position,
Hao Zhu9b051592019-01-07 18:13:51 -0500531 center = styling_latex_position_center(x, table_info, hold_position,
532 table.envir),
Hao Zhua3fc0c42017-02-27 12:04:59 -0500533 left = styling_latex_position_left(x, table_info),
Hao Zhu9b051592019-01-07 18:13:51 -0500534 right = styling_latex_position_right(x, table_info, hold_position,
535 table.envir),
Hao Zhuf2f42022020-08-18 22:48:52 -0400536 float_left = styling_latex_position_float(x, table_info, "l", table.envir,
537 wraptable_position),
538 float_right = styling_latex_position_float(x, table_info, "r", table.envir,
539 wraptable_position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500540 )
541}
542
Hao Zhu9b051592019-01-07 18:13:51 -0500543styling_latex_position_center <- function(x, table_info, hold_position,
544 table.envir) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500545 if (!table_info$table_env & table_info$tabular == "tabular") {
Hao Zhu9b051592019-01-07 18:13:51 -0500546 x <- paste0("\\begin{", table.envir, "}\n\\centering", x,
547 "\n\\end{", table.envir, "}")
newtuxd2867062017-09-29 00:19:27 -0400548 if (hold_position == "hold_position") {
549 x <- styling_latex_hold_position(x)
Greg Macfarlane70404ee2020-12-23 10:15:26 -0700550 } else if(hold_position == "HOLD_position") {
newtuxd2867062017-09-29 00:19:27 -0400551 x <- styling_latex_HOLD_position(x)
552 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500553 }
554 return(x)
555}
556
557styling_latex_position_left <- function(x, table_info) {
558 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
559 longtable_option <- "\\[l\\]"
560 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
561 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
562}
563
Hao Zhu9b051592019-01-07 18:13:51 -0500564styling_latex_position_right <- function(x, table_info, hold_position,
565 table.envir) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500566 warning("Position = right is only supported for longtable in LaTeX. ",
567 "Setting back to center...")
Hao Zhu9b051592019-01-07 18:13:51 -0500568 styling_latex_position_center(x, table_info, hold_position, table.envir)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500569}
570
Hao Zhuf2f42022020-08-18 22:48:52 -0400571styling_latex_position_float <- function(x, table_info, option, table.envir,
572 wraptable_width) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500573 if (table_info$tabular == "longtable") {
574 warning("wraptable is not supported for longtable.")
575 if (option == "l") return(styling_latex_position_left(x, table_info))
Hao Zhu9b051592019-01-07 18:13:51 -0500576 if (option == "r") return(styling_latex_position_right(x, table_info, F,
577 table.envir))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500578 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500579 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
580 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500581 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500582 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
Hao Zhuf2f42022020-08-18 22:48:52 -0400583 option <- paste0(option, "\\{", wraptable_width, "\\}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500584 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
585 x <- sub("\\\\begin\\{table\\}", option, x)
586 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500587 } else {
588 option <- sprintf("\\begin{wraptable}{%s}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400589 option <- paste0(option, "{0pt}")
Hao Zhuf7994dd2017-02-27 16:58:42 -0500590 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500591 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500592 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500593}
594
595styling_latex_font_size <- function(x, table_info, font_size) {
596 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400597 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500598 return(sub(table_info$begin_tabular,
599 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
600 "\\}\\\\selectfont\n", table_info$begin_tabular),
601 x))
602 }
603 # For longtable and tabular without table environment. Simple wrap around
604 # fontsize is good enough
605 return(paste0(
606 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
Hao Zhu76762d82018-07-25 20:56:52 -0400607 "\n\\endgroup{}"
Hao Zhua3fc0c42017-02-27 12:04:59 -0500608 ))
609}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400610
Hao Zhu245931c2017-09-01 22:43:56 -0400611styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400612 x <- sub(
613 paste0("begin\\{", current_env, "\\}\\[t\\]"),
614 paste0("begin\\{", latex_table_env, "\\}"), x
615 )
616 x <- sub(
617 paste0("begin\\{", current_env, "\\}"),
618 paste0("begin\\{", latex_table_env, "\\}"), x
619 )
620 x <- sub(
621 paste0("end\\{", current_env, "\\}"),
622 paste0("end\\{", latex_table_env, "\\}"), x
623 )
624 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400625}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400626