Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 1 | #' Add footnote (new) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 2 | #' |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 3 | #' @description `footnote` provides a more flexible way to add footnote. You |
| 4 | #' can add mutiple sets of footnote using differeny notation system. It is |
| 5 | #' also possible to specify footnote section header one by one and print |
| 6 | #' footnotes as a chunk of texts. |
| 7 | #' |
| 8 | #' @param kable_input HTML or LaTeX table generated by `knitr::kable` |
| 9 | #' @param general Text for general footnote comments. Footnotes in this section |
| 10 | #' won't be labeled with any notations |
| 11 | #' @param number A vector of footnote texts. Footnotes here will be numbered. |
| 12 | #' There is no upper cap for the number of footnotes here |
| 13 | #' @param alphabet A vector of footnote texts, Footnotes here will be labeled |
| 14 | #' with abc. The vector here should not have more than 26 elements. |
| 15 | #' @param symbol A vector of footnote texts, Footnotes here will be labeled |
| 16 | #' with special symbols. The vector here should not have more than 20 elements. |
| 17 | #' @param footnote_order The order of how to arrange `general`, `number`, |
| 18 | #' `alphabet` and `symbol`. |
| 19 | #' @param footnote_as_chunk T/F value. Default is FALSE. It controls whether |
| 20 | #' the footnotes should be printed in a chunk (without line break). |
| 21 | #' @param escape T/F value. It controls whether the contents and titles should |
| 22 | #' be escaped against HTML or LaTeX. Default is TRUE. |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 23 | #' @param threeparttable T/F value for whether to use LaTeX package |
| 24 | #' threeparttable. Threeparttable will force the width of caption and |
| 25 | #' footnotes be the width of the original table. It's useful when you have |
| 26 | #' long paragraph of footnotes. |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 27 | #' @param general_title Section header for general footnotes. Default is |
| 28 | #' "Note: ". |
| 29 | #' @param number_title Section header for number footnotes. Default is "". |
| 30 | #' @param alphabet_title Section header for alphabet footnotes. Default is "". |
| 31 | #' @param symbol_title Section header for symbol footnotes. Default is "". |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 32 | #' @param title_format Choose from "italic"(default), "bold" and "underline". |
| 33 | #' Multiple options are possible. |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 34 | #' @param symbol_manual User can manually supply a vector of either html or |
| 35 | #' latex symbols. For example, `symbol_manual = c('*', '\\\\dag', '\\\\ddag')`.` |
Hao Zhu | 465fc65 | 2018-05-20 20:02:36 -0400 | [diff] [blame] | 36 | #' |
Hao Zhu | b1e2e3d | 2018-01-09 13:31:42 -0500 | [diff] [blame] | 37 | #' @examples dt <- mtcars[1:5, 1:5] |
Hao Zhu | 593f57e | 2018-01-09 13:30:01 -0500 | [diff] [blame] | 38 | #' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b")) |
| 39 | #' |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 40 | #' @export |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 41 | footnote <- function(kable_input, |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 42 | general = NULL, |
| 43 | number = NULL, |
| 44 | alphabet = NULL, |
| 45 | symbol = NULL, |
| 46 | footnote_order = c("general", "number", |
| 47 | "alphabet", "symbol"), |
| 48 | footnote_as_chunk = FALSE, |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 49 | escape = TRUE, |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 50 | threeparttable = FALSE, |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 51 | general_title = "Note: ", |
| 52 | number_title = "", |
| 53 | alphabet_title = "", |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 54 | symbol_title = "", |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 55 | title_format = "italic", |
| 56 | symbol_manual = NULL |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 57 | ) { |
| 58 | kable_format <- attr(kable_input, "format") |
| 59 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 60 | warning("Please specify format in kable. kableExtra can customize either ", |
| 61 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 62 | "for details.") |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 63 | return(kable_input) |
| 64 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 65 | if (length(alphabet) > 26) { |
| 66 | alphabet <- alphabet[1:26] |
| 67 | warning("Please don't use more than 26 footnotes in table_footnote ", |
| 68 | "alphabet. Use number instead.") |
| 69 | } |
| 70 | if (length(symbol) > 20) { |
| 71 | symbol <- symbol[1:20] |
| 72 | warning("Please don't use more than 20 footnotes in table_footnote ", |
| 73 | "symbol. Use number instead.") |
| 74 | } |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 75 | footnote_titles <- list( |
| 76 | general = general_title, number = number_title, |
| 77 | alphabet = alphabet_title, symbol = symbol_title |
| 78 | ) |
| 79 | footnote_contents <- list( |
| 80 | general = general, number = number, alphabet = alphabet, symbol = symbol |
| 81 | ) |
| 82 | notnull <- names(footnote_contents)[!sapply(footnote_contents, is.null)] |
| 83 | if (length(notnull) == 0) {return(kable_input)} |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 84 | footnote_order <- footnote_order[footnote_order %in% notnull] |
| 85 | footnote_titles <- footnote_titles[footnote_order] |
| 86 | footnote_contents <- footnote_contents[footnote_order] |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 87 | if (escape) { |
| 88 | if (kable_format == "html") { |
| 89 | footnote_contents <- lapply(footnote_contents, escape_html) |
| 90 | footnote_titles <- lapply(footnote_titles, escape_html) |
| 91 | } else { |
Hao Zhu | d463087 | 2018-03-26 11:26:36 -0400 | [diff] [blame] | 92 | footnote_contents <- lapply(footnote_contents, escape_latex2) |
Hao Zhu | 1aff734 | 2018-04-02 18:33:15 -0400 | [diff] [blame] | 93 | footnote_contents <- lapply(footnote_contents, linebreak) |
Hao Zhu | d463087 | 2018-03-26 11:26:36 -0400 | [diff] [blame] | 94 | footnote_titles <- lapply(footnote_titles, escape_latex2) |
Hao Zhu | 1aff734 | 2018-04-02 18:33:15 -0400 | [diff] [blame] | 95 | footnote_titles <- lapply(footnote_titles, linebreak) |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 96 | } |
| 97 | } |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 98 | title_format <- match.arg(title_format, c("italic", "bold", "underline"), |
| 99 | several.ok = TRUE) |
| 100 | footnote_titles <- lapply(footnote_titles, footnote_title_format, |
| 101 | kable_format, title_format) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 102 | footnote_table <- footnote_table_maker( |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 103 | kable_format, footnote_titles, footnote_contents, symbol_manual |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 104 | ) |
| 105 | if (kable_format == "html") { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 106 | return(footnote_html(kable_input, footnote_table, footnote_as_chunk)) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 107 | } |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 108 | if (kable_format == "latex") { |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 109 | return(footnote_latex(kable_input, footnote_table, footnote_as_chunk, |
| 110 | threeparttable)) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 111 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 114 | footnote_title_format <- function(x, format, title_format) { |
| 115 | if (x == "") return(x) |
| 116 | if (format == "html") { |
| 117 | title_style <- "" |
| 118 | if ("italic" %in% title_format) { |
| 119 | title_style <- paste0(title_style, "font-style: italic;") |
| 120 | } |
| 121 | if ("bold" %in% title_format) { |
| 122 | title_style <- paste0(title_style, "font-weight: bold;") |
| 123 | } |
| 124 | if ("underline" %in% title_format) { |
| 125 | title_style <- paste0(title_style, "text-decoration: underline;") |
| 126 | } |
| 127 | return(paste0( |
| 128 | '<span style="', title_style, '">', x, '</span>' |
| 129 | )) |
| 130 | } else { |
| 131 | if ("italic" %in% title_format) { |
| 132 | x <- paste0("\\\\textit\\{", x, "\\}") |
| 133 | } |
| 134 | if ("bold" %in% title_format) { |
| 135 | x <- paste0("\\\\textbf\\{", x, "\\}") |
| 136 | } |
| 137 | if ("underline" %in% title_format) { |
| 138 | x <- paste0("\\\\underline\\{", x, "\\}") |
| 139 | } |
| 140 | return(x) |
| 141 | } |
| 142 | } |
| 143 | |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 144 | footnote_table_maker <- function(format, footnote_titles, footnote_contents, |
| 145 | symbol_manual) { |
| 146 | if (is.null(symbol_manual)) { |
| 147 | number_index <- read.csv(system.file("symbol_index.csv", |
| 148 | package = "kableExtra")) |
| 149 | if (format == "latex") { |
| 150 | symbol_index <- number_index$symbol.latex |
| 151 | } else { |
| 152 | symbol_index <- number_index$symbol.html |
| 153 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 154 | } else { |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 155 | symbol_index <- symbol_manual |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 156 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 157 | |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 158 | |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 159 | if (!is.null(footnote_contents$general)) { |
| 160 | footnote_contents$general <- data.frame( |
| 161 | index = "", |
Hao Zhu | bab692d | 2018-01-09 17:49:55 -0500 | [diff] [blame] | 162 | footnote = footnote_contents$general |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 163 | ) |
| 164 | } |
| 165 | if (!is.null(footnote_contents$number)) { |
| 166 | footnote_contents$number <- data.frame( |
| 167 | index = as.character(1:length(footnote_contents$number)), |
| 168 | footnote = footnote_contents$number |
| 169 | ) |
| 170 | } |
| 171 | if (!is.null(footnote_contents$alphabet)) { |
| 172 | footnote_contents$alphabet <- data.frame( |
| 173 | index = letters[1:length(footnote_contents$alphabet)], |
| 174 | footnote = footnote_contents$alphabet |
| 175 | ) |
| 176 | } |
| 177 | if (!is.null(footnote_contents$symbol)) { |
| 178 | footnote_contents$symbol <- data.frame( |
| 179 | index = symbol_index[1:length(footnote_contents$symbol)], |
| 180 | footnote = footnote_contents$symbol |
| 181 | ) |
| 182 | } |
| 183 | |
| 184 | out <- list() |
| 185 | out$contents <- footnote_contents |
| 186 | out$titles <- footnote_titles |
| 187 | return(out) |
| 188 | } |
| 189 | |
| 190 | # HTML |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 191 | footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 192 | kable_attrs <- attributes(kable_input) |
| 193 | kable_xml <- read_kable_as_xml(kable_input) |
| 194 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 195 | new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 196 | xml_add_child(kable_xml, new_html_footnote) |
| 197 | |
| 198 | out <- as_kable_xml(kable_xml) |
| 199 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 200 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 201 | return(out) |
| 202 | } |
| 203 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 204 | html_tfoot_maker <- function(footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 205 | footnote_types <- names(footnote_table$contents) |
| 206 | footnote_text <- c() |
| 207 | for (i in footnote_types) { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 208 | footnote_text <- c(footnote_text, html_tfoot_maker_( |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 209 | footnote_table$contents[[i]], footnote_table$titles[[i]], i, |
| 210 | footnote_as_chunk)) |
| 211 | } |
| 212 | footnote_text <- paste0( |
| 213 | "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>" |
| 214 | ) |
| 215 | footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR")) |
| 216 | return(xml_child(xml_child(footnote_node, 1), 1)) |
| 217 | } |
| 218 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 219 | html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 220 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 221 | paste0('<sup>', x[1], '</sup> ', x[2]) |
| 222 | }) |
| 223 | if (ft_title != "") { |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 224 | title_text <- ft_title |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 225 | footnote_text <- c(title_text, footnote_text) |
| 226 | } |
| 227 | if (!ft_chunk) { |
| 228 | footnote_text <- paste0( |
| 229 | '<tr><td style="padding: 0; border: 0;" colspan="100%">', |
| 230 | footnote_text, '</td></tr>' |
| 231 | ) |
| 232 | } else { |
| 233 | footnote_text <- paste0( |
| 234 | '<tr><td style="padding: 0; border: 0;" colspan="100%">', |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 235 | paste0(footnote_text, collapse = " "), |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 236 | '</td></tr>' |
| 237 | ) |
| 238 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 239 | return(footnote_text) |
| 240 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 241 | |
| 242 | # LaTeX |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 243 | footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk, |
| 244 | threeparttable) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 245 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 246 | out <- solve_enc(kable_input) |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 247 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 248 | footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk, |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 249 | table_info$ncol, threeparttable) |
| 250 | if (threeparttable) { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 251 | if (table_info$tabular %in% c("longtable", "longtabu") ) { |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 252 | out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"), |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 253 | paste0("\\\\begin{ThreePartTable}\n\\\\begin{TableNotes}", |
| 254 | ifelse(footnote_as_chunk, "[para]", ""), |
Hao Zhu | 5b8eb2b | 2018-05-20 18:16:06 -0400 | [diff] [blame] | 255 | "\n", footnote_text, |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 256 | "\n\\\\end{TableNotes}\n\\\\begin{", |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 257 | table_info$tabular, "}"), |
| 258 | out) |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 259 | out <- sub(paste0("\\\\end\\{",table_info$tabular, "\\}"), |
| 260 | paste0("\\\\end{", table_info$tabular, |
| 261 | "}\n\\\\end{ThreePartTable}"), |
| 262 | out) |
| 263 | if (table_info$booktabs) { |
| 264 | out <- sub("\\\\bottomrule", "\\\\bottomrule\n\\\\insertTableNotes", out) |
| 265 | } else { |
| 266 | out <- sub("\\\\hline\n\\\\end\\{longtable\\}", |
| 267 | "\\\\hline\n\\\\insertTableNotes\n\\\\end\\{longtable\\}", |
| 268 | out) |
| 269 | } |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 270 | } else { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 271 | if (table_info$tabular == "tabu") { |
| 272 | stop("Please use `longtable = T` in your kable function. ", |
| 273 | "Full width threeparttable only works with longtable.") |
| 274 | } |
| 275 | out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"), |
| 276 | paste0("\\\\begin{threeparttable}\n\\\\begin{", |
| 277 | table_info$tabular, "}"), |
| 278 | out) |
| 279 | out <- sub(table_info$end_tabular, |
| 280 | paste0("\\\\end{", table_info$tabular, |
| 281 | "}\n\\\\begin{tablenotes}", |
| 282 | ifelse(footnote_as_chunk, "[para]", ""), |
Hao Zhu | 5b8eb2b | 2018-05-20 18:16:06 -0400 | [diff] [blame] | 283 | "\n", footnote_text, |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 284 | "\n\\\\end{tablenotes}\n\\\\end{threeparttable}"), |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 285 | out) |
| 286 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 287 | } else { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 288 | if (table_info$booktabs) { |
| 289 | out <- sub("\\\\bottomrule", |
| 290 | paste0("\\\\bottomrule\n", footnote_text), out) |
| 291 | } else { |
| 292 | out <- sub(table_info$end_tabular, |
| 293 | paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"), |
| 294 | out) |
| 295 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 298 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 299 | attr(out, "kable_meta") <- table_info |
| 300 | return(out) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 301 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 302 | |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 303 | latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol, |
| 304 | threeparttable) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 305 | footnote_types <- names(footnote_table$contents) |
| 306 | footnote_text <- c() |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 307 | if (threeparttable) { |
| 308 | for (i in footnote_types) { |
| 309 | footnote_text <- c(footnote_text, latex_tfoot_maker_tpt_( |
| 310 | footnote_table$contents[[i]], footnote_table$titles[[i]], |
| 311 | footnote_as_chunk, ncol)) |
| 312 | } |
| 313 | } else { |
| 314 | for (i in footnote_types) { |
| 315 | footnote_text <- c(footnote_text, latex_tfoot_maker_( |
| 316 | footnote_table$contents[[i]], footnote_table$titles[[i]], |
| 317 | footnote_as_chunk, ncol)) |
| 318 | } |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 319 | } |
| 320 | footnote_text <- paste0(footnote_text, collapse = "\n") |
| 321 | return(footnote_text) |
| 322 | } |
Hao Zhu | 9f91748 | 2018-01-08 18:09:33 -0500 | [diff] [blame] | 323 | |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 324 | latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_chunk, ncol) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 325 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 326 | if (x[1] == "") { |
| 327 | x[2] |
| 328 | } else { |
| 329 | paste0('\\\\textsuperscript{', x[1], '} ', x[2]) |
| 330 | } |
| 331 | }) |
| 332 | if (ft_title != "") { |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 333 | title_text <- ft_title |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 334 | footnote_text <- c(title_text, footnote_text) |
| 335 | } |
| 336 | if (!ft_chunk) { |
| 337 | footnote_text <- paste0( |
| 338 | '\\\\multicolumn{', ncol, '}{l}{', footnote_text, '}\\\\\\\\' |
| 339 | ) |
| 340 | } else { |
| 341 | footnote_text <- paste0( |
| 342 | '\\\\multicolumn{', ncol, '}{l}{', |
| 343 | paste0(footnote_text, collapse = " "), |
| 344 | '}\\\\\\\\' |
| 345 | ) |
| 346 | } |
| 347 | return(footnote_text) |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 348 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 349 | |
| 350 | latex_tfoot_maker_tpt_ <- function(ft_contents, ft_title, ft_chunk, ncol) { |
| 351 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 352 | if (x[1] == "") { |
| 353 | paste0('\\\\item ', x[2]) |
| 354 | } else { |
| 355 | paste0('\\\\item[', x[1], '] ', x[2]) |
| 356 | } |
| 357 | }) |
| 358 | if (ft_title != "") { |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 359 | title_text <- paste0('\\\\item ', ft_title, ' ') |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 360 | footnote_text <- c(title_text, footnote_text) |
| 361 | } |
| 362 | footnote_text <- paste0(footnote_text, collapse = "\n") |
| 363 | # if (!ft_chunk) { |
| 364 | # footnote_text <- paste0(footnote_text, collapse = "\n") |
| 365 | # } else { |
| 366 | # footnote_text <- paste0(footnote_text, collapse = " ") |
| 367 | # } |
| 368 | return(footnote_text) |
| 369 | } |