Improve coding formats in footnote
diff --git a/R/footnote.R b/R/footnote.R
index eeb55e6..121066b 100644
--- a/R/footnote.R
+++ b/R/footnote.R
@@ -28,19 +28,19 @@
ids.ops <- read.csv(system.file("symbol_index.csv", package = "kableExtra"))
ids <- ids.ops[, notation]
ids.intable <- gsub("\\*", "\\\\*", ids)
- ids.simple <- c(
- "*", "\u2020", "\u2021", "\u00A7", "\u00B6",
- "**", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
- "***", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
- "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
- "****", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
- "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
- )
+ # ids.simple <- c(
+ # "*", "\u2020", "\u2021", "\u00A7", "\u00B6",
+ # "**", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
+ # "***", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
+ # "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
+ # "****", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
+ # "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
+ # )
#count the number of items in label and intable notation
count.label <- length(label)
count.intablenote <- sum(str_count(input, "\\[note\\]"))
- if (count.intablenote != 0 & count.label != count.intablenote){
+ if (count.intablenote != 0 & count.label != count.intablenote) {
warning(paste("You entered", count.label, "labels but you put",
count.intablenote, "[note] in your table."))
}
@@ -55,8 +55,7 @@
)[[1]],
"[0-9]{1,2}")))
-
-
+ # Pandoc ---------------------------
# Footnote solution for markdown and pandoc. It is not perfect as
# markdown doesn't support complex table formats but this solution
# should be able to satisfy people who don't want to spend extra
@@ -66,7 +65,7 @@
if (count.intablenote != 0) {
for (i in 1:count.intablenote) {
replace_note <- paste0("^", ids.intable[i], "^",
- paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
+ paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
export[which(str_detect(export, "\\[note\\]"))[1]] <-
sub("\\[note\\]", replace_note,
@@ -74,136 +73,148 @@
}
}
# Fix extra in table notation
- for(i in extra.notation){
+ for (i in extra.notation) {
export <- gsub(paste0("\\[note", i, "\\]"),
paste0("^", ids.intable[i], "^",
- paste0(rep(" ", 4 - ceiling(i/5)),
- collapse = "")),
+ paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")),
export)
}
- export[length(export)+1] <- ""
- export[length(export)+1] <- "__Note:__"
- export[length(export)+1] <- paste0(
+ 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"){
+ # LaTeX Tables --------------------------------
+ if (attr(input, "format") == "latex") {
# Clean the entry for labels
- label <- knitr:::escape_latex(label)
- label <- gsub("\\\\", "\\\\\\\\", label)
+ label <- escape_latex(label)
+ label <- gsub("\\\\", "\\\\\\\\", label)
kable_info <- magic_mirror(input)
- if(kable_info$tabular == "longtable"){
- if(notation != "number"){
- warning("Currently, if you enabled longtable in kable, you can only use",
- " number as your footnote notations. ")
- }
- if(threeparttable == T){
- warning("Currently, threeparttable does not support longtable.")
+ if (kable_info$tabular == "longtable") {
+ if (notation != "number") {
+ warning("Notation is set to 'number' and other formats are not supported.")
+ notation <- "number"
}
# 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 (threeparttable == T) {
+ warning("Threeparttable does not support longtable.")
+ threeparttable = F
+ }
# Longtable doesn't support footnote in caption directly.
# See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
- count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
- if (count.in.caption.note != 0){
- # Since caption is the first part of table, we can just start
- caption.footnote <- paste0("\\\\addtocounter{footnote}{-", count.in.caption.note, "}")
- for(i in 1:count.in.caption.note){
+
+ count.in.caption.note <- 0
+ if (!is.na(kable_info$caption)) {
+ count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
+ }
+ if (count.in.caption.note != 0) {
+ caption.footnote <- paste0("\\\\addtocounter{footnote}{-",
+ count.in.caption.note, "}")
+ for (i in 1:count.in.caption.note) {
export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
caption.footnote <- paste0(
- caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}"
- )
+ caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{",
+ label[i], "}")
}
- if (str_detect(export, "\\\\toprule")){
+ if (str_detect(export, "\\\\toprule")) {
export <- sub("\\\\toprule",
paste0("\\\\toprule\n", caption.footnote), export)
- }else{
+ } else {
export <- sub("\\\\hline",
paste0("\\\\hline\n", caption.footnote), export)
}
}
- for(i in (count.in.caption.note + 1):count.intablenote){
+ for (i in (count.in.caption.note + 1):count.intablenote) {
export <- sub("\\[note\\]",
paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
}
- for(i in extra.notation){
+ for (i in extra.notation) {
export <- gsub(paste0("\\[note", i, "\\]"),
paste0("\\\\footnotemark[", i, "]"),
export)
}
- }else{
+ } else {
# Replace in-table notation with appropriate symbol
- for(i in 1:count.intablenote){
- export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export)
+ for (i in 1:count.intablenote) {
+ export <- sub("\\[note\\]",
+ paste0("\\\\textsuperscript{", ids.intable[i], "}"),
+ export)
}
# Fix extra in table notation
- for(i in extra.notation){
+ for (i in extra.notation) {
export <- gsub(paste0("\\[note", i, "\\]"),
paste0("\\\\textsuperscript{", ids.intable[i], "}"),
export)
}
- if(threeparttable == T){
+ if (threeparttable == T) {
# generate footer with appropriate symbol
footer <- ""
- for(i in 1:count.label){
+ for (i in 1:count.label) {
footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
}
- if(grepl("\\\\caption\\{.*?\\}", export)){
- export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
- }else{
+ if (grepl("\\\\caption\\{.*?\\}", export)) {
+ export <- sub("\\\\caption\\{",
+ "\\\\begin{threeparttable}\n\\\\caption{",
+ export)
+ } else {
export <- sub("\\\\begin\\{tabular\\}",
- "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
+ "\\\\begin{threeparttable}\n\\\\begin{tabular}",
+ export)
}
export <- gsub(
"\\\\end\\{tabular\\}",
- paste0(
- "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
- footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
- ),
+ paste0("\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
+ footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"),
export)
- }else{
- table.width <- max(nchar(
- str_replace_all(
- str_replace_all(kable_info$contents, "\\[note\\]", ""),
- "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
- footer <- ""
- for (i in 1:count.label){
- label.wrap <- strwrap(label[i], table.width)
- footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
- "}{l}{\\\\textsuperscript{", ids[i], "} ",
- label.wrap[1], "}\\\\\\\\\n")
- if(length(label.wrap) > 1){
- for (j in 2:length(label.wrap)){
- footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
- "}{l}{", label.wrap[j], "}\\\\\\\\\n")
- }
+ } else {
+ table.width <- max(nchar(
+ str_replace_all(
+ str_replace_all(kable_info$contents, "\\[note\\]", ""),
+ "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
+ footer <- ""
+ for (i in 1:count.label) {
+ label.wrap <- strwrap(label[i], table.width)
+ footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
+ "}{l}{\\\\textsuperscript{", ids[i], "} ",
+ label.wrap[1], "}\\\\\\\\\n")
+ if (length(label.wrap) > 1) {
+ for (j in 2:length(label.wrap)) {
+ footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
+ "}{l}{", label.wrap[j], "}\\\\\\\\\n")
}
}
- export <- gsub("\\\\end\\{tabular\\}",
- paste0(footer, "\\\\end{tabular}"), export)
+ }
+ export <- gsub("\\\\end\\{tabular\\}",
+ paste0(footer, "\\\\end{tabular}"),
+ export)
}
}
}
- if(attr(input, "format")=="html"){
+
+ # HTML Tables -------------------
+ if (attr(input, "format") == "html") {
# Clean the entry for labels
- label <- knitr:::escape_html(label)
+ label <- escape_html(label)
# Replace in-table notation with appropriate symbol
- for(i in 1:count.intablenote){
- export <- sub("\\[note\\]", paste0("<sup>", ids.intable[i], "</sup>"), export)
+ for (i in 1:count.intablenote) {
+ export <- sub("\\[note\\]",
+ paste0("<sup>", ids.intable[i], "</sup>"),
+ export)
}
# Fix extra in table notation
- for(i in extra.notation){
+ for (i in extra.notation) {
export <- gsub(paste0("\\[note", i, "\\]"),
paste0("<sup>", ids.intable[i], "</sup>"),
export)
@@ -211,7 +222,7 @@
# Build footer
footer <- "<tfoot>\n"
- for(i in 1:count.label){
+ for (i in 1:count.label) {
footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ",
"colspan='100%'><sup>", ids[i],"</sup> ",
label[i], "</td>\n</tr>\n")
diff --git a/R/from_knitr.R b/R/from_knitr.R
new file mode 100644
index 0000000..f782a19
--- /dev/null
+++ b/R/from_knitr.R
@@ -0,0 +1,25 @@
+# These functions are imported from knitr/highr as `:::` is not recommended by
+# CRAN
+
+#' escape special LaTeX characters
+#' @author Yihui Xie
+escape_latex <- function(x, newlines = FALSE, spaces = FALSE) {
+ x = gsub('\\\\', '\\\\textbackslash', x)
+ x = gsub('([#$%&_{}])', '\\\\\\1', x)
+ x = gsub('\\\\textbackslash', '\\\\textbackslash{}', x)
+ x = gsub('~', '\\\\textasciitilde{}', x)
+ x = gsub('\\^', '\\\\textasciicircum{}', x)
+ if (newlines) x = gsub('(?<!\n)\n(?!\n)', '\\\\\\\\', x, perl = TRUE)
+ if (spaces) x = gsub(' ', '\\\\ \\\\ ', x)
+ x
+}
+
+#' escape special HTML characters
+#' @author Yihui Xie
+escape_html <- function(x) {
+ x = gsub('&', '&', x)
+ x = gsub('<', '<', x)
+ x = gsub('>', '>', x)
+ x = gsub('"', '"', x)
+ x
+}