blob: 72b330649c1bb04a6aa31eff709f84147f6a0e7f [file] [log] [blame]
Hao Zhucdd7f922018-01-08 11:39:40 -05001#' Footnote marker
Hao Zhue0782ab2018-01-09 13:24:13 -05002#'
3#' @description Put footnote mark in superscription in table. Unless you are
4#' using it in the `caption` of `kable`, you will need to put `escape = F`
5#' in `kable` (similar with `cell_spec`). Again, similar with `cell_spec`, the
6#' `format` option here can read default value from global option
7#' `knitr.table.format`.
8#'
9#' @param x a number. For example, for `footnote_marker_alphabet(2)` will
10#' return "b" in HTML.
11#' @param format Either `html` or `latex`. All functions here can read
12#' default value from global option `knitr.table.format`.
Hao Zhuf94a26f2018-04-05 17:42:55 -040013#' @param double_escape T/F if output is in LaTeX, whether it should be double
14#' escaped. If you are using footnote_marker in `group_rows`` labeling row or
15#' `add_header_above`, you need to set this to be `TRUE`.
Hao Zhue0782ab2018-01-09 13:24:13 -050016#'
Hao Zhub1e2e3d2018-01-09 13:31:42 -050017#' @examples dt <- mtcars[1:5, 1:5]
Hao Zhu593f57e2018-01-09 13:30:01 -050018#' colnames(dt)[1] <- paste0("mpg", footnote_marker_alphabet(2, "html"))
19#' rownames(dt)[2] <- paste0(rownames(dt)[2], footnote_marker_alphabet(1, "html"))
20#' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b"))
21#'
Hao Zhu1ac13ad2018-01-08 16:12:24 -050022#' @export
Hao Zhuf94a26f2018-04-05 17:42:55 -040023footnote_marker_number <- function(x, format, double_escape = FALSE) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -050024 if (missing(format) || is.null(format)) {
25 format <- getOption('knitr.table.format')
26 }
27 if (is.null(format)) {
28 message("Setting footnote_marker_number format as html")
29 format <- "html"
30 }
31 if (format == "html") {
32 return(paste0("<sup>", x, "</sup>"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040033 } else if (!double_escape) {
Hao Zhu19c4fa52018-01-09 12:01:14 -050034 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040035 } else {
36 return(paste0("\\\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050037 }
38}
39
Hao Zhue0782ab2018-01-09 13:24:13 -050040#' @rdname footnote_marker_number
Hao Zhu1ac13ad2018-01-08 16:12:24 -050041#' @export
Hao Zhuf94a26f2018-04-05 17:42:55 -040042footnote_marker_alphabet <- function(x, format, double_escape = FALSE) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -050043 if (missing(format) || is.null(format)) {
44 format <- getOption('knitr.table.format')
45 }
46 if (is.null(format)) {
47 message("Setting footnote_marker_alphabet format as html")
48 format <- "html"
49 }
50 if (is.numeric(x)) x <- letters[x]
51 if (format == "html") {
52 return(paste0("<sup>", x, "</sup>"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040053 } else if (!double_escape) {
Hao Zhu19c4fa52018-01-09 12:01:14 -050054 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040055 } else {
56 return(paste0("\\\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050057 }
58}
59
Hao Zhue0782ab2018-01-09 13:24:13 -050060#' @rdname footnote_marker_number
Hao Zhu1ac13ad2018-01-08 16:12:24 -050061#' @export
Hao Zhuf94a26f2018-04-05 17:42:55 -040062footnote_marker_symbol <- function(x, format, double_escape = FALSE) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -050063 if (missing(format) || is.null(format)) {
64 format <- getOption('knitr.table.format')
65 }
66 if (is.null(format)) {
67 message("Setting footnote_marker_symbol format as html")
68 format <- "html"
69 }
70 number_index <- read.csv(system.file("symbol_index.csv",
71 package = "kableExtra"))
72 if (format == "html") {
73 x <- number_index$symbol.html[x]
74 return(paste0("<sup>", x, "</sup>"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040075 } else if (!double_escape) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -050076 x <- number_index$symbol.latex[x]
Hao Zhue809c562018-03-26 17:31:55 -040077 x <- gsub("\\\\\\\\", "\\\\", x)
Hao Zhu19c4fa52018-01-09 12:01:14 -050078 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040079 } else {
80 x <- number_index$symbol.latex[x]
81 return(paste0("\\\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050082 }
83}