I am writing a document in R markdown (.rmd). I would like to be able to knit both Word and PDF outputs. I am having difficulty with figure numbering. With PDF output, the figures were automatically numbered (via Latex output of fig.lp?) But figures were not numbered in Word .
After much searching, I found code that will provide figure numbering in Word - but now I get double page numbering when knitting a PDF. I'm new, so I can't insert an image. But the figure caption looks like:
Figure 1. Figure 1. Blah Blah Blah
Is there a way to suppress the automatic numbering for PDF?
A similar question was asked here, but a solution was not given. My YAML header and figure numbering chunck are included below.
YAML:
output:
pdf_document:
fig_caption: yes
keep_tex: yes
word_document:
fig_caption: yes
Figure numbering code (found via http://galahad.well.ox.ac.uk/repro/)
figRef <- local({
tag <- numeric()
created <- logical()
used <- logical()
function(label, caption, prefix = options("figcap.prefix"),
sep = options("figcap.sep"), prefix.highlight = options("figcap.prefix.highlight")) {
i <- which(names(tag) == label)
if (length(i) == 0) {
i <- length(tag) + 1
tag <<- c(tag, i)
names(tag)[length(tag)] <<- label
used <<- c(used, FALSE)
names(used)[length(used)] <<- label
created <<- c(created, FALSE)
names(created)[length(created)] <<- label
}
if (!missing(caption)) {
created[label] <<- TRUE
paste0(prefix.highlight, prefix, " ", i, sep, prefix.highlight,
" ", caption)
} else {
used[label] <<- TRUE
paste(prefix, tag[label])
}
}
})
this is then called in chunk options as follows:
```{r, echo=FALSE, message=FALSE, fig.width=6, fig.cap=figRef("Ex-Airfoil", "Example NACA Airfoil")}
Is there a way to suppress the automatic numbering for PDF?
Sure. Add a format
variable for your output format and a handler for that format within figref
. With RStudio preview edition you could use format <- knitr::opts_knit$get("out.format")
but with the release version you'd need to set it manually.
Then in figref()
add whatever you desire for the output...
if ( format == "latex" ) return( caption )
if (!missing(caption)) {
--- >8 ---
Personally I'd use the preview edition and a switch statement for the handling. Along the lines of https://stackoverflow.com/a/27321367/173985.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With