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