Finished all latex part of footnote. Finished latex part of magic_mirror
diff --git a/R/footnote.R b/R/footnote.R
index e35a9f1..be0ea04 100644
--- a/R/footnote.R
+++ b/R/footnote.R
@@ -58,6 +58,12 @@
ids <- ids.ops[,notation]
# pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^
ids.intable <- gsub("\\*", "\\\\*", ids)
+ ids.simple <- c(
+ "*", "†", "‡", "§", "¶",
+ "**", "††", "‡‡", "§§", "¶¶",
+ "***", "†††", "‡‡‡", "§§§", "¶¶¶",
+ "****", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
+ )
#count the number of items in label and intable notation
count.label = length(label)
@@ -69,6 +75,16 @@
export <- input
+ # Find out if there are any extra in-table notations needed to be corrected
+ extra.notation <- as.numeric(
+ str_extract(
+ str_extract_all(
+ paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
+ )[[1]],
+ "[0-9]{1,2}"))
+
+
+
# 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
@@ -79,21 +95,15 @@
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]))),
+ paste0(rep(" ", 4 - ceiling(i/5)),
collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]])
}
}
# Fix extra in table notation
- extra.notation <- as.numeric(
- str_extract(
- str_extract_all(
- paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
- )[[1]],
- "[0-9]{1,2}"))
for(i in extra.notation){
export <- gsub(paste0("\\[note", i, "\\]"),
paste0("^", ids.intable[i], "^",
- paste0(rep(" ", 4 - nchar(as.character(ids[i]))),
+ paste0(rep(" ", 4 - ceiling(i/5)),
collapse = "")),
export)
}
@@ -108,46 +118,105 @@
# Generate latex table footnote --------------------------------
if(attr(input, "format")=="latex"){
kable_info <- magic_mirror(input)
- if(threeparttable == F | latex.tabular == "longtable"){
+ 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 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 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){
+ # 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){
+ export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
+ caption.footnote <- paste0(
+ caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}"
+ )
+ }
+
+ if (str_detect(export, "\\\\toprule")){
+ export <- sub("\\\\toprule",
+ paste0("\\\\toprule\n", caption.footnote), export)
+ }else{
+ export <- sub("\\\\hline",
+ paste0("\\\\hline\n", caption.footnote), export)
+ }
+ }
+ for(i in (count.in.caption.note + 1):count.intablenoot){
export <- sub("\\[note\\]",
- paste0("\\\\footnote[", ids[i], "]{", label[i], "}"), export)
+ paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
+ }
+ for(i in extra.notation){
+ export <- gsub(paste0("\\[note", i, "\\]"),
+ paste0("\\\\footnotemark[", i, "]"),
+ export)
}
}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")
- }
-
# Replace in-table notation with appropriate symbol
for(i in 1:count.intablenoot){
- export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), export)
+ export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export)
}
- if(grepl("\\\\caption\\{.*?\\}", export)){
- export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
- }else{
- export <- sub("\\\\begin\\{tabular\\}",
- "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
+ # Fix extra in table notation
+ for(i in extra.notation){
+ export <- gsub(paste0("\\[note", i, "\\]"),
+ paste0("\\\\textsuperscript{", ids.intable[i], "}"),
+ export)
}
- export <- gsub(
- "\\\\end\\{tabular\\}",
- paste0(
- "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
- footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
- ),
- export)
+ if(threeparttable == T){
+ # generate footer with appropriate symbol
+ footer <- ""
+ 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{
+ export <- sub("\\\\begin\\{tabular\\}",
+ "\\\\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}"
+ ),
+ 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")
+ }
+ }
+ }
+ export <- gsub("\\\\end\\{tabular\\}",
+ paste0(footer, "\\\\end{tabular}"), export)
+ }
}
}
if(attr(input, "format")=="html"){
+
}
return(export)
}
diff --git a/R/magic_mirror.R b/R/magic_mirror.R
index af77b50..d9d78d1 100644
--- a/R/magic_mirror.R
+++ b/R/magic_mirror.R
@@ -33,13 +33,21 @@
kable_info$booktabs <- ifelse(grepl("\\\\toprule", input), TRUE, FALSE)
# Align
kable_info$align <- gsub("\\|", "", str_match(
- input, paste0("\\\\begin\\{", kable_info$tabular,"\\}\\{(.*?)\\}"))[2])
+ input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
# N of columns
kable_info$ncol <- nchar(kable_info$align)
- # N of rows
- kable_info$nrow <- str_count(input, "\\\\\n")
# Caption
kable_info$caption <- str_match(input, "caption\\{(.*?)\\}")[2]
+ # N of rows
+ kable_info$nrow <- str_count(input, "\\\\\n") -
+ # in the dev version (currently as of 11.2015) of knitr, when longtable is
+ # enabled, caption is moved inside the tabular environment. As a result,
+ # the number of rows should be adjusted.
+ ifelse(
+ kable_info$tabular == "longtable" & !is.na(kable_info$caption) &
+ !str_detect(input, "\\\\begin\\{table\\}\\n\\n\\\\caption"),
+ 1,0
+ )
# Contents
kable_info$contents <- str_match_all(input, "\n(.*)\\\\\\\\")[[1]][,2]
# Column names
diff --git a/tests/testthat/test-add_footnote.r b/tests/testthat/test-add_footnote.r
index 4a8e32c..1083e6c 100644
--- a/tests/testthat/test-add_footnote.r
+++ b/tests/testthat/test-add_footnote.r
@@ -8,3 +8,5 @@
latextable_2 <- kable(rtable, format = "latex", caption = "Table", row.names = T, align = c("l", "c", "c"))
latextable_3 <- kable(rtable, format = "latex", booktab = T)
latextable_4 <- kable(rtable, format = "latex", booktab = T, caption = "Table")
+latextable_5 <- kable(rtable, format = "latex", longtable = T)
+latextable_6 <- kable(rtable, format = "latex", longtable = T, caption = "Table long")