blob: 6b60780b1b81a1375b9a2c430bb0035066817b08 [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
Hao Zhud8516932019-01-06 18:56:47 -050047#' @param stripe_index LaTeX option allowing users to customize which rows
48#' should have stripe color.
Hao Zhu81eb9342018-12-11 10:38:52 -050049#' @param latex_table_env LaTeX option. A character string to define customized
50#' table environment such as tabu or tabularx.You shouldn't expect all features
51#' could be supported in self-defined environments.
Duncan Murdoch911dca32019-01-05 16:39:32 -050052#' @param protect_latex If `TRUE`, LaTeX code embedded between dollar signs
Duncan Murdoch95c14372019-01-06 10:51:58 -050053#' will be protected from HTML escaping.
Hao Zhue10cfd32017-02-21 16:41:14 -050054#'
antaldanielcf11dac2018-05-30 21:54:37 +020055#' @details For LaTeX, if you use other than English environment
56#' - all tables are converted to 'UTF-8'. If you use, for example, Hungarian
57#' characters on a Windows machine, make sure to use
58#' Sys.setlocale("LC_ALL","Hungarian") to avoid unexpected conversions.
Duncan Murdoch95c14372019-01-06 10:51:58 -050059#' - `protect_latex = TRUE` has no effect.
60#'
61#' For HTML,
62#' - `protect_latex = TRUE` is for including complicated math in HTML output.
63#' The LaTeX may not include dollar signs even if they are escaped.
64#' Pandoc's rules for recognizing embedded LaTeX are used.
antaldanielcf11dac2018-05-30 21:54:37 +020065#'
Hao Zhu78e61222017-05-24 20:53:35 -040066#' @examples x_html <- knitr::kable(head(mtcars), "html")
67#' kable_styling(x_html, "striped", position = "left", font_size = 7)
68#'
69#' x_latex <- knitr::kable(head(mtcars), "latex")
70#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
71#'
Hao Zhue10cfd32017-02-21 16:41:14 -050072#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050073kable_styling <- function(kable_input,
74 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050075 latex_options = "basic",
76 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050077 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040078 font_size = NULL,
Salzerc53f0cd2018-02-23 06:38:34 -050079 row_label_position = "l",
Hao Zhu81eb9342018-12-11 10:38:52 -050080 repeat_header_text = "\\textit{(continued)}",
81 repeat_header_method = c("append", "replace"),
82 repeat_header_continued = FALSE,
83 stripe_color = "gray!6",
Hao Zhud8516932019-01-06 18:56:47 -050084 stripe_index = NULL,
Duncan Murdoch911dca32019-01-05 16:39:32 -050085 latex_table_env = NULL,
86 protect_latex = TRUE) {
Hao Zhu9b45a182017-02-27 18:17:46 -050087
88 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
89 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
90 }
91 if (length(latex_options) == 1 && latex_options == "basic") {
92 latex_options <- getOption("kable_styling_latex_options", "basic")
93 }
94 if (position == "center") {
95 position <- getOption("kable_styling_position", "center")
96 }
97 position <- match.arg(position,
98 c("center", "left", "right", "float_left", "float_right"))
99 if (is.null(font_size)) {
100 font_size <- getOption("kable_styling_font_size", NULL)
101 }
102
Hao Zhuc1f38412017-02-23 12:13:48 -0500103 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -0500104
Hao Zhuc1f38412017-02-23 12:13:48 -0500105 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -0500106 warning("Please specify format in kable. kableExtra can customize either ",
107 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
108 "for details.")
109 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -0500110 }
111 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500112 if (is.null(full_width)) {
113 full_width <- getOption("kable_styling_full_width", T)
114 }
Hao Zhuc1f38412017-02-23 12:13:48 -0500115 return(htmlTable_styling(kable_input,
116 bootstrap_options = bootstrap_options,
117 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -0500118 position = position,
Duncan Murdoch911dca32019-01-05 16:39:32 -0500119 font_size = font_size,
120 protect_latex = protect_latex))
Hao Zhuc1f38412017-02-23 12:13:48 -0500121 }
122 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500123 if (is.null(full_width)) {
124 full_width <- getOption("kable_styling_full_width", F)
125 }
Hao Zhube1e0552018-12-12 14:10:43 -0500126 repeat_header_method <- match.arg(repeat_header_method)
Hao Zhuc05e1812017-02-25 01:45:35 -0500127 return(pdfTable_styling(kable_input,
128 latex_options = latex_options,
129 full_width = full_width,
130 position = position,
Salzerc53f0cd2018-02-23 06:38:34 -0500131 font_size = font_size,
132 row_label_position = row_label_position,
Hao Zhu81eb9342018-12-11 10:38:52 -0500133 repeat_header_text = repeat_header_text,
134 repeat_header_method = repeat_header_method,
135 repeat_header_continued = repeat_header_continued,
136 stripe_color = stripe_color,
Hao Zhud8516932019-01-06 18:56:47 -0500137 stripe_index = stripe_index,
Hao Zhu81eb9342018-12-11 10:38:52 -0500138 latex_table_env = latex_table_env))
Hao Zhuc1f38412017-02-23 12:13:48 -0500139 }
140}
141
Duncan Murdoch911dca32019-01-05 16:39:32 -0500142extract_latex_from_kable <- function(kable_input) {
143 kable_attrs <- attributes(kable_input)
Duncan Murdoch95c14372019-01-06 10:51:58 -0500144 regexp <- paste0("(?<!\\e)", # Not escaped
145 "([$]{1}(?![ ])[^$]+(?<![$\\\\ ])[$]{1}", # $...$
146 "|[$]{2}(?![ ])[^$]+(?<![$\\\\ ])[$]{2})", # $$...$$
147 "(?!\\d)") # Not followed by digit
Duncan Murdoch911dca32019-01-05 16:39:32 -0500148 latex <- character()
Duncan Murdoch95c14372019-01-06 10:51:58 -0500149 while (str_detect(kable_input, regexp)) {
150 block <- str_extract(kable_input, regexp)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500151 name <- paste0("latex", digest(block))
152 latex[name] <- block
Duncan Murdoch95c14372019-01-06 10:51:58 -0500153 kable_input <- str_replace(kable_input, regexp, name)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500154 }
155 kable_attrs$extracted_latex <- latex
156 attributes(kable_input) <- kable_attrs
157 kable_input
158}
159
160replace_latex_in_kable <- function(kable_input, latex) {
161 kable_attrs <- attributes(kable_input)
162 for (n in names(latex)) {
163 kable_input <- str_replace_all(kable_input, fixed(n), latex[n])
164 }
165 attributes(kable_input) <- kable_attrs
166 kable_input
167}
168
Hao Zhuc1f38412017-02-23 12:13:48 -0500169# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500170htmlTable_styling <- function(kable_input,
171 bootstrap_options = "basic",
172 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500173 position = c("center", "left", "right",
174 "float_left", "float_right"),
Duncan Murdoch911dca32019-01-05 16:39:32 -0500175 font_size = NULL,
176 protect_latex = TRUE) {
177 if (protect_latex) {
178 kable_input <- extract_latex_from_kable(kable_input)
179 }
Hao Zhu909ea382017-06-12 15:43:47 -0400180 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400181 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500182
183 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500184 bootstrap_options <- match.arg(
185 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500186 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500187 several.ok = T
188 )
189
Hao Zhu26234122017-02-22 15:34:33 -0500190 kable_xml_class <- NULL
191 if (xml_has_attr(kable_xml, "class")) {
192 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500193 }
Hao Zhu26234122017-02-22 15:34:33 -0500194 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
195 bootstrap_options <- "table"
196 } else {
197 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
198 bootstrap_options <- paste0("table-", bootstrap_options)
199 bootstrap_options <- c("table", bootstrap_options)
200 }
201 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
202 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500203
Hao Zhu26234122017-02-22 15:34:33 -0500204 # Modify style
205 kable_xml_style <- NULL
206 if (xml_has_attr(kable_xml, "style")) {
207 kable_xml_style <- xml_attr(kable_xml, "style")
208 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500209 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500210 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500211 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400212 kable_caption_node <- xml_tpart(kable_xml, "caption")
213 if (!is.null(kable_caption_node)) {
214 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
215 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500216 }
217 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500218 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500219 }
Hao Zhu94956582017-02-21 18:18:29 -0500220
Hao Zhuc05e1812017-02-25 01:45:35 -0500221 position <- match.arg(position)
222 position_style <- switch(
223 position,
224 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400225 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500226 right = "margin-right: 0; margin-left: auto",
227 float_left = "float: left; margin-right: 10px;",
228 float_right = "float: right; margin-left: 10px;"
229 )
230 kable_xml_style <- c(kable_xml_style, position_style)
231
Hao Zhu26234122017-02-22 15:34:33 -0500232 if (length(kable_xml_style) != 0) {
233 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500234 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500235
Hao Zhuf2dfd142017-07-24 14:43:28 -0400236 out <- as_kable_xml(kable_xml)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500237 if (protect_latex) {
238 out <- replace_latex_in_kable(out, kable_attrs$extracted_latex)
239 kable_attrs$extracted_latex <- NULL
240 }
Hao Zhu909ea382017-06-12 15:43:47 -0400241 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500242 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu9b45a182017-02-27 18:17:46 -0500243 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500244}
Hao Zhuc1f38412017-02-23 12:13:48 -0500245
Hao Zhube1e0552018-12-12 14:10:43 -0500246# LaTeX table style ------------
Hao Zhuc1f38412017-02-23 12:13:48 -0500247pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500248 latex_options = "basic",
Hao Zhube1e0552018-12-12 14:10:43 -0500249 full_width = FALSE,
Hao Zhu81eb9342018-12-11 10:38:52 -0500250 position,
251 font_size,
252 row_label_position,
253 repeat_header_text,
254 repeat_header_method,
255 repeat_header_continued,
256 stripe_color,
Hao Zhud8516932019-01-06 18:56:47 -0500257 stripe_index,
Hao Zhu81eb9342018-12-11 10:38:52 -0500258 latex_table_env) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500259
Hao Zhuc05e1812017-02-25 01:45:35 -0500260 latex_options <- match.arg(
261 latex_options,
Salzerc53f0cd2018-02-23 06:38:34 -0500262 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500263 several.ok = T
264 )
265
Hao Zhua3fc0c42017-02-27 12:04:59 -0500266 out <- NULL
Hao Zhu3fc0e882018-04-03 16:06:41 -0400267 out <- solve_enc(kable_input)
268
Hao Zhuc05e1812017-02-25 01:45:35 -0500269 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500270
271 if ("striped" %in% latex_options) {
Hao Zhud8516932019-01-06 18:56:47 -0500272 out <- styling_latex_striped(out, table_info, stripe_color, stripe_index)
Hao Zhuc05e1812017-02-25 01:45:35 -0500273 }
274
275 # hold_position is only meaningful in a table environment
276 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500277 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500278 }
279
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100280 # HOLD_position is only meaningful in a table environment
281 if ("HOLD_position" %in% latex_options & table_info$table_env) {
282 out <- styling_latex_HOLD_position(out)
283 }
284
Hao Zhua3fc0c42017-02-27 12:04:59 -0500285 if ("scale_down" %in% latex_options) {
286 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500287 }
288
Hao Zhu971d89f2017-06-07 19:22:47 -0400289 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400290 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400291 repeat_header_method, repeat_header_continued)
Hao Zhu971d89f2017-06-07 19:22:47 -0400292 }
293
Hao Zhuc05e1812017-02-25 01:45:35 -0500294 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400295 latex_table_env <- ifelse(table_info$tabular == "longtable",
296 "longtabu", "tabu")
297 full_width_return <- styling_latex_full_width(out, table_info)
298 out <- full_width_return[[1]]
299 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500300 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500301
Hao Zhua3fc0c42017-02-27 12:04:59 -0500302 if (!is.null(font_size)) {
303 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500304 }
305
Hao Zhu245931c2017-09-01 22:43:56 -0400306 if (!is.null(latex_table_env)) {
307 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
308 table_info$tabular <- latex_table_env
309 table_info$begin_tabular <- sub("tabular", latex_table_env,
310 table_info$begin_tabular)
311 table_info$end_tabular <- sub("tabular", latex_table_env,
312 table_info$end_tabular)
313 }
314
Hao Zhua3fc0c42017-02-27 12:04:59 -0500315 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500316
317 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400318 attr(out, "kable_meta") <- table_info
bsalzer74e2e222018-02-12 17:23:12 -0500319
Salzerc53f0cd2018-02-23 06:38:34 -0500320 if (row_label_position != "l") {
Hao Zhu81eb9342018-12-11 10:38:52 -0500321 if (table_info$tabular == "longtable") {
bsalzer74e2e222018-02-12 17:23:12 -0500322 out <- sub("\\\\begin\\{longtable\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500323 paste0("\\\\begin\\{longtable\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500324 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500325 out)
326 } else {
327 out <- sub("\\\\begin\\{tabular\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500328 paste0("\\\\begin\\{tabular\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500329 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500330 out)
331 }
332 }
333
Hao Zhuc05e1812017-02-25 01:45:35 -0500334 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500335}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500336
Hao Zhud8516932019-01-06 18:56:47 -0500337styling_latex_striped <- function(x, table_info, color, stripe_index) {
338 if (is.null(stripe_index)) {
Hao Zhu685eaab2019-01-07 12:18:26 -0500339 striped_index <- seq(1, table_info$nrow - table_info$position_offset, 2)
Hao Zhud8516932019-01-06 18:56:47 -0500340 }
341 row_spec(x, striped_index, background = color)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500342}
343
344styling_latex_hold_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400345 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
346 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[!h]")
347 } else {
348 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]")
349 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500350}
351
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100352styling_latex_HOLD_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400353 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
354 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[H]")
355 } else {
356 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]")
357 }
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100358}
359
Hao Zhua3fc0c42017-02-27 12:04:59 -0500360styling_latex_scale_down <- function(x, table_info) {
361 # You cannot put longtable in a resizebox
362 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
363 if (table_info$tabular == "longtable") {
364 warning("Longtable cannot be resized.")
365 return(x)
366 }
367 x <- sub(table_info$begin_tabular,
Hao Zhuae80df42018-04-12 15:45:11 -0400368 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{\n",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500369 table_info$begin_tabular),
370 x)
371 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
372}
373
Hao Zhuca211db2017-07-25 00:06:43 -0400374styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400375 repeat_header_method,
376 repeat_header_continued) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400377 x <- read_lines(x)
378 if (table_info$booktabs) {
379 header_rows_start <- which(x == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800380 if (is.null(table_info$colnames)) {
381 header_rows_end <- header_rows_start
382 } else {
383 header_rows_end <- which(x == "\\midrule")[1]
384 }
Hao Zhua31e97f2017-06-08 14:55:41 -0400385 } else {
386 header_rows_start <- which(x == "\\hline")[1]
387 header_rows_end <- which(x == "\\hline")[2]
388 }
Hao Zhufdec1842017-06-08 17:06:04 -0400389
390 if (is.na(table_info$caption)) {
391 continue_line <- paste0(
392 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
393 "}\\\\"
394 )
395 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400396 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400397 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400398 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400399 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400400 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400401 }
402
Hao Zhu6c9714a2017-10-02 14:52:45 -0400403 if (!table_info$booktabs) {
404 bottom_part <- NULL
405 } else {
406 index_bottomrule <- which(x == "\\bottomrule")
407 x <- x[-index_bottomrule]
Hao Zhu20784e72017-10-10 14:48:33 -0400408 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*")
Hao Zhu6c9714a2017-10-02 14:52:45 -0400409
410 if (repeat_header_continued == FALSE) {
Hao Zhu44f6c492017-10-20 23:05:37 -0400411 bottom_part <- "\\\n\\endfoot\n\\bottomrule\n\\endlastfoot"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400412 } else {
413 if (repeat_header_continued == TRUE) {
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400414 bottom_text <- "\\textit{(continued \\ldots)}"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400415 } else {
416 bottom_text <- repeat_header_continued
417 }
418 bottom_part <- paste0(
Hao Zhu44f6c492017-10-20 23:05:37 -0400419 "\\midrule\n",
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400420 "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400421 "\\endfoot\n",
Hao Zhuc83eb632017-10-10 14:11:08 -0400422 "\\bottomrule\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400423 "\\endlastfoot"
424 )
425 }
426 }
427
428 # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400429 x <- c(
430 x[1:header_rows_end],
431 "\\endfirsthead",
432 continue_line,
433 x[header_rows_start:header_rows_end],
434 "\\endhead",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400435 bottom_part,
Hao Zhua31e97f2017-06-08 14:55:41 -0400436 x[(header_rows_end + 1):length(x)]
437 )
438 x <- paste0(x, collapse = "\n")
439 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400440}
441
Hao Zhua3fc0c42017-02-27 12:04:59 -0500442styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400443 col_align <- as.character(factor(
444 table_info$align_vector, c("c", "l", "r"),
445 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
446 ))
447 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
448 col_align_vector <- col_align
449 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500450 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
451 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400452 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500453 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400454 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500455}
456
457styling_latex_position <- function(x, table_info, position, latex_options) {
newtuxd2867062017-09-29 00:19:27 -0400458 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhu064990d2017-10-17 18:08:42 -0400459 if (length(hold_position) == 0) hold_position <- ""
Hao Zhua3fc0c42017-02-27 12:04:59 -0500460 switch(
461 position,
462 center = styling_latex_position_center(x, table_info, hold_position),
463 left = styling_latex_position_left(x, table_info),
464 right = styling_latex_position_right(x, table_info, hold_position),
465 float_left = styling_latex_position_float(x, table_info, "l"),
466 float_right = styling_latex_position_float(x, table_info, "r")
467 )
468}
469
470styling_latex_position_center <- function(x, table_info, hold_position) {
471 if (!table_info$table_env & table_info$tabular == "tabular") {
newtuxd2867062017-09-29 00:19:27 -0400472 x <- paste0("\\begin{table}\n\\centering", x, "\n\\end{table}")
473 if (hold_position == "hold_position") {
474 x <- styling_latex_hold_position(x)
475 } else {
476 x <- styling_latex_HOLD_position(x)
477 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500478 }
479 return(x)
480}
481
482styling_latex_position_left <- function(x, table_info) {
483 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
484 longtable_option <- "\\[l\\]"
485 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
486 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
487}
488
489styling_latex_position_right <- function(x, table_info, hold_position) {
490 warning("Position = right is only supported for longtable in LaTeX. ",
491 "Setting back to center...")
492 styling_latex_position_center(x, table_info, hold_position)
493}
494
495styling_latex_position_float <- function(x, table_info, option) {
496 if (table_info$tabular == "longtable") {
497 warning("wraptable is not supported for longtable.")
498 if (option == "l") return(styling_latex_position_left(x, table_info))
499 if (option == "r") return(styling_latex_position_right(x, table_info, F))
500 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500501 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
502 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500503 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500504 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400505 option <- paste0(option, "\\{0pt\\}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500506 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
507 x <- sub("\\\\begin\\{table\\}", option, x)
508 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500509 } else {
510 option <- sprintf("\\begin{wraptable}{%s}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400511 option <- paste0(option, "{0pt}")
Hao Zhuf7994dd2017-02-27 16:58:42 -0500512 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500513 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500514 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500515}
516
517styling_latex_font_size <- function(x, table_info, font_size) {
518 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400519 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500520 return(sub(table_info$begin_tabular,
521 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
522 "\\}\\\\selectfont\n", table_info$begin_tabular),
523 x))
524 }
525 # For longtable and tabular without table environment. Simple wrap around
526 # fontsize is good enough
527 return(paste0(
528 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
Hao Zhu76762d82018-07-25 20:56:52 -0400529 "\n\\endgroup{}"
Hao Zhua3fc0c42017-02-27 12:04:59 -0500530 ))
531}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400532
Hao Zhu245931c2017-09-01 22:43:56 -0400533styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400534 x <- sub(
535 paste0("begin\\{", current_env, "\\}\\[t\\]"),
536 paste0("begin\\{", latex_table_env, "\\}"), x
537 )
538 x <- sub(
539 paste0("begin\\{", current_env, "\\}"),
540 paste0("begin\\{", latex_table_env, "\\}"), x
541 )
542 x <- sub(
543 paste0("end\\{", current_env, "\\}"),
544 paste0("end\\{", latex_table_env, "\\}"), x
545 )
546 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400547}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400548