blob: 93ce3d4b06c4e88a9fc586aa6c4f4422dc383073 [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 Zhu9399dcc2020-08-26 17:27:38 -040017#' @examples
18#' \dontrun{
19#' dt <- mtcars[1:5, 1:5]
Hao Zhu593f57e2018-01-09 13:30:01 -050020#' colnames(dt)[1] <- paste0("mpg", footnote_marker_alphabet(2, "html"))
21#' rownames(dt)[2] <- paste0(rownames(dt)[2], footnote_marker_alphabet(1, "html"))
22#' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b"))
Hao Zhu9399dcc2020-08-26 17:27:38 -040023#' }
Hao Zhu593f57e2018-01-09 13:30:01 -050024#'
Hao Zhu1ac13ad2018-01-08 16:12:24 -050025#' @export
Hao Zhuf94a26f2018-04-05 17:42:55 -040026footnote_marker_number <- function(x, format, double_escape = FALSE) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -050027 if (missing(format) || is.null(format)) {
28 format <- getOption('knitr.table.format')
29 }
30 if (is.null(format)) {
31 message("Setting footnote_marker_number format as html")
32 format <- "html"
33 }
34 if (format == "html") {
35 return(paste0("<sup>", x, "</sup>"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040036 } else if (!double_escape) {
Hao Zhu19c4fa52018-01-09 12:01:14 -050037 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040038 } else {
39 return(paste0("\\\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050040 }
41}
42
Hao Zhue0782ab2018-01-09 13:24:13 -050043#' @rdname footnote_marker_number
Hao Zhu1ac13ad2018-01-08 16:12:24 -050044#' @export
Hao Zhuf94a26f2018-04-05 17:42:55 -040045footnote_marker_alphabet <- function(x, format, double_escape = FALSE) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -050046 if (missing(format) || is.null(format)) {
47 format <- getOption('knitr.table.format')
48 }
49 if (is.null(format)) {
50 message("Setting footnote_marker_alphabet format as html")
51 format <- "html"
52 }
53 if (is.numeric(x)) x <- letters[x]
54 if (format == "html") {
55 return(paste0("<sup>", x, "</sup>"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040056 } else if (!double_escape) {
Hao Zhu19c4fa52018-01-09 12:01:14 -050057 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040058 } else {
59 return(paste0("\\\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050060 }
61}
62
Hao Zhue0782ab2018-01-09 13:24:13 -050063#' @rdname footnote_marker_number
Hao Zhu1ac13ad2018-01-08 16:12:24 -050064#' @export
Hao Zhuf94a26f2018-04-05 17:42:55 -040065footnote_marker_symbol <- function(x, format, double_escape = FALSE) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -050066 if (missing(format) || is.null(format)) {
67 format <- getOption('knitr.table.format')
68 }
69 if (is.null(format)) {
70 message("Setting footnote_marker_symbol format as html")
71 format <- "html"
72 }
73 number_index <- read.csv(system.file("symbol_index.csv",
74 package = "kableExtra"))
75 if (format == "html") {
76 x <- number_index$symbol.html[x]
77 return(paste0("<sup>", x, "</sup>"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040078 } else if (!double_escape) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -050079 x <- number_index$symbol.latex[x]
Hao Zhue809c562018-03-26 17:31:55 -040080 x <- gsub("\\\\\\\\", "\\\\", x)
Hao Zhu19c4fa52018-01-09 12:01:14 -050081 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhuf94a26f2018-04-05 17:42:55 -040082 } else {
83 x <- number_index$symbol.latex[x]
84 return(paste0("\\\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050085 }
86}