blob: 55a342c16b40e9d2a7a06700c402169987dcb271 [file] [log] [blame]
Hao Zhue10cfd32017-02-21 16:41:14 -05001#' HTML table attributes
2#'
Hao Zhu26234122017-02-22 15:34:33 -05003#' @description This function provides a cleaner approach to modify the style
Hao Zhue7c8f702017-10-10 13:22:59 -04004#' of HTML tables other than using the `table.attr` option in `knitr::kable()`. Note
5#' that those bootstrap options requires Twitter bootstrap theme, which is not avaiable
6#' in some customized template being loaded.
Hao Zhue10cfd32017-02-21 16:41:14 -05007#'
Hao Zhuf7994dd2017-02-27 16:58:42 -05008#' @param kable_input Output of `knitr::kable()` with `format` specified
9#' @param bootstrap_options A character vector for bootstrap table options.
Hao Zhu6a076462017-03-01 12:59:01 -050010#' Please see package vignette or visit the w3schools'
Hao Zhuf7994dd2017-02-27 16:58:42 -050011#' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page}
12#' for more information. Possible options include `basic`, `striped`,
13#' `bordered`, `hover`, `condensed` and `responsive`.
14#' @param latex_options A character vector for LaTeX table options. Please see
Hao Zhu6a076462017-03-01 12:59:01 -050015#' package vignette for more information. Possible options include
Rob Shepherdc59d15e2017-08-26 16:06:36 +010016#' `basic`, `striped`, `hold_position`, `HOLD_position`, `scale_down` & `repeat_header`.
Hao Zhu971d89f2017-06-07 19:22:47 -040017#' `striped` will add alternative row colors to the table. It will imports
18#' `LaTeX` package `xcolor` if enabled. `hold_position` will "hold" the floating
19#' table to the exact position. It is useful when the `LaTeX` table is contained
20#' in a `table` environment after you specified captions in `kable()`. It will
21#' force the table to stay in the position where it was created in the document.
Hao Zhu53e240f2017-09-04 20:04:29 -040022#' A stronger version: `HOLD_position` requires the `float` package and specifies `[H]`.
Hao Zhuf7994dd2017-02-27 16:58:42 -050023#' `scale_down` is useful for super wide table. It will automatically adjust
Hao Zhu971d89f2017-06-07 19:22:47 -040024#' the table to page width. `repeat_header` in only meaningful in a longtable
25#' environment. It will let the header row repeat on every page in that long
Salzerc53f0cd2018-02-23 06:38:34 -050026#' table.
Hao Zhu59f5fe02017-02-22 11:27:14 -050027#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhuf7994dd2017-02-27 16:58:42 -050028#' table should have 100\% width. Since HTML and pdf have different flavors on
29#' the preferable format for `full_width`. If not specified, a HTML table will
30#' have full width by default but this option will be set to `FALSE` for a
31#' LaTeX table
32#' @param position A character string determining how to position the table
33#' on a page. Possible values include `left`, `center`, `right`, `float_left`
34#' and `float_right`. Please see the package doc site for demonstrations. For
35#' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be
36#' imported.
Hao Zhu94956582017-02-21 18:18:29 -050037#' @param font_size A numeric input for table font size
Hao Zhu81eb9342018-12-11 10:38:52 -050038#' @param row_label_position A character string determining the justification
39#' of the row labels in a table. Possible values inclued `l` for left, `c` for
40#' center, and `r` for right. The default value is `l` for left justifcation.
41#' @param repeat_header_method LaTeX option, can either be `append`(default) or
42#' `replace`
43#' @param repeat_header_text LaTeX option. A text string you want to append on
44#' or replace the caption.
45#' @param stripe_color LaTeX option allowing users to pick a different color
46#' for their strip lines. This option is not available in HTML
47#' @param latex_table_env LaTeX option. A character string to define customized
48#' table environment such as tabu or tabularx.You shouldn't expect all features
49#' could be supported in self-defined environments.
Duncan Murdoch911dca32019-01-05 16:39:32 -050050#' @param protect_latex If `TRUE`, LaTeX code embedded between dollar signs
51#' will be protected from escaping. Has no effect unless HTML format
52#' is chosen: this is for including complicated math in HTML output.
Hao Zhue10cfd32017-02-21 16:41:14 -050053#'
antaldanielcf11dac2018-05-30 21:54:37 +020054#' @details For LaTeX, if you use other than English environment
55#' - all tables are converted to 'UTF-8'. If you use, for example, Hungarian
56#' characters on a Windows machine, make sure to use
57#' Sys.setlocale("LC_ALL","Hungarian") to avoid unexpected conversions.
58#'
Hao Zhu78e61222017-05-24 20:53:35 -040059#' @examples x_html <- knitr::kable(head(mtcars), "html")
60#' kable_styling(x_html, "striped", position = "left", font_size = 7)
61#'
62#' x_latex <- knitr::kable(head(mtcars), "latex")
63#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
64#'
Hao Zhue10cfd32017-02-21 16:41:14 -050065#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050066kable_styling <- function(kable_input,
67 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050068 latex_options = "basic",
69 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050070 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040071 font_size = NULL,
Salzerc53f0cd2018-02-23 06:38:34 -050072 row_label_position = "l",
Hao Zhu81eb9342018-12-11 10:38:52 -050073 repeat_header_text = "\\textit{(continued)}",
74 repeat_header_method = c("append", "replace"),
75 repeat_header_continued = FALSE,
76 stripe_color = "gray!6",
Duncan Murdoch911dca32019-01-05 16:39:32 -050077 latex_table_env = NULL,
78 protect_latex = TRUE) {
Hao Zhu9b45a182017-02-27 18:17:46 -050079
80 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
81 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
82 }
83 if (length(latex_options) == 1 && latex_options == "basic") {
84 latex_options <- getOption("kable_styling_latex_options", "basic")
85 }
86 if (position == "center") {
87 position <- getOption("kable_styling_position", "center")
88 }
89 position <- match.arg(position,
90 c("center", "left", "right", "float_left", "float_right"))
91 if (is.null(font_size)) {
92 font_size <- getOption("kable_styling_font_size", NULL)
93 }
94
Hao Zhuc1f38412017-02-23 12:13:48 -050095 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050096
Hao Zhuc1f38412017-02-23 12:13:48 -050097 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050098 warning("Please specify format in kable. kableExtra can customize either ",
99 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
100 "for details.")
101 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -0500102 }
103 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500104 if (is.null(full_width)) {
105 full_width <- getOption("kable_styling_full_width", T)
106 }
Hao Zhuc1f38412017-02-23 12:13:48 -0500107 return(htmlTable_styling(kable_input,
108 bootstrap_options = bootstrap_options,
109 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -0500110 position = position,
Duncan Murdoch911dca32019-01-05 16:39:32 -0500111 font_size = font_size,
112 protect_latex = protect_latex))
Hao Zhuc1f38412017-02-23 12:13:48 -0500113 }
114 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500115 if (is.null(full_width)) {
116 full_width <- getOption("kable_styling_full_width", F)
117 }
Hao Zhube1e0552018-12-12 14:10:43 -0500118 repeat_header_method <- match.arg(repeat_header_method)
Hao Zhuc05e1812017-02-25 01:45:35 -0500119 return(pdfTable_styling(kable_input,
120 latex_options = latex_options,
121 full_width = full_width,
122 position = position,
Salzerc53f0cd2018-02-23 06:38:34 -0500123 font_size = font_size,
124 row_label_position = row_label_position,
Hao Zhu81eb9342018-12-11 10:38:52 -0500125 repeat_header_text = repeat_header_text,
126 repeat_header_method = repeat_header_method,
127 repeat_header_continued = repeat_header_continued,
128 stripe_color = stripe_color,
129 latex_table_env = latex_table_env))
Hao Zhuc1f38412017-02-23 12:13:48 -0500130 }
131}
132
Duncan Murdoch911dca32019-01-05 16:39:32 -0500133extract_latex_from_kable <- function(kable_input) {
134 kable_attrs <- attributes(kable_input)
135 regexp <- "(^|[^\\\\])([$][^$]*[^$\\\\]+[$]|[$][$][^$]*[^$\\\\]+[$][$])"
136 latex <- character()
137 while (grepl(regexp, kable_input)) {
Duncan Murdochc3e1a3b2019-01-05 18:21:27 -0500138 block <- str_replace(str_extract(kable_input, regexp),
139 regexp,
140 "\\2")
141
Duncan Murdoch911dca32019-01-05 16:39:32 -0500142 name <- paste0("latex", digest(block))
143 latex[name] <- block
Duncan Murdochc3e1a3b2019-01-05 18:21:27 -0500144 kable_input <- str_replace(kable_input, regexp, paste0("\\1", name))
Duncan Murdoch911dca32019-01-05 16:39:32 -0500145 }
146 kable_attrs$extracted_latex <- latex
147 attributes(kable_input) <- kable_attrs
148 kable_input
149}
150
151replace_latex_in_kable <- function(kable_input, latex) {
152 kable_attrs <- attributes(kable_input)
153 for (n in names(latex)) {
154 kable_input <- str_replace_all(kable_input, fixed(n), latex[n])
155 }
156 attributes(kable_input) <- kable_attrs
157 kable_input
158}
159
Hao Zhuc1f38412017-02-23 12:13:48 -0500160# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500161htmlTable_styling <- function(kable_input,
162 bootstrap_options = "basic",
163 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500164 position = c("center", "left", "right",
165 "float_left", "float_right"),
Duncan Murdoch911dca32019-01-05 16:39:32 -0500166 font_size = NULL,
167 protect_latex = TRUE) {
168 if (protect_latex) {
169 kable_input <- extract_latex_from_kable(kable_input)
170 }
Hao Zhu909ea382017-06-12 15:43:47 -0400171 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400172 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500173
174 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500175 bootstrap_options <- match.arg(
176 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500177 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500178 several.ok = T
179 )
180
Hao Zhu26234122017-02-22 15:34:33 -0500181 kable_xml_class <- NULL
182 if (xml_has_attr(kable_xml, "class")) {
183 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500184 }
Hao Zhu26234122017-02-22 15:34:33 -0500185 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
186 bootstrap_options <- "table"
187 } else {
188 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
189 bootstrap_options <- paste0("table-", bootstrap_options)
190 bootstrap_options <- c("table", bootstrap_options)
191 }
192 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
193 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500194
Hao Zhu26234122017-02-22 15:34:33 -0500195 # Modify style
196 kable_xml_style <- NULL
197 if (xml_has_attr(kable_xml, "style")) {
198 kable_xml_style <- xml_attr(kable_xml, "style")
199 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500200 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500201 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500202 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400203 kable_caption_node <- xml_tpart(kable_xml, "caption")
204 if (!is.null(kable_caption_node)) {
205 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
206 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500207 }
208 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500209 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500210 }
Hao Zhu94956582017-02-21 18:18:29 -0500211
Hao Zhuc05e1812017-02-25 01:45:35 -0500212 position <- match.arg(position)
213 position_style <- switch(
214 position,
215 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400216 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500217 right = "margin-right: 0; margin-left: auto",
218 float_left = "float: left; margin-right: 10px;",
219 float_right = "float: right; margin-left: 10px;"
220 )
221 kable_xml_style <- c(kable_xml_style, position_style)
222
Hao Zhu26234122017-02-22 15:34:33 -0500223 if (length(kable_xml_style) != 0) {
224 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500225 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500226
Hao Zhuf2dfd142017-07-24 14:43:28 -0400227 out <- as_kable_xml(kable_xml)
Duncan Murdoch911dca32019-01-05 16:39:32 -0500228 if (protect_latex) {
229 out <- replace_latex_in_kable(out, kable_attrs$extracted_latex)
230 kable_attrs$extracted_latex <- NULL
231 }
Hao Zhu909ea382017-06-12 15:43:47 -0400232 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500233 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu9b45a182017-02-27 18:17:46 -0500234 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500235}
Hao Zhuc1f38412017-02-23 12:13:48 -0500236
Hao Zhube1e0552018-12-12 14:10:43 -0500237# LaTeX table style ------------
Hao Zhuc1f38412017-02-23 12:13:48 -0500238pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500239 latex_options = "basic",
Hao Zhube1e0552018-12-12 14:10:43 -0500240 full_width = FALSE,
Hao Zhu81eb9342018-12-11 10:38:52 -0500241 position,
242 font_size,
243 row_label_position,
244 repeat_header_text,
245 repeat_header_method,
246 repeat_header_continued,
247 stripe_color,
248 latex_table_env) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500249
Hao Zhuc05e1812017-02-25 01:45:35 -0500250 latex_options <- match.arg(
251 latex_options,
Salzerc53f0cd2018-02-23 06:38:34 -0500252 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500253 several.ok = T
254 )
255
Hao Zhua3fc0c42017-02-27 12:04:59 -0500256 out <- NULL
Hao Zhu3fc0e882018-04-03 16:06:41 -0400257 out <- solve_enc(kable_input)
258
Hao Zhuc05e1812017-02-25 01:45:35 -0500259 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500260
261 if ("striped" %in% latex_options) {
Hao Zhu71a224f2017-08-18 11:06:22 -0400262 out <- styling_latex_striped(out, table_info, stripe_color)
Hao Zhuc05e1812017-02-25 01:45:35 -0500263 }
264
265 # hold_position is only meaningful in a table environment
266 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500267 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500268 }
269
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100270 # HOLD_position is only meaningful in a table environment
271 if ("HOLD_position" %in% latex_options & table_info$table_env) {
272 out <- styling_latex_HOLD_position(out)
273 }
274
Hao Zhua3fc0c42017-02-27 12:04:59 -0500275 if ("scale_down" %in% latex_options) {
276 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500277 }
278
Hao Zhu971d89f2017-06-07 19:22:47 -0400279 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400280 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400281 repeat_header_method, repeat_header_continued)
Hao Zhu971d89f2017-06-07 19:22:47 -0400282 }
283
Hao Zhuc05e1812017-02-25 01:45:35 -0500284 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400285 latex_table_env <- ifelse(table_info$tabular == "longtable",
286 "longtabu", "tabu")
287 full_width_return <- styling_latex_full_width(out, table_info)
288 out <- full_width_return[[1]]
289 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500290 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500291
Hao Zhua3fc0c42017-02-27 12:04:59 -0500292 if (!is.null(font_size)) {
293 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500294 }
295
Hao Zhu245931c2017-09-01 22:43:56 -0400296 if (!is.null(latex_table_env)) {
297 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
298 table_info$tabular <- latex_table_env
299 table_info$begin_tabular <- sub("tabular", latex_table_env,
300 table_info$begin_tabular)
301 table_info$end_tabular <- sub("tabular", latex_table_env,
302 table_info$end_tabular)
303 }
304
Hao Zhua3fc0c42017-02-27 12:04:59 -0500305 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500306
307 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400308 attr(out, "kable_meta") <- table_info
bsalzer74e2e222018-02-12 17:23:12 -0500309
Salzerc53f0cd2018-02-23 06:38:34 -0500310 if (row_label_position != "l") {
Hao Zhu81eb9342018-12-11 10:38:52 -0500311 if (table_info$tabular == "longtable") {
bsalzer74e2e222018-02-12 17:23:12 -0500312 out <- sub("\\\\begin\\{longtable\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500313 paste0("\\\\begin\\{longtable\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500314 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500315 out)
316 } else {
317 out <- sub("\\\\begin\\{tabular\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500318 paste0("\\\\begin\\{tabular\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500319 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500320 out)
321 }
322 }
323
Hao Zhuc05e1812017-02-25 01:45:35 -0500324 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500325}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500326
Hao Zhue1be9602017-08-17 15:44:31 -0400327styling_latex_striped <- function(x, table_info, color) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400328 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
329 if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400330 row_color <- sprintf("\\rowcolors{%s}{white}{%s}",
331 1 + table_info$position_offset, color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400332 } else {
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400333 if (table_info$position_offset == 0) {
Leo83f05132018-05-10 14:12:16 +0800334 row_color <- sprintf("\\rowcolors{1}{white}{%s}", color)
335 } else {
336 row_color <- sprintf("\\rowcolors{2}{%s}{white}", color)
337 }
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400338 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400339
340 x <- read_lines(x)
341 if (table_info$booktabs) {
Hao Zhu76f0eb62018-09-15 12:38:33 -0400342 header_rows_start <- which(trimws(x) == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800343 if (is.null(table_info$colnames)) {
344 header_rows_end <- header_rows_start
345 } else {
Hao Zhu76f0eb62018-09-15 12:38:33 -0400346 header_rows_end <- which(trimws(x) == "\\midrule")[1]
Leo83f05132018-05-10 14:12:16 +0800347 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400348 } else {
Hao Zhu76f0eb62018-09-15 12:38:33 -0400349 header_rows_start <- which(trimws(x) == "\\hline")[1]
350 header_rows_end <- which(trimws(x) == "\\hline")[2]
Hao Zhu00ba87c2017-08-01 12:42:58 -0400351 }
352
353 x <- c(
354 row_color,
355 x[1:(header_rows_start - 1)],
356 "\\hiderowcolors",
357 x[header_rows_start:header_rows_end],
358 "\\showrowcolors",
359 x[(header_rows_end + 1):length(x)],
360 "\\rowcolors{2}{white}{white}"
361 )
362 x <- paste0(x, collapse = "\n")
363 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500364}
365
366styling_latex_hold_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400367 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
368 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[!h]")
369 } else {
370 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]")
371 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500372}
373
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100374styling_latex_HOLD_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400375 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
376 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[H]")
377 } else {
378 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]")
379 }
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100380}
381
Hao Zhua3fc0c42017-02-27 12:04:59 -0500382styling_latex_scale_down <- function(x, table_info) {
383 # You cannot put longtable in a resizebox
384 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
385 if (table_info$tabular == "longtable") {
386 warning("Longtable cannot be resized.")
387 return(x)
388 }
389 x <- sub(table_info$begin_tabular,
Hao Zhuae80df42018-04-12 15:45:11 -0400390 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{\n",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500391 table_info$begin_tabular),
392 x)
393 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
394}
395
Hao Zhuca211db2017-07-25 00:06:43 -0400396styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400397 repeat_header_method,
398 repeat_header_continued) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400399 x <- read_lines(x)
400 if (table_info$booktabs) {
401 header_rows_start <- which(x == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800402 if (is.null(table_info$colnames)) {
403 header_rows_end <- header_rows_start
404 } else {
405 header_rows_end <- which(x == "\\midrule")[1]
406 }
Hao Zhua31e97f2017-06-08 14:55:41 -0400407 } else {
408 header_rows_start <- which(x == "\\hline")[1]
409 header_rows_end <- which(x == "\\hline")[2]
410 }
Hao Zhufdec1842017-06-08 17:06:04 -0400411
412 if (is.na(table_info$caption)) {
413 continue_line <- paste0(
414 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
415 "}\\\\"
416 )
417 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400418 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400419 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400420 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400421 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400422 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400423 }
424
Hao Zhu6c9714a2017-10-02 14:52:45 -0400425 if (!table_info$booktabs) {
426 bottom_part <- NULL
427 } else {
428 index_bottomrule <- which(x == "\\bottomrule")
429 x <- x[-index_bottomrule]
Hao Zhu20784e72017-10-10 14:48:33 -0400430 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*")
Hao Zhu6c9714a2017-10-02 14:52:45 -0400431
432 if (repeat_header_continued == FALSE) {
Hao Zhu44f6c492017-10-20 23:05:37 -0400433 bottom_part <- "\\\n\\endfoot\n\\bottomrule\n\\endlastfoot"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400434 } else {
435 if (repeat_header_continued == TRUE) {
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400436 bottom_text <- "\\textit{(continued \\ldots)}"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400437 } else {
438 bottom_text <- repeat_header_continued
439 }
440 bottom_part <- paste0(
Hao Zhu44f6c492017-10-20 23:05:37 -0400441 "\\midrule\n",
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400442 "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400443 "\\endfoot\n",
Hao Zhuc83eb632017-10-10 14:11:08 -0400444 "\\bottomrule\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400445 "\\endlastfoot"
446 )
447 }
448 }
449
450 # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400451 x <- c(
452 x[1:header_rows_end],
453 "\\endfirsthead",
454 continue_line,
455 x[header_rows_start:header_rows_end],
456 "\\endhead",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400457 bottom_part,
Hao Zhua31e97f2017-06-08 14:55:41 -0400458 x[(header_rows_end + 1):length(x)]
459 )
460 x <- paste0(x, collapse = "\n")
461 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400462}
463
Hao Zhua3fc0c42017-02-27 12:04:59 -0500464styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400465 col_align <- as.character(factor(
466 table_info$align_vector, c("c", "l", "r"),
467 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
468 ))
469 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
470 col_align_vector <- col_align
471 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500472 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
473 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400474 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500475 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400476 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500477}
478
479styling_latex_position <- function(x, table_info, position, latex_options) {
newtuxd2867062017-09-29 00:19:27 -0400480 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhu064990d2017-10-17 18:08:42 -0400481 if (length(hold_position) == 0) hold_position <- ""
Hao Zhua3fc0c42017-02-27 12:04:59 -0500482 switch(
483 position,
484 center = styling_latex_position_center(x, table_info, hold_position),
485 left = styling_latex_position_left(x, table_info),
486 right = styling_latex_position_right(x, table_info, hold_position),
487 float_left = styling_latex_position_float(x, table_info, "l"),
488 float_right = styling_latex_position_float(x, table_info, "r")
489 )
490}
491
492styling_latex_position_center <- function(x, table_info, hold_position) {
493 if (!table_info$table_env & table_info$tabular == "tabular") {
newtuxd2867062017-09-29 00:19:27 -0400494 x <- paste0("\\begin{table}\n\\centering", x, "\n\\end{table}")
495 if (hold_position == "hold_position") {
496 x <- styling_latex_hold_position(x)
497 } else {
498 x <- styling_latex_HOLD_position(x)
499 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500500 }
501 return(x)
502}
503
504styling_latex_position_left <- function(x, table_info) {
505 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
506 longtable_option <- "\\[l\\]"
507 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
508 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
509}
510
511styling_latex_position_right <- function(x, table_info, hold_position) {
512 warning("Position = right is only supported for longtable in LaTeX. ",
513 "Setting back to center...")
514 styling_latex_position_center(x, table_info, hold_position)
515}
516
517styling_latex_position_float <- function(x, table_info, option) {
518 if (table_info$tabular == "longtable") {
519 warning("wraptable is not supported for longtable.")
520 if (option == "l") return(styling_latex_position_left(x, table_info))
521 if (option == "r") return(styling_latex_position_right(x, table_info, F))
522 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500523 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
524 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500525 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500526 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400527 option <- paste0(option, "\\{0pt\\}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500528 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
529 x <- sub("\\\\begin\\{table\\}", option, x)
530 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500531 } else {
532 option <- sprintf("\\begin{wraptable}{%s}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400533 option <- paste0(option, "{0pt}")
Hao Zhuf7994dd2017-02-27 16:58:42 -0500534 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500535 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500536 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500537}
538
539styling_latex_font_size <- function(x, table_info, font_size) {
540 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400541 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500542 return(sub(table_info$begin_tabular,
543 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
544 "\\}\\\\selectfont\n", table_info$begin_tabular),
545 x))
546 }
547 # For longtable and tabular without table environment. Simple wrap around
548 # fontsize is good enough
549 return(paste0(
550 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
Hao Zhu76762d82018-07-25 20:56:52 -0400551 "\n\\endgroup{}"
Hao Zhua3fc0c42017-02-27 12:04:59 -0500552 ))
553}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400554
Hao Zhu245931c2017-09-01 22:43:56 -0400555styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400556 x <- sub(
557 paste0("begin\\{", current_env, "\\}\\[t\\]"),
558 paste0("begin\\{", latex_table_env, "\\}"), x
559 )
560 x <- sub(
561 paste0("begin\\{", current_env, "\\}"),
562 paste0("begin\\{", latex_table_env, "\\}"), x
563 )
564 x <- sub(
565 paste0("end\\{", current_env, "\\}"),
566 paste0("end\\{", latex_table_env, "\\}"), x
567 )
568 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400569}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400570