blob: 305d3d03be13c9bdbccf71e61b29440b82ef01f6 [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
26#' 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 Zhue1be9602017-08-17 15:44:31 -040038#' @param ... extra options for HTML or LaTeX. See `details`.
39#'
40#' @details For LaTeX, extra options includes:
41#' - `repeat_header_method` can either be `append`(default) or `replace`
42#' - `repeat_header_text` is just a text string you want to append on or
43#' replace the caption.
Hao Zhu71a224f2017-08-18 11:06:22 -040044#' - `stripe_color` allows users to pick a different color for their strip lines.
Hao Zhu245931c2017-09-01 22:43:56 -040045#' - `latex_table_env` character string to define customized table environment
46#' such as tabu or tabularx.You shouldn't expect all features could be
47#' supported in self-defined environments.
Hao Zhue10cfd32017-02-21 16:41:14 -050048#'
Hao Zhu78e61222017-05-24 20:53:35 -040049#' @examples x_html <- knitr::kable(head(mtcars), "html")
50#' kable_styling(x_html, "striped", position = "left", font_size = 7)
51#'
52#' x_latex <- knitr::kable(head(mtcars), "latex")
53#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
54#'
Hao Zhue10cfd32017-02-21 16:41:14 -050055#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050056kable_styling <- function(kable_input,
57 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050058 latex_options = "basic",
59 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050060 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040061 font_size = NULL,
62 ...) {
Hao Zhu9b45a182017-02-27 18:17:46 -050063
64 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
65 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
66 }
67 if (length(latex_options) == 1 && latex_options == "basic") {
68 latex_options <- getOption("kable_styling_latex_options", "basic")
69 }
70 if (position == "center") {
71 position <- getOption("kable_styling_position", "center")
72 }
73 position <- match.arg(position,
74 c("center", "left", "right", "float_left", "float_right"))
75 if (is.null(font_size)) {
76 font_size <- getOption("kable_styling_font_size", NULL)
77 }
78
Hao Zhuc1f38412017-02-23 12:13:48 -050079 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050080
Hao Zhuc1f38412017-02-23 12:13:48 -050081 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050082 warning("Please specify format in kable. kableExtra can customize either ",
83 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
84 "for details.")
85 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050086 }
87 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050088 if (is.null(full_width)) {
89 full_width <- getOption("kable_styling_full_width", T)
90 }
Hao Zhuc1f38412017-02-23 12:13:48 -050091 return(htmlTable_styling(kable_input,
92 bootstrap_options = bootstrap_options,
93 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050094 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -040095 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -050096 }
97 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -050098 if (is.null(full_width)) {
99 full_width <- getOption("kable_styling_full_width", F)
100 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500101 return(pdfTable_styling(kable_input,
102 latex_options = latex_options,
103 full_width = full_width,
104 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -0400105 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -0500106 }
107}
108
109# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500110htmlTable_styling <- function(kable_input,
111 bootstrap_options = "basic",
112 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500113 position = c("center", "left", "right",
114 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -0500115 font_size = NULL) {
Hao Zhu909ea382017-06-12 15:43:47 -0400116 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400117 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500118
119 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500120 bootstrap_options <- match.arg(
121 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500122 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500123 several.ok = T
124 )
125
Hao Zhu26234122017-02-22 15:34:33 -0500126 kable_xml_class <- NULL
127 if (xml_has_attr(kable_xml, "class")) {
128 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500129 }
Hao Zhu26234122017-02-22 15:34:33 -0500130 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
131 bootstrap_options <- "table"
132 } else {
133 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
134 bootstrap_options <- paste0("table-", bootstrap_options)
135 bootstrap_options <- c("table", bootstrap_options)
136 }
137 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
138 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500139
Hao Zhu26234122017-02-22 15:34:33 -0500140 # Modify style
141 kable_xml_style <- NULL
142 if (xml_has_attr(kable_xml, "style")) {
143 kable_xml_style <- xml_attr(kable_xml, "style")
144 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500145 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500146 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500147 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400148 kable_caption_node <- xml_tpart(kable_xml, "caption")
149 if (!is.null(kable_caption_node)) {
150 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
151 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500152 }
153 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500154 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500155 }
Hao Zhu94956582017-02-21 18:18:29 -0500156
Hao Zhuc05e1812017-02-25 01:45:35 -0500157 position <- match.arg(position)
158 position_style <- switch(
159 position,
160 center = "margin-left: auto; margin-right: auto;",
Hao Zhufd516ba2017-07-28 14:30:25 -0400161 left = "",
Hao Zhuc05e1812017-02-25 01:45:35 -0500162 right = "margin-right: 0; margin-left: auto",
163 float_left = "float: left; margin-right: 10px;",
164 float_right = "float: right; margin-left: 10px;"
165 )
166 kable_xml_style <- c(kable_xml_style, position_style)
167
Hao Zhu26234122017-02-22 15:34:33 -0500168 if (length(kable_xml_style) != 0) {
169 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500170 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500171
Hao Zhuf2dfd142017-07-24 14:43:28 -0400172 out <- as_kable_xml(kable_xml)
Hao Zhu909ea382017-06-12 15:43:47 -0400173 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500174 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu9b45a182017-02-27 18:17:46 -0500175 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500176}
Hao Zhuc1f38412017-02-23 12:13:48 -0500177
178# LaTeX table style
179pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500180 latex_options = "basic",
181 full_width = F,
182 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500183 "float_left", "float_right"),
Hao Zhua31e97f2017-06-08 14:55:41 -0400184 font_size = NULL,
Hao Zhuca211db2017-07-25 00:06:43 -0400185 repeat_header_text = "\\textit{(continued)}",
Hao Zhue1be9602017-08-17 15:44:31 -0400186 repeat_header_method = c("append", "replace"),
Hao Zhu6c9714a2017-10-02 14:52:45 -0400187 repeat_header_continued = FALSE,
Hao Zhu245931c2017-09-01 22:43:56 -0400188 stripe_color = "gray!6",
189 latex_table_env = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500190
Hao Zhuc05e1812017-02-25 01:45:35 -0500191 latex_options <- match.arg(
192 latex_options,
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100193 c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500194 several.ok = T
195 )
196
Hao Zhuca211db2017-07-25 00:06:43 -0400197 repeat_header_method <- match.arg(repeat_header_method)
198
Hao Zhua3fc0c42017-02-27 12:04:59 -0500199 out <- NULL
Hao Zhud2c0f732017-08-26 10:40:14 -0400200 out <- enc2utf8(as.character(kable_input))
Hao Zhuc05e1812017-02-25 01:45:35 -0500201 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500202
203 if ("striped" %in% latex_options) {
Hao Zhu71a224f2017-08-18 11:06:22 -0400204 out <- styling_latex_striped(out, table_info, stripe_color)
Hao Zhuc05e1812017-02-25 01:45:35 -0500205 }
206
207 # hold_position is only meaningful in a table environment
208 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500209 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500210 }
211
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100212 # HOLD_position is only meaningful in a table environment
213 if ("HOLD_position" %in% latex_options & table_info$table_env) {
214 out <- styling_latex_HOLD_position(out)
215 }
216
Hao Zhua3fc0c42017-02-27 12:04:59 -0500217 if ("scale_down" %in% latex_options) {
218 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500219 }
220
Hao Zhu971d89f2017-06-07 19:22:47 -0400221 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400222 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400223 repeat_header_method, repeat_header_continued)
Hao Zhu971d89f2017-06-07 19:22:47 -0400224 }
225
Hao Zhuc05e1812017-02-25 01:45:35 -0500226 if (full_width) {
Hao Zhu245931c2017-09-01 22:43:56 -0400227 latex_table_env <- ifelse(table_info$tabular == "longtable",
228 "longtabu", "tabu")
229 full_width_return <- styling_latex_full_width(out, table_info)
230 out <- full_width_return[[1]]
231 table_info$align_vector <- full_width_return[[2]]
Hao Zhua3fc0c42017-02-27 12:04:59 -0500232 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500233
Hao Zhua3fc0c42017-02-27 12:04:59 -0500234 if (!is.null(font_size)) {
235 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500236 }
237
Hao Zhu245931c2017-09-01 22:43:56 -0400238 if (!is.null(latex_table_env)) {
239 out <- styling_latex_table_env(out, table_info$tabular, latex_table_env)
240 table_info$tabular <- latex_table_env
241 table_info$begin_tabular <- sub("tabular", latex_table_env,
242 table_info$begin_tabular)
243 table_info$end_tabular <- sub("tabular", latex_table_env,
244 table_info$end_tabular)
245 }
246
Hao Zhuc05e1812017-02-25 01:45:35 -0500247 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500248 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500249
250 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400251 attr(out, "kable_meta") <- table_info
Hao Zhuc05e1812017-02-25 01:45:35 -0500252 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500253}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500254
Hao Zhue1be9602017-08-17 15:44:31 -0400255styling_latex_striped <- function(x, table_info, color) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400256 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
257 if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
Hao Zhue1be9602017-08-17 15:44:31 -0400258 row_color <- sprintf("\\rowcolors{2}{white}{%s}", color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400259 } else {
Hao Zhue1be9602017-08-17 15:44:31 -0400260 row_color <- sprintf("\\rowcolors{2}{%s}{white}", color)
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400261 }
Hao Zhu00ba87c2017-08-01 12:42:58 -0400262
263 x <- read_lines(x)
264 if (table_info$booktabs) {
265 header_rows_start <- which(x == "\\toprule")[1]
266 header_rows_end <- which(x == "\\midrule")[1]
267 } else {
268 header_rows_start <- which(x == "\\hline")[1]
269 header_rows_end <- which(x == "\\hline")[2]
270 }
271
272 x <- c(
273 row_color,
274 x[1:(header_rows_start - 1)],
275 "\\hiderowcolors",
276 x[header_rows_start:header_rows_end],
277 "\\showrowcolors",
278 x[(header_rows_end + 1):length(x)],
279 "\\rowcolors{2}{white}{white}"
280 )
281 x <- paste0(x, collapse = "\n")
282 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500283}
284
285styling_latex_hold_position <- function(x) {
286 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
287}
288
Rob Shepherdc59d15e2017-08-26 16:06:36 +0100289styling_latex_HOLD_position <- function(x) {
290 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[H]", x)
291}
292
Hao Zhua3fc0c42017-02-27 12:04:59 -0500293styling_latex_scale_down <- function(x, table_info) {
294 # You cannot put longtable in a resizebox
295 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
296 if (table_info$tabular == "longtable") {
297 warning("Longtable cannot be resized.")
298 return(x)
299 }
300 x <- sub(table_info$begin_tabular,
Hao Zhucc21dc72017-05-20 01:15:25 -0400301 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500302 table_info$begin_tabular),
303 x)
304 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
305}
306
Hao Zhuca211db2017-07-25 00:06:43 -0400307styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
Hao Zhu6c9714a2017-10-02 14:52:45 -0400308 repeat_header_method,
309 repeat_header_continued) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400310 x <- read_lines(x)
311 if (table_info$booktabs) {
312 header_rows_start <- which(x == "\\toprule")[1]
313 header_rows_end <- which(x == "\\midrule")[1]
314 } else {
315 header_rows_start <- which(x == "\\hline")[1]
316 header_rows_end <- which(x == "\\hline")[2]
317 }
Hao Zhufdec1842017-06-08 17:06:04 -0400318
319 if (is.na(table_info$caption)) {
320 continue_line <- paste0(
321 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
322 "}\\\\"
323 )
324 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400325 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400326 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400327 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400328 }
Hao Zhu68d4fe92017-07-25 14:07:50 -0400329 continue_line <- paste0("\\caption[]{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400330 }
331
Hao Zhu6c9714a2017-10-02 14:52:45 -0400332 if (!table_info$booktabs) {
333 bottom_part <- NULL
334 } else {
335 index_bottomrule <- which(x == "\\bottomrule")
336 x <- x[-index_bottomrule]
Hao Zhu20784e72017-10-10 14:48:33 -0400337 x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*")
Hao Zhu6c9714a2017-10-02 14:52:45 -0400338
339 if (repeat_header_continued == FALSE) {
Hao Zhu44f6c492017-10-20 23:05:37 -0400340 bottom_part <- "\\\n\\endfoot\n\\bottomrule\n\\endlastfoot"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400341 } else {
342 if (repeat_header_continued == TRUE) {
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400343 bottom_text <- "\\textit{(continued \\ldots)}"
Hao Zhu6c9714a2017-10-02 14:52:45 -0400344 } else {
345 bottom_text <- repeat_header_continued
346 }
347 bottom_part <- paste0(
Hao Zhu44f6c492017-10-20 23:05:37 -0400348 "\\midrule\n",
Hao Zhub7f4f4e2017-10-02 14:58:41 -0400349 "\\multicolumn{", table_info$ncol, "}{r@{}}{", bottom_text, "}\\\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400350 "\\endfoot\n",
Hao Zhuc83eb632017-10-10 14:11:08 -0400351 "\\bottomrule\n",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400352 "\\endlastfoot"
353 )
354 }
355 }
356
357 # x[index_bottomrule - 1] <- paste0(x[index_bottomrule - 1], "*\\bottomrule")
Hao Zhua31e97f2017-06-08 14:55:41 -0400358 x <- c(
359 x[1:header_rows_end],
360 "\\endfirsthead",
361 continue_line,
362 x[header_rows_start:header_rows_end],
363 "\\endhead",
Hao Zhu6c9714a2017-10-02 14:52:45 -0400364 bottom_part,
Hao Zhua31e97f2017-06-08 14:55:41 -0400365 x[(header_rows_end + 1):length(x)]
366 )
367 x <- paste0(x, collapse = "\n")
368 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400369}
370
Hao Zhua3fc0c42017-02-27 12:04:59 -0500371styling_latex_full_width <- function(x, table_info) {
Hao Zhu245931c2017-09-01 22:43:56 -0400372 col_align <- as.character(factor(
373 table_info$align_vector, c("c", "l", "r"),
374 c(">{\\\\centering}X", ">{\\\\raggedright}X", ">{\\\\raggedleft}X")
375 ))
376 col_align[is.na(col_align)] <- table_info$align_vector[is.na(col_align)]
377 col_align_vector <- col_align
378 col_align <- paste0(" to \\\\linewidth {", paste(col_align, collapse = ""), "}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500379 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
380 table_info$begin_tabular, x)
Hao Zhu245931c2017-09-01 22:43:56 -0400381 x <- sub(table_info$begin_tabular,
Hao Zhua3fc0c42017-02-27 12:04:59 -0500382 paste0(table_info$begin_tabular, col_align), x)
Hao Zhu245931c2017-09-01 22:43:56 -0400383 return(list(x, col_align_vector))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500384}
385
386styling_latex_position <- function(x, table_info, position, latex_options) {
newtuxd2867062017-09-29 00:19:27 -0400387 hold_position <- intersect(c("hold_position", "HOLD_position"), latex_options)
Hao Zhu064990d2017-10-17 18:08:42 -0400388 if (length(hold_position) == 0) hold_position <- ""
Hao Zhua3fc0c42017-02-27 12:04:59 -0500389 switch(
390 position,
391 center = styling_latex_position_center(x, table_info, hold_position),
392 left = styling_latex_position_left(x, table_info),
393 right = styling_latex_position_right(x, table_info, hold_position),
394 float_left = styling_latex_position_float(x, table_info, "l"),
395 float_right = styling_latex_position_float(x, table_info, "r")
396 )
397}
398
399styling_latex_position_center <- function(x, table_info, hold_position) {
400 if (!table_info$table_env & table_info$tabular == "tabular") {
newtuxd2867062017-09-29 00:19:27 -0400401 x <- paste0("\\begin{table}\n\\centering", x, "\n\\end{table}")
402 if (hold_position == "hold_position") {
403 x <- styling_latex_hold_position(x)
404 } else {
405 x <- styling_latex_HOLD_position(x)
406 }
Hao Zhua3fc0c42017-02-27 12:04:59 -0500407 }
408 return(x)
409}
410
411styling_latex_position_left <- function(x, table_info) {
412 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
413 longtable_option <- "\\[l\\]"
414 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
415 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
416}
417
418styling_latex_position_right <- function(x, table_info, hold_position) {
419 warning("Position = right is only supported for longtable in LaTeX. ",
420 "Setting back to center...")
421 styling_latex_position_center(x, table_info, hold_position)
422}
423
424styling_latex_position_float <- function(x, table_info, option) {
425 if (table_info$tabular == "longtable") {
426 warning("wraptable is not supported for longtable.")
427 if (option == "l") return(styling_latex_position_left(x, table_info))
428 if (option == "r") return(styling_latex_position_right(x, table_info, F))
429 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500430 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
431 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500432 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500433 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
434 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
435 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
436 x <- sub("\\\\begin\\{table\\}", option, x)
437 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500438 } else {
439 option <- sprintf("\\begin{wraptable}{%s}", option)
440 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
441 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500442 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500443 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500444}
445
446styling_latex_font_size <- function(x, table_info, font_size) {
447 row_height <- font_size + 2
Hao Zhu245931c2017-09-01 22:43:56 -0400448 if (table_info$tabular != "longtable" & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500449 return(sub(table_info$begin_tabular,
450 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
451 "\\}\\\\selectfont\n", table_info$begin_tabular),
452 x))
453 }
454 # For longtable and tabular without table environment. Simple wrap around
455 # fontsize is good enough
456 return(paste0(
457 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400458 "\\endgroup{}"
Hao Zhua3fc0c42017-02-27 12:04:59 -0500459 ))
460}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400461
Hao Zhu245931c2017-09-01 22:43:56 -0400462styling_latex_table_env <- function(x, current_env, latex_table_env) {
Hao Zhu3af9f942017-09-07 12:30:21 -0400463 x <- sub(
464 paste0("begin\\{", current_env, "\\}\\[t\\]"),
465 paste0("begin\\{", latex_table_env, "\\}"), x
466 )
467 x <- sub(
468 paste0("begin\\{", current_env, "\\}"),
469 paste0("begin\\{", latex_table_env, "\\}"), x
470 )
471 x <- sub(
472 paste0("end\\{", current_env, "\\}"),
473 paste0("end\\{", latex_table_env, "\\}"), x
474 )
475 return(x)
Hao Zhu245931c2017-09-01 22:43:56 -0400476}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400477