R/hook_anchor.r

Defines functions hook_anchor

Documented in hook_anchor

#' Anchor Chunk Hook Extension for package:knitr
#' 
#' \pkg{knitr} hook functions are called when the corresponding chunk options 
#'  are not \code{NULL} to do additional jobs beside the R code in chunks. 
#'  \pkg{kfigr} provides the hook "anchor" which adds an HTML anchor tag 
#'  immediately above a code chunk.
#'
#' @details the function hook_anchor is set as a hook in \pkg{knitr} when 
#'   \pkg{kfigr} is attached (and removed when \pkg{kfigr} is detached). It 
#'   writes an HMTL anchor tag directly above a code chunk in the form 
#'   \code{<a name="chunk-name"></a>} where \code{chunk-name} is the chunk 
#'   label contained in \code{options$label}.
#'
#' @references \url{http://yihui.org/knitr/hooks#chunk-hooks}
#'
#' @seealso \code{\link{figr}}, \code{\link{anchors}}
#'
#' @param before,options,envir see references
#'
#' @examples
#' \dontrun{
#' require(knitr)
#' knit_hooks$set(anchor = hook_anchor)
#' # then in code chunks, use e.g. the option anchor = "figure"
#' }
#'
#' @export
hook_anchor <- function(before, options, envir){
  if (before){  
    invisible(index(options$label, options$anchor))
    paste('<a name="', options$label, '"></a>', sep='')
  }
}

Try the kfigr package in your browser

Any scripts or data that you put into this service are public.

kfigr documentation built on June 10, 2021, 9:10 a.m.