Add footnote support to rmarkdown/pandoc
diff --git a/R/footnote.R b/R/footnote.R
index cca651a..8724141 100644
--- a/R/footnote.R
+++ b/R/footnote.R
@@ -14,7 +14,7 @@
#' "number", "alphabet" and "symbol".
#'
#' @export
-add_footnote <- function(input, label = NULL, notation = "alphabet") {
+add_footnote <- function(input, label = NULL, notation = "alphabet", threeparttable = F) {
if (is.null(label)){return(input)}
# Define available id list
if (!notation %in% c("number", "alphabet", "symbol")){
@@ -35,54 +35,91 @@
"**", "††", "‡‡", "§§", "¶¶",
"*", "†††", "‡‡‡", "§§§", "¶¶¶",
"**", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
+ ),
+ symbol.markdown = c(
+ "\\*", "†", "‡", "§", "¶",
+ "\\*\\*", "††", "‡‡", "§§", "¶¶",
+ "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
+ "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
+ ),
+ symbol.pandoc = c(
+ "\\*", "†", "‡", "§", "¶",
+ "\\*\\*", "††", "‡‡", "§§", "¶¶",
+ "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
+ "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
)
)
ids <- ids.ops[,notation]
+ ids.intable <- gsub("\\*", "\\\\*", ids)
+
+ #count the number of items in label and intable notation
+ count.label = length(label)
+ count.intablenoot = sum(str_count(input, "\\[note\\]"))
+ if (count.intablenoot != 0 & count.label != count.intablenoot){
+ warning(paste("You entered", count.label, "labels but you put",
+ count.intablenoot, "[note] in your table."))
+ }
if(!attr(input, "format") %in% c("html", "latex")){
- warning("Currently kableExtra only supports html and latex. You have to specify your kable export format or set it in the global option `knitr.table.format`.")
export <- input
+ if(count.intablenoot != 0){
+ for(i in 1:count.intablenoot){
+ export[which(str_detect(export, "\\[note\\]"))[1]] <-
+ sub("\\[note\\]", paste0("^", ids.intable[i], "^",
+ paste0(rep(" ", 4 - nchar(as.character(ids[i]))),
+ collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]])
+ }
}
- # Generate latex table footnote using threeparttable --------------------------------
+ export[length(export)+1] <- ""
+ export[length(export)+1] <- "__Note:__"
+ export[length(export)+1] <- paste0(
+ paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
+ )
+ }
+
+ # Generate latex table footnote --------------------------------
if(attr(input, "format")=="latex"){
- #count the number of items in label and intable notation
- count.label = length(label)
- count.intablenoot = sum(gregexpr("\\[note\\]", input)[[1]]>0)
- if (count.intablenoot != 0 & count.label != count.intablenoot){
- warning(paste("You entered", count.label, "labels but you put",
- count.intablenoot, "[note] in your table."))
+ # If longtable is used, then use page footnote instead of threeparttable
+ # as it makes more sense to see the footnote at the bottom of page if
+ # table is longer than one page.
+ if(grepl("\\\\begin\\{longtable\\}", input)){
+
+ for(i in 1:count.intablenoot){
+ input <- sub("\\[note\\]", paste0("\\\\footnote[", ids[i], "]{", label[i], "}"), input)
+ }
+ }else{
+ # Regular cases other than longtable
+ # generate footer with appropriate symbol
+ footer <- ""
+ for(i in 1:count.label){
+ footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
}
- # generate footer with appropriate symbol
- footer <- ""
- for(i in 1:count.label){
- footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
- }
+ # Replace in-table notation with appropriate symbol
+ for(i in 1:count.intablenoot){
+ input <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), input)
+ }
- # Replace in-table notation with appropriate symbol
- for(i in 1:count.intablenoot){
- input <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), input)
- }
-
- #
- if(grepl("\\\\caption\\{.*?\\}", input)){
- if(grepl("\\\\begin\\{tabular\\}", input)){
+ if(grepl("\\\\caption\\{.*?\\}", input)){
export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", input)
- }else{export <- input}
- }else{
- export <- sub("\\\\begin\\{tabular\\}", "\\\\begin{threeparttable}\n\\\\begin{tabular}", input)
- }
- export <- gsub(
+ }else{
+ export <- sub("\\\\begin\\{tabular\\}", "\\\\begin{threeparttable}\n\\\\begin{tabular}", input)
+ }
+ export <- gsub(
"\\\\end\\{tabular\\}",
paste0(
"\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
- ),
+ ),
export)
+ }
}
if(attr(input, "format")=="html"){
-
+ export <- input
}
return(export)
}
+
+
+