blob: 5dc7c25f8f39d9a6aba1554a581d6f479ec9fc42 [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
Salzerc53f0cd2018-02-23 06:38:34 -050038#' @param row_label_position A character string determining the justification of the row
39#' labels in a table. Possible values inclued `l` for left, `c` for center, and `r` for
40#' right. The default value is `l` for left justifcation.
Hao Zhue1be9602017-08-17 15:44:31 -040041#' @param ... extra options for HTML or LaTeX. See `details`.
42#'
43#' @details For LaTeX, extra options includes:
44#' - `repeat_header_method` can either be `append`(default) or `replace`
45#' - `repeat_header_text` is just a text string you want to append on or
46#' replace the caption.
Hao Zhu71a224f2017-08-18 11:06:22 -040047#' - `stripe_color` allows users to pick a different color for their strip lines.
Hao Zhu245931c2017-09-01 22:43:56 -040048#' - `latex_table_env` character string to define customized table environment
49#' such as tabu or tabularx.You shouldn't expect all features could be
50#' supported in self-defined environments.
Hao Zhue10cfd32017-02-21 16:41:14 -050051#'
Hao Zhu78e61222017-05-24 20:53:35 -040052#' @examples x_html <- knitr::kable(head(mtcars), "html")
53#' kable_styling(x_html, "striped", position = "left", font_size = 7)
54#'
55#' x_latex <- knitr::kable(head(mtcars), "latex")
56#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
57#'
Hao Zhue10cfd32017-02-21 16:41:14 -050058#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050059kable_styling <- function(kable_input,
60 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050061 latex_options = "basic",
62 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050063 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040064 font_size = NULL,
Salzerc53f0cd2018-02-23 06:38:34 -050065 row_label_position = "l",
Hao Zhua31e97f2017-06-08 14:55:41 -040066 ...) {
Hao Zhu9b45a182017-02-27 18:17:46 -050067
68 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
69 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
70 }
71 if (length(latex_options) == 1 && latex_options == "basic") {
72 latex_options <- getOption("kable_styling_latex_options", "basic")
73 }
74 if (position == "center") {
75 position <- getOption("kable_styling_position", "center")
76 }
77 position <- match.arg(position,
78 c("center", "left", "right", "float_left", "float_right"))
79 if (is.null(font_size)) {
80 font_size <- getOption("kable_styling_font_size", NULL)
81 }
82
Hao Zhuc1f38412017-02-23 12:13:48 -050083 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050084
Hao Zhuc1f38412017-02-23 12:13:48 -050085 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050086 warning("Please specify format in kable. kableExtra can customize either ",
87 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
88 "for details.")
89 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050090 }
91 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050092 if (is.null(full_width)) {
93 full_width <- getOption("kable_styling_full_width", T)
94 }
Salzerc53f0cd2018-02-23 06:38:34 -050095 if(!missing(row_label_position)) {
96 warning("'row_label_position' is not active for HTML tables yet and parameter will not be used.")
97 }
Hao Zhuc1f38412017-02-23 12:13:48 -050098 return(htmlTable_styling(kable_input,
99 bootstrap_options = bootstrap_options,
100 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -0500101 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -0400102 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -0500103 }
104 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -0500105 if (is.null(full_width)) {
106 full_width <- getOption("kable_styling_full_width", F)
107 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500108 return(pdfTable_styling(kable_input,
109 latex_options = latex_options,
110 full_width = full_width,
111 position = position,
Salzerc53f0cd2018-02-23 06:38:34 -0500112 font_size = font_size,
113 row_label_position = row_label_position,
114 ...))
Hao Zhuc1f38412017-02-23 12:13:48 -0500115 }
116}
117
118# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500119htmlTable_styling <- function(kable_input,
120 bootstrap_options = "basic",
121 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500122 position = c("center", "left", "right",
123 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -0500124 font_size = NULL) {
Hao Zhu909ea382017-06-12 15:43:47 -0400125 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400126 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500127
128 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500129 bootstrap_options <- match.arg(
130 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500131 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500132 several.ok = T
133 )
134
Hao Zhu26234122017-02-22 15:34:33 -0500135 kable_xml_class <- NULL
136 if (xml_has_attr(kable_xml, "class")) {
137 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500138 }
Hao Zhu26234122017-02-22 15:34:33 -0500139 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
140 bootstrap_options <- "table"
141 } else {
142 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
143 bootstrap_options <- paste0("table-", bootstrap_options)
144 bootstrap_options <- c("table", bootstrap_options)
145 }
146 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
147 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500148
Hao Zhu26234122017-02-22 15:34:33 -0500149 # Modify style
150 kable_xml_style <- NULL
151 if (xml_has_attr(kable_xml, "style")) {
152 kable_xml_style <- xml_attr(kable_xml, "style")
153 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500154 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500155 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500156 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400157 kable_caption_node <- xml_tpart(kable_xml, "caption")
158 if (!is.null(kable_caption_node)) {
159 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
160 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500161 }
162 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500163 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500164 }
Hao Zhu94956582017-02-21 18:18:29 -0500165
Hao Zhuc05e1812017-02-25 01:45:35 -0500166 position <- match.arg(position)
167 position_style <- switch(
168 position,
169 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400170 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500171 right = "margin-right: 0; margin-left: auto",
172 float_left = "float: left; margin-right: 10px;",
173 float_right = "float: right; margin-left: 10px;"
174 )
175 kable_xml_style <- c(kable_xml_style, position_style)
176
Hao Zhu26234122017-02-22 15:34:33 -0500177 if (length(kable_xml_style) != 0) {
178 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500179 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500180
Hao Zhuf2dfd142017-07-24 14:43:28 -0400181 out <- as_kable_xml(kable_xml)
Hao Zhu909ea382017-06-12 15:43:47 -0400182 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500183 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu9b45a182017-02-27 18:17:46 -0500184 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500185}
Hao Zhuc1f38412017-02-23 12:13:48 -0500186
187# LaTeX table style
188pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500189 latex_options = "basic",
190 full_width = F,
191 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500192 "float_left", "float_right"),
Hao Zhua31e97f2017-06-08 14:55:41 -0400193 font_size = NULL,
Hao Zhuca211db2017-07-25 00:06:43 -0400194 repeat_header_text = "\\textit{(continued)}",
Hao Zhue1be9602017-08-17 15:44:31 -0400195 repeat_header_method = c("append", "replace"),
Hao Zhu6c9714a2017-10-02 14:52:45 -0400196 repeat_header_continued = FALSE,
Hao Zhu245931c2017-09-01 22:43:56 -0400197 stripe_color = "gray!6",
Salzerc53f0cd2018-02-23 06:38:34 -0500198 latex_table_env = NULL,
199 row_label_position) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500200
Hao Zhuc05e1812017-02-25 01:45:35 -0500201 latex_options <- match.arg(
202 latex_options,
Salzerc53f0cd2018-02-23 06:38:34 -0500203 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500204 several.ok = T
205 )
206
Hao Zhuca211db2017-07-25 00:06:43 -0400207 repeat_header_method <- match.arg(repeat_header_method)
208
Hao Zhua3fc0c42017-02-27 12:04:59 -0500209 out <- NULL
Hao Zhu3fc0e882018-04-03 16:06:41 -0400210 out <- solve_enc(kable_input)
211
Hao Zhuc05e1812017-02-25 01:45:35 -0500212 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500213
214 if ("striped" %in% latex_options) {
Hao Zhu71a224f2017-08-18 11:06:22 -0400215 out <- styling_latex_striped(out, table_info, stripe_color)
Hao Zhuc05e1812017-02-25 01:45:35 -0500216 }
217
218 # hold_position is only meaningful in a table environment
219 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500220 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500221 }
222
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100223 # HOLD_position is only meaningful in a table environment
224 if ("HOLD_position" %in% latex_options & table_info$table_env) {
225 out <- styling_latex_HOLD_position(out)
226 }
227
Hao Zhua3fc0c42017-02-27 12:04:59 -0500228 if ("scale_down" %in% latex_options) {
229 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500230 }
231
Hao Zhu971d89f2017-06-07 19:22:47 -0400232 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400233 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400234 repeat_header_method, repeat_header_continued)
Hao Zhu971d89f2017-06-07 19:22:47 -0400235 }
236
Hao Zhuc05e1812017-02-25 01:45:35 -0500237 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400238 latex_table_env <- ifelse(table_info$tabular == "longtable",
239 "longtabu", "tabu")
240 full_width_return <- styling_latex_full_width(out, table_info)
241 out <- full_width_return[[1]]
242 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500243 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500244
Hao Zhua3fc0c42017-02-27 12:04:59 -0500245 if (!is.null(font_size)) {
246 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500247 }
248
Hao Zhu245931c2017-09-01 22:43:56 -0400249 if (!is.null(latex_table_env)) {
250 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
251 table_info$tabular <- latex_table_env
252 table_info$begin_tabular <- sub("tabular", latex_table_env,
253 table_info$begin_tabular)
254 table_info$end_tabular <- sub("tabular", latex_table_env,
255 table_info$end_tabular)
256 }
257
Hao Zhuc05e1812017-02-25 01:45:35 -0500258 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500259 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500260
261 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400262 attr(out, "kable_meta") <- table_info
bsalzer74e2e222018-02-12 17:23:12 -0500263
Salzerc53f0cd2018-02-23 06:38:34 -0500264 if (row_label_position != "l") {
bsalzer74e2e222018-02-12 17:23:12 -0500265 if(table_info$tabular=="longtable") {
266 out <- sub("\\\\begin\\{longtable\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500267 paste0("\\\\begin\\{longtable\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500268 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500269 out)
270 } else {
271 out <- sub("\\\\begin\\{tabular\\}\\{l",
bsalzer86692132018-02-13 19:50:05 -0500272 paste0("\\\\begin\\{tabular\\}\\{",
Salzerc53f0cd2018-02-23 06:38:34 -0500273 row_label_position),
bsalzer74e2e222018-02-12 17:23:12 -0500274 out)
275 }
276 }
277
Hao Zhuc05e1812017-02-25 01:45:35 -0500278 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500279}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500280
Hao Zhue1be9602017-08-17 15:44:31 -0400281styling_latex_striped <- function(x, table_info, color) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400282 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
283 if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400284 row_color <- sprintf("\\rowcolors{%s}{white}{%s}",
285 1 + table_info$position_offset, color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400286 } else {
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400287 if (table_info$position_offset == 0) {
Leo83f05132018-05-10 14:12:16 +0800288 row_color <- sprintf("\\rowcolors{1}{white}{%s}", color)
289 } else {
290 row_color <- sprintf("\\rowcolors{2}{%s}{white}", color)
291 }
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400292 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400293
294 x <- read_lines(x)
295 if (table_info$booktabs) {
296 header_rows_start <- which(x == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800297 if (is.null(table_info$colnames)) {
298 header_rows_end <- header_rows_start
299 } else {
300 header_rows_end <- which(x == "\\midrule")[1]
301 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400302 } else {
303 header_rows_start <- which(x == "\\hline")[1]
304 header_rows_end <- which(x == "\\hline")[2]
305 }
306
307 x <- c(
308 row_color,
309 x[1:(header_rows_start - 1)],
310 "\\hiderowcolors",
311 x[header_rows_start:header_rows_end],
312 "\\showrowcolors",
313 x[(header_rows_end + 1):length(x)],
314 "\\rowcolors{2}{white}{white}"
315 )
316 x <- paste0(x, collapse = "\n")
317 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500318}
319
320styling_latex_hold_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400321 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
322 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[!h]")
323 } else {
324 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]")
325 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500326}
327
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100328styling_latex_HOLD_position <- function(x) {
Hao Zhu164c48d2018-03-19 15:46:51 -0400329 if (str_detect(x, "\\\\begin\\{table\\}\\[t\\]")) {
330 str_replace(x, "\\\\begin\\{table\\}\\[t\\]", "\\\\begin\\{table\\}[H]")
331 } else {
332 str_replace(x, "\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]")
333 }
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100334}
335
Hao Zhua3fc0c42017-02-27 12:04:59 -0500336styling_latex_scale_down <- function(x, table_info) {
337 # You cannot put longtable in a resizebox
338 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
339 if (table_info$tabular == "longtable") {
340 warning("Longtable cannot be resized.")
341 return(x)
342 }
343 x <- sub(table_info$begin_tabular,
Hao Zhuae80df42018-04-12 15:45:11 -0400344 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{\n",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500345 table_info$begin_tabular),
346 x)
347 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
348}
349
Hao Zhuca211db2017-07-25 00:06:43 -0400350styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400351 repeat_header_method,
352 repeat_header_continued) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400353 x <- read_lines(x)
354 if (table_info$booktabs) {
355 header_rows_start <- which(x == "\\toprule")[1]
Leo83f05132018-05-10 14:12:16 +0800356 if (is.null(table_info$colnames)) {
357 header_rows_end <- header_rows_start
358 } else {
359 header_rows_end <- which(x == "\\midrule")[1]
360 }
Hao Zhua31e97f2017-06-08 14:55:41 -0400361 } else {
362 header_rows_start <- which(x == "\\hline")[1]
363 header_rows_end <- which(x == "\\hline")[2]
364 }
Hao Zhufdec1842017-06-08 17:06:04 -0400365
366 if (is.na(table_info$caption)) {
367 continue_line <- paste0(
368 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
369 "}\\\\"
370 )
371 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400372 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400373 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400374 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400375 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400376 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400377 }
378
Hao Zhu6c9714a2017-10-02 14:52:45 -0400379 if (!table_info$booktabs) {
380 bottom_part <- NULL
381 } else {
382 index_bottomrule <- which(x == "\\bottomrule")
383 x <- x[-index_bottomrule]
Hao Zhu20784e72017-10-10 14:48:33 -0400384 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*")
Hao Zhu6c9714a2017-10-02 14:52:45 -0400385
386 if (repeat_header_continued == FALSE) {
Hao Zhu44f6c492017-10-20 23:05:37 -0400387 bottom_part <- "\\\n\\endfoot\n\\bottomrule\n\\endlastfoot"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400388 } else {
389 if (repeat_header_continued == TRUE) {
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400390 bottom_text <- "\\textit{(continued \\ldots)}"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400391 } else {
392 bottom_text <- repeat_header_continued
393 }
394 bottom_part <- paste0(
Hao Zhu44f6c492017-10-20 23:05:37 -0400395 "\\midrule\n",
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400396 "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400397 "\\endfoot\n",
Hao Zhuc83eb632017-10-10 14:11:08 -0400398 "\\bottomrule\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400399 "\\endlastfoot"
400 )
401 }
402 }
403
404 # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400405 x <- c(
406 x[1:header_rows_end],
407 "\\endfirsthead",
408 continue_line,
409 x[header_rows_start:header_rows_end],
410 "\\endhead",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400411 bottom_part,
Hao Zhua31e97f2017-06-08 14:55:41 -0400412 x[(header_rows_end + 1):length(x)]
413 )
414 x <- paste0(x, collapse = "\n")
415 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400416}
417
Hao Zhua3fc0c42017-02-27 12:04:59 -0500418styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400419 col_align <- as.character(factor(
420 table_info$align_vector, c("c", "l", "r"),
421 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
422 ))
423 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
424 col_align_vector <- col_align
425 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500426 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
427 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400428 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500429 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400430 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500431}
432
433styling_latex_position <- function(x, table_info, position, latex_options) {
newtuxd2867062017-09-29 00:19:27 -0400434 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhu064990d2017-10-17 18:08:42 -0400435 if (length(hold_position) == 0) hold_position <- ""
Hao Zhua3fc0c42017-02-27 12:04:59 -0500436 switch(
437 position,
438 center = styling_latex_position_center(x, table_info, hold_position),
439 left = styling_latex_position_left(x, table_info),
440 right = styling_latex_position_right(x, table_info, hold_position),
441 float_left = styling_latex_position_float(x, table_info, "l"),
442 float_right = styling_latex_position_float(x, table_info, "r")
443 )
444}
445
446styling_latex_position_center <- function(x, table_info, hold_position) {
447 if (!table_info$table_env & table_info$tabular == "tabular") {
newtuxd2867062017-09-29 00:19:27 -0400448 x <- paste0("\\begin{table}\n\\centering", x, "\n\\end{table}")
449 if (hold_position == "hold_position") {
450 x <- styling_latex_hold_position(x)
451 } else {
452 x <- styling_latex_HOLD_position(x)
453 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500454 }
455 return(x)
456}
457
458styling_latex_position_left <- function(x, table_info) {
459 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
460 longtable_option <- "\\[l\\]"
461 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
462 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
463}
464
465styling_latex_position_right <- function(x, table_info, hold_position) {
466 warning("Position = right is only supported for longtable in LaTeX. ",
467 "Setting back to center...")
468 styling_latex_position_center(x, table_info, hold_position)
469}
470
471styling_latex_position_float <- function(x, table_info, option) {
472 if (table_info$tabular == "longtable") {
473 warning("wraptable is not supported for longtable.")
474 if (option == "l") return(styling_latex_position_left(x, table_info))
475 if (option == "r") return(styling_latex_position_right(x, table_info, F))
476 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500477 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
478 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500479 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500480 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400481 option <- paste0(option, "\\{0pt\\}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500482 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
483 x <- sub("\\\\begin\\{table\\}", option, x)
484 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500485 } else {
486 option <- sprintf("\\begin{wraptable}{%s}", option)
Hao Zhua2cb2312018-05-12 22:27:51 -0400487 option <- paste0(option, "{0pt}")
Hao Zhuf7994dd2017-02-27 16:58:42 -0500488 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500489 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500490 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500491}
492
493styling_latex_font_size <- function(x, table_info, font_size) {
494 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400495 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500496 return(sub(table_info$begin_tabular,
497 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
498 "\\}\\\\selectfont\n", table_info$begin_tabular),
499 x))
500 }
501 # For longtable and tabular without table environment. Simple wrap around
502 # fontsize is good enough
503 return(paste0(
504 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
Hao Zhu76762d82018-07-25 20:56:52 -0400505 "\n\\endgroup{}"
Hao Zhua3fc0c42017-02-27 12:04:59 -0500506 ))
507}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400508
Hao Zhu245931c2017-09-01 22:43:56 -0400509styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400510 x <- sub(
511 paste0("begin\\{", current_env, "\\}\\[t\\]"),
512 paste0("begin\\{", latex_table_env, "\\}"), x
513 )
514 x <- sub(
515 paste0("begin\\{", current_env, "\\}"),
516 paste0("begin\\{", latex_table_env, "\\}"), x
517 )
518 x <- sub(
519 paste0("end\\{", current_env, "\\}"),
520 paste0("end\\{", latex_table_env, "\\}"), x
521 )
522 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400523}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400524