blob: beb1c4ea6b5937313258c6af2e4198a9e50a7003 [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`.
13#'
Hao Zhu593f57e2018-01-09 13:30:01 -050014#' @examples x <- mtcars[1:5, 1:5]
15#' colnames(dt)[1] <- paste0("mpg", footnote_marker_alphabet(2, "html"))
16#' rownames(dt)[2] <- paste0(rownames(dt)[2], footnote_marker_alphabet(1, "html"))
17#' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b"))
18#'
Hao Zhu1ac13ad2018-01-08 16:12:24 -050019#' @export
20footnote_marker_number <- function(x, format) {
21 if (missing(format) || is.null(format)) {
22 format <- getOption('knitr.table.format')
23 }
24 if (is.null(format)) {
25 message("Setting footnote_marker_number format as html")
26 format <- "html"
27 }
28 if (format == "html") {
29 return(paste0("<sup>", x, "</sup>"))
30 } else {
Hao Zhu19c4fa52018-01-09 12:01:14 -050031 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050032 }
33}
34
Hao Zhue0782ab2018-01-09 13:24:13 -050035#' @rdname footnote_marker_number
Hao Zhu1ac13ad2018-01-08 16:12:24 -050036#' @export
37footnote_marker_alphabet <- function(x, format) {
38 if (missing(format) || is.null(format)) {
39 format <- getOption('knitr.table.format')
40 }
41 if (is.null(format)) {
42 message("Setting footnote_marker_alphabet format as html")
43 format <- "html"
44 }
45 if (is.numeric(x)) x <- letters[x]
46 if (format == "html") {
47 return(paste0("<sup>", x, "</sup>"))
48 } else {
Hao Zhu19c4fa52018-01-09 12:01:14 -050049 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050050 }
51}
52
Hao Zhue0782ab2018-01-09 13:24:13 -050053#' @rdname footnote_marker_number
Hao Zhu1ac13ad2018-01-08 16:12:24 -050054#' @export
55footnote_marker_symbol <- function(x, format) {
56 if (missing(format) || is.null(format)) {
57 format <- getOption('knitr.table.format')
58 }
59 if (is.null(format)) {
60 message("Setting footnote_marker_symbol format as html")
61 format <- "html"
62 }
63 number_index <- read.csv(system.file("symbol_index.csv",
64 package = "kableExtra"))
65 if (format == "html") {
66 x <- number_index$symbol.html[x]
67 return(paste0("<sup>", x, "</sup>"))
68 } else {
69 x <- number_index$symbol.latex[x]
Hao Zhu19c4fa52018-01-09 12:01:14 -050070 return(paste0("\\textsuperscript{", x, "}"))
Hao Zhu1ac13ad2018-01-08 16:12:24 -050071 }
72}