blob: 2045c15e200688528742c549824fd2cc32295900 [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.
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 Zhue10cfd32017-02-21 16:41:14 -050061#'
antaldanielcf11dac2018-05-30 21:54:37 +020062#' @details For LaTeX, if you use other than English environment
63#' - all tables are converted to 'UTF-8'. If you use, for example, Hungarian
64#' characters on a Windows machine, make sure to use
65#' Sys.setlocale("LC_ALL","Hungarian") to avoid unexpected conversions.
Duncan Murdoch95c14372019-01-06 10:51:58 -050066#' - `protect_latex = TRUE` has no effect.
67#'
68#' For HTML,
69#' - `protect_latex = TRUE` is for including complicated math in HTML output.
70#' The LaTeX may not include dollar signs even if they are escaped.
71#' Pandoc's rules for recognizing embedded LaTeX are used.
antaldanielcf11dac2018-05-30 21:54:37 +020072#'
Hao Zhu78e61222017-05-24 20:53:35 -040073#' @examples x_html <- knitr::kable(head(mtcars), "html")
74#' kable_styling(x_html, "striped", position = "left", font_size = 7)
75#'
76#' x_latex <- knitr::kable(head(mtcars), "latex")
77#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
78#'
Hao Zhue10cfd32017-02-21 16:41:14 -050079#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050080kable_styling <- function(kable_input,
81 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050082 latex_options = "basic",
83 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050084 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040085 font_size = NULL,
Salzerc53f0cd2018-02-23 06:38:34 -050086 row_label_position = "l",
Hao Zhu81eb9342018-12-11 10:38:52 -050087 repeat_header_text = "\\textit{(continued)}",
88 repeat_header_method = c("append", "replace"),
89 repeat_header_continued = FALSE,
90 stripe_color = "gray!6",
Hao Zhud8516932019-01-06 18:56:47 -050091 stripe_index = NULL,
Duncan Murdoch911dca32019-01-05 16:39:32 -050092 latex_table_env = NULL,
Hao Zhu9b051592019-01-07 18:13:51 -050093 protect_latex = TRUE,
94 table.envir = "table") {
Hao Zhu9b45a182017-02-27 18:17:46 -050095
96 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
97 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
98 }
99 if (length(latex_options) == 1 && latex_options == "basic") {
100 latex_options <- getOption("kable_styling_latex_options", "basic")
101 }
102 if (position == "center") {
103 position <- getOption("kable_styling_position", "center")
104 }
105 position <- match.arg(position,
106 c("center", "left", "right", "float_left", "float_right"))
107 if (is.null(font_size)) {
108 font_size <- getOption("kable_styling_font_size", NULL)
109 }
110
Hao Zhuc1f38412017-02-23 12:13:48 -0500111 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -0500112
Hao Zhuc1f38412017-02-23 12:13:48 -0500113 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -0500114 warning("Please specify format in kable. kableExtra can customize either ",
115 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
116 "for details.")
117 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -0500118 }
119 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500120 if (is.null(full_width)) {
121 full_width <- getOption("kable_styling_full_width", T)
122 }
Hao Zhuc1f38412017-02-23 12:13:48 -0500123 return(htmlTable_styling(kable_input,
124 bootstrap_options = bootstrap_options,
125 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -0500126 position = position,
Duncan Murdoch911dca32019-01-05 16:39:32 -0500127 font_size = font_size,
128 protect_latex = protect_latex))
Hao Zhuc1f38412017-02-23 12:13:48 -0500129 }
130 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500131 if (is.null(full_width)) {
132 full_width <- getOption("kable_styling_full_width", F)
133 }
Hao Zhube1e0552018-12-12 14:10:43 -0500134 repeat_header_method <- match.arg(repeat_header_method)
Hao Zhuc05e1812017-02-25 01:45:35 -0500135 return(pdfTable_styling(kable_input,
136 latex_options = latex_options,
137 full_width = full_width,
138 position = position,
Salzerc53f0cd2018-02-23 06:38:34 -0500139 font_size = font_size,
140 row_label_position = row_label_position,
Hao Zhu81eb9342018-12-11 10:38:52 -0500141 repeat_header_text = repeat_header_text,
142 repeat_header_method = repeat_header_method,
143 repeat_header_continued = repeat_header_continued,
144 stripe_color = stripe_color,
Hao Zhud8516932019-01-06 18:56:47 -0500145 stripe_index = stripe_index,
Hao Zhu9b051592019-01-07 18:13:51 -0500146 latex_table_env = latex_table_env,
147 table.envir = table.envir))
Hao Zhuc1f38412017-02-23 12:13:48 -0500148 }
149}
150
Duncan Murdoch911dca32019-01-05 16:39:32 -0500151extract_latex_from_kable <- function(kable_input) {
152 kable_attrs <- attributes(kable_input)
Duncan Murdoch95c14372019-01-06 10:51:58 -0500153 regexp <- paste0("(?<!\\e)", # Not escaped
154 "([$]{1}(?![ ])[^$]+(?<![$\\\\ ])[$]{1}", # $...$
155 "|[$]{2}(?![ ])[^$]+(?<![$\\\\ ])[$]{2})", # $$...$$
156 "(?!\\d)") # Not followed by digit
Duncan Murdoch911dca32019-01-05 16:39:32 -0500157 latex <- character()
Duncan Murdoch95c14372019-01-06 10:51:58 -0500158 while (str_detect(kable_input, regexp)) {
159 block <- str_extract(kable_input, regexp)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500160 name <- paste0("latex", digest(block))
161 latex[name] <- block
Duncan Murdoch95c14372019-01-06 10:51:58 -0500162 kable_input <- str_replace(kable_input, regexp, name)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500163 }
164 kable_attrs$extracted_latex <- latex
165 attributes(kable_input) <- kable_attrs
166 kable_input
167}
168
169replace_latex_in_kable <- function(kable_input, latex) {
170 kable_attrs <- attributes(kable_input)
171 for (n in names(latex)) {
172 kable_input <- str_replace_all(kable_input, fixed(n), latex[n])
173 }
174 attributes(kable_input) <- kable_attrs
175 kable_input
176}
177
Hao Zhuc1f38412017-02-23 12:13:48 -0500178# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500179htmlTable_styling <- function(kable_input,
180 bootstrap_options = "basic",
181 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500182 position = c("center", "left", "right",
183 "float_left", "float_right"),
Duncan Murdoch911dca32019-01-05 16:39:32 -0500184 font_size = NULL,
185 protect_latex = TRUE) {
186 if (protect_latex) {
187 kable_input <- extract_latex_from_kable(kable_input)
188 }
Hao Zhu909ea382017-06-12 15:43:47 -0400189 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400190 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500191
192 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500193 bootstrap_options <- match.arg(
194 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500195 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500196 several.ok = T
197 )
198
Hao Zhu26234122017-02-22 15:34:33 -0500199 kable_xml_class <- NULL
200 if (xml_has_attr(kable_xml, "class")) {
201 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500202 }
Hao Zhu26234122017-02-22 15:34:33 -0500203 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
204 bootstrap_options <- "table"
205 } else {
206 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
207 bootstrap_options <- paste0("table-", bootstrap_options)
208 bootstrap_options <- c("table", bootstrap_options)
209 }
210 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
211 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500212
Hao Zhu26234122017-02-22 15:34:33 -0500213 # Modify style
214 kable_xml_style <- NULL
215 if (xml_has_attr(kable_xml, "style")) {
216 kable_xml_style <- xml_attr(kable_xml, "style")
217 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500218 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500219 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500220 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400221 kable_caption_node <- xml_tpart(kable_xml, "caption")
222 if (!is.null(kable_caption_node)) {
223 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
224 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500225 }
226 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500227 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500228 }
Hao Zhu94956582017-02-21 18:18:29 -0500229
Hao Zhuc05e1812017-02-25 01:45:35 -0500230 position <- match.arg(position)
231 position_style <- switch(
232 position,
233 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400234 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500235 right = "margin-right: 0; margin-left: auto",
236 float_left = "float: left; margin-right: 10px;",
237 float_right = "float: right; margin-left: 10px;"
238 )
239 kable_xml_style <- c(kable_xml_style, position_style)
240
Hao Zhu26234122017-02-22 15:34:33 -0500241 if (length(kable_xml_style) != 0) {
242 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500243 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500244
Hao Zhuf2dfd142017-07-24 14:43:28 -0400245 out <- as_kable_xml(kable_xml)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500246 if (protect_latex) {
247 out <- replace_latex_in_kable(out, kable_attrs$extracted_latex)
248 kable_attrs$extracted_latex <- NULL
249 }
Hao Zhu909ea382017-06-12 15:43:47 -0400250 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500251 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu9b45a182017-02-27 18:17:46 -0500252 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500253}
Hao Zhuc1f38412017-02-23 12:13:48 -0500254
Hao Zhube1e0552018-12-12 14:10:43 -0500255# LaTeX table style ------------
Hao Zhuc1f38412017-02-23 12:13:48 -0500256pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500257 latex_options = "basic",
Hao Zhube1e0552018-12-12 14:10:43 -0500258 full_width = FALSE,
Hao Zhu81eb9342018-12-11 10:38:52 -0500259 position,
260 font_size,
261 row_label_position,
262 repeat_header_text,
263 repeat_header_method,
264 repeat_header_continued,
265 stripe_color,
Hao Zhud8516932019-01-06 18:56:47 -0500266 stripe_index,
Hao Zhu9b051592019-01-07 18:13:51 -0500267 latex_table_env,
268 table.envir) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500269
Hao Zhuc05e1812017-02-25 01:45:35 -0500270 latex_options <- match.arg(
271 latex_options,
Salzerc53f0cd2018-02-23 06:38:34 -0500272 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500273 several.ok = T
274 )
275
Hao Zhua3fc0c42017-02-27 12:04:59 -0500276 out <- NULL
Hao Zhu3fc0e882018-04-03 16:06:41 -0400277 out <- solve_enc(kable_input)
278
Hao Zhuc05e1812017-02-25 01:45:35 -0500279 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500280
281 if ("striped" %in% latex_options) {
Hao Zhud8516932019-01-06 18:56:47 -0500282 out <- styling_latex_striped(out, table_info, stripe_color, stripe_index)
Hao Zhuc05e1812017-02-25 01:45:35 -0500283 }
284
285 # hold_position is only meaningful in a table environment
286 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500287 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500288 }
289
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100290 # HOLD_position is only meaningful in a table environment
291 if ("HOLD_position" %in% latex_options & table_info$table_env) {
292 out <- styling_latex_HOLD_position(out)
293 }
294
Hao Zhua3fc0c42017-02-27 12:04:59 -0500295 if ("scale_down" %in% latex_options) {
296 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500297 }
298
Hao Zhu971d89f2017-06-07 19:22:47 -0400299 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400300 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400301 repeat_header_method, repeat_header_continued)
Hao Zhu971d89f2017-06-07 19:22:47 -0400302 }
303
Hao Zhuc05e1812017-02-25 01:45:35 -0500304 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400305 latex_table_env <- ifelse(table_info$tabular == "longtable",
306 "longtabu", "tabu")
307 full_width_return <- styling_latex_full_width(out, table_info)
308 out <- full_width_return[[1]]
309 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500310 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500311
Hao Zhua3fc0c42017-02-27 12:04:59 -0500312 if (!is.null(font_size)) {
313 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500314 }
315
Hao Zhu245931c2017-09-01 22:43:56 -0400316 if (!is.null(latex_table_env)) {
317 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
318 table_info$tabular <- latex_table_env
319 table_info$begin_tabular <- sub("tabular", latex_table_env,
320 table_info$begin_tabular)
321 table_info$end_tabular <- sub("tabular", latex_table_env,
322 table_info$end_tabular)
323 }
324
Hao Zhu9b051592019-01-07 18:13:51 -0500325 out <- styling_latex_position(out, table_info, position, latex_options,
326 table.envir)
Hao Zhuc05e1812017-02-25 01:45:35 -0500327
328 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400329 attr(out, "kable_meta") <- table_info
bsalzer74e2e222018-02-12 17:23:12 -0500330
Salzerc53f0cd2018-02-23 06:38:34 -0500331 if (row_label_position != "l") {
Hao Zhu81eb9342018-12-11 10:38:52 -0500332 if (table_info$tabular == "longtable") {
bsalzer74e2e222018-02-12 17:23:12 -0500333 out <- sub("\\\\begin\\{longtable\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500334 paste0("\\\\begin\\{longtable\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500335 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500336 out)
337 } else {
338 out <- sub("\\\\begin\\{tabular\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500339 paste0("\\\\begin\\{tabular\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500340 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500341 out)
342 }
343 }
344
Hao Zhuc05e1812017-02-25 01:45:35 -0500345 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500346}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500347
Hao Zhud8516932019-01-06 18:56:47 -0500348styling_latex_striped <- function(x, table_info, color, stripe_index) {
349 if (is.null(stripe_index)) {
Hao Zhu685eaab2019-01-07 12:18:26 -0500350 striped_index <- seq(1, table_info$nrow - table_info$position_offset, 2)
Hao Zhud8516932019-01-06 18:56:47 -0500351 }
352 row_spec(x, striped_index, background = color)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500353}
354
355styling_latex_hold_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400356 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
357 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[!h]")
358 } else {
359 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]")
360 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500361}
362
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100363styling_latex_HOLD_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400364 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
365 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[H]")
366 } else {
367 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]")
368 }
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100369}
370
Hao Zhua3fc0c42017-02-27 12:04:59 -0500371styling_latex_scale_down <- function(x, table_info) {
372 # You cannot put longtable in a resizebox
373 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
374 if (table_info$tabular == "longtable") {
375 warning("Longtable cannot be resized.")
376 return(x)
377 }
378 x <- sub(table_info$begin_tabular,
Hao Zhuae80df42018-04-12 15:45:11 -0400379 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{\n",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500380 table_info$begin_tabular),
381 x)
382 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
383}
384
Hao Zhuca211db2017-07-25 00:06:43 -0400385styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400386 repeat_header_method,
387 repeat_header_continued) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400388 x <- read_lines(x)
389 if (table_info$booktabs) {
390 header_rows_start <- which(x == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800391 if (is.null(table_info$colnames)) {
392 header_rows_end <- header_rows_start
393 } else {
394 header_rows_end <- which(x == "\\midrule")[1]
395 }
Hao Zhua31e97f2017-06-08 14:55:41 -0400396 } else {
397 header_rows_start <- which(x == "\\hline")[1]
398 header_rows_end <- which(x == "\\hline")[2]
399 }
Hao Zhufdec1842017-06-08 17:06:04 -0400400
401 if (is.na(table_info$caption)) {
402 continue_line <- paste0(
403 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
404 "}\\\\"
405 )
406 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400407 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400408 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400409 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400410 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400411 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400412 }
413
Hao Zhu6c9714a2017-10-02 14:52:45 -0400414 if (!table_info$booktabs) {
415 bottom_part <- NULL
416 } else {
417 index_bottomrule <- which(x == "\\bottomrule")
418 x <- x[-index_bottomrule]
Hao Zhu20784e72017-10-10 14:48:33 -0400419 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*")
Hao Zhu6c9714a2017-10-02 14:52:45 -0400420
421 if (repeat_header_continued == FALSE) {
Hao Zhu44f6c492017-10-20 23:05:37 -0400422 bottom_part <- "\\\n\\endfoot\n\\bottomrule\n\\endlastfoot"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400423 } else {
424 if (repeat_header_continued == TRUE) {
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400425 bottom_text <- "\\textit{(continued \\ldots)}"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400426 } else {
427 bottom_text <- repeat_header_continued
428 }
429 bottom_part <- paste0(
Hao Zhu44f6c492017-10-20 23:05:37 -0400430 "\\midrule\n",
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400431 "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400432 "\\endfoot\n",
Hao Zhuc83eb632017-10-10 14:11:08 -0400433 "\\bottomrule\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400434 "\\endlastfoot"
435 )
436 }
437 }
438
439 # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400440 x <- c(
441 x[1:header_rows_end],
442 "\\endfirsthead",
443 continue_line,
444 x[header_rows_start:header_rows_end],
445 "\\endhead",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400446 bottom_part,
Hao Zhua31e97f2017-06-08 14:55:41 -0400447 x[(header_rows_end + 1):length(x)]
448 )
449 x <- paste0(x, collapse = "\n")
450 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400451}
452
Hao Zhua3fc0c42017-02-27 12:04:59 -0500453styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400454 col_align <- as.character(factor(
455 table_info$align_vector, c("c", "l", "r"),
456 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
457 ))
458 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
459 col_align_vector <- col_align
460 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500461 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
462 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400463 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500464 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400465 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500466}
467
Hao Zhu9b051592019-01-07 18:13:51 -0500468styling_latex_position <- function(x, table_info, position, latex_options,
469 table.envir) {
newtuxd2867062017-09-29 00:19:27 -0400470 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhu064990d2017-10-17 18:08:42 -0400471 if (length(hold_position) == 0) hold_position <- ""
Hao Zhua3fc0c42017-02-27 12:04:59 -0500472 switch(
473 position,
Hao Zhu9b051592019-01-07 18:13:51 -0500474 center = styling_latex_position_center(x, table_info, hold_position,
475 table.envir),
Hao Zhua3fc0c42017-02-27 12:04:59 -0500476 left = styling_latex_position_left(x, table_info),
Hao Zhu9b051592019-01-07 18:13:51 -0500477 right = styling_latex_position_right(x, table_info, hold_position,
478 table.envir),
479 float_left = styling_latex_position_float(x, table_info, "l", table.envir),
480 float_right = styling_latex_position_float(x, table_info, "r", table.envir)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500481 )
482}
483
Hao Zhu9b051592019-01-07 18:13:51 -0500484styling_latex_position_center <- function(x, table_info, hold_position,
485 table.envir) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500486 if (!table_info$table_env & table_info$tabular == "tabular") {
Hao Zhu9b051592019-01-07 18:13:51 -0500487 x <- paste0("\\begin{", table.envir, "}\n\\centering", x,
488 "\n\\end{", table.envir, "}")
newtuxd2867062017-09-29 00:19:27 -0400489 if (hold_position == "hold_position") {
490 x <- styling_latex_hold_position(x)
491 } else {
492 x <- styling_latex_HOLD_position(x)
493 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500494 }
495 return(x)
496}
497
498styling_latex_position_left <- function(x, table_info) {
499 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
500 longtable_option <- "\\[l\\]"
501 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
502 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
503}
504
Hao Zhu9b051592019-01-07 18:13:51 -0500505styling_latex_position_right <- function(x, table_info, hold_position,
506 table.envir) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500507 warning("Position = right is only supported for longtable in LaTeX. ",
508 "Setting back to center...")
Hao Zhu9b051592019-01-07 18:13:51 -0500509 styling_latex_position_center(x, table_info, hold_position, table.envir)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500510}
511
Hao Zhu9b051592019-01-07 18:13:51 -0500512styling_latex_position_float <- function(x, table_info, option, table.envir) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500513 if (table_info$tabular == "longtable") {
514 warning("wraptable is not supported for longtable.")
515 if (option == "l") return(styling_latex_position_left(x, table_info))
Hao Zhu9b051592019-01-07 18:13:51 -0500516 if (option == "r") return(styling_latex_position_right(x, table_info, F,
517 table.envir))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500518 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500519 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
520 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500521 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500522 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400523 option <- paste0(option, "\\{0pt\\}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500524 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
525 x <- sub("\\\\begin\\{table\\}", option, x)
526 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500527 } else {
528 option <- sprintf("\\begin{wraptable}{%s}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400529 option <- paste0(option, "{0pt}")
Hao Zhuf7994dd2017-02-27 16:58:42 -0500530 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500531 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500532 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500533}
534
535styling_latex_font_size <- function(x, table_info, font_size) {
536 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400537 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500538 return(sub(table_info$begin_tabular,
539 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
540 "\\}\\\\selectfont\n", table_info$begin_tabular),
541 x))
542 }
543 # For longtable and tabular without table environment. Simple wrap around
544 # fontsize is good enough
545 return(paste0(
546 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
Hao Zhu76762d82018-07-25 20:56:52 -0400547 "\n\\endgroup{}"
Hao Zhua3fc0c42017-02-27 12:04:59 -0500548 ))
549}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400550
Hao Zhu245931c2017-09-01 22:43:56 -0400551styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400552 x <- sub(
553 paste0("begin\\{", current_env, "\\}\\[t\\]"),
554 paste0("begin\\{", latex_table_env, "\\}"), x
555 )
556 x <- sub(
557 paste0("begin\\{", current_env, "\\}"),
558 paste0("begin\\{", latex_table_env, "\\}"), x
559 )
560 x <- sub(
561 paste0("end\\{", current_env, "\\}"),
562 paste0("end\\{", latex_table_env, "\\}"), x
563 )
564 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400565}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400566