Custom output hooks in knitr

落花浮王杯 提交于 2019-12-11 02:40:02

问题


I am trying to create a chunk hook that can produce a \floatfoot{} as part of the output from a chunk.

Hopefully the following example makes clear what I am trying to achieve:

1. .Rnw file

Here is the knitr file.

\documentclass[a4paper]{article}
\title{Learn Moar knitr!}
\author{Foo Bar}
\date{\today}

\usepackage{blindtext}
\usepackage{floatrow}
\begin{document}
\blindtext

<<label=plotWithNotes, fig.lp='fig:', fig.cap='This is a figure'>>=
plot(rnorm(100), rbinom(n = 100, size = 1, prob = 0.5))
@

\end{document}

2. LaTeX chunk

\begin{figure}[]
\includegraphics[width=\maxwidth]{figure/plotWithNotes} \caption[This is a figure]{This is a figure\label{fig:plotWithNotes}}
\end{figure}

3. floatfoot

The markup \floatfoot{} provided by the floatrow package produces a footnote to the graph, and to show its use, I modify the LaTeX chunk above.

\begin{figure}[]
\includegraphics[width=\maxwidth]{figure/plotWithNotes} \caption[This is a figure]{This is a figure\label{fig:plotWithNotes}}
\floatfoot{\emph{Note:} This is a footer to the plot and contains some information about the plot.}
\end{figure}

Is there any way I can generate the text inside floatfoot above using a chunk option?

I made a start on writing such a function, but can't get it to work:

<<echo = TRUE, include = FALSE>>=
fnOutHookInsertFoot = function(x = NULL, options = NULL) {
  # find the location of the \end{figure}
  gsub('\\end{figure}', '\\floatfoot{Some text.}\\end{figure}', 
       x, fixed = TRUE)

}
# fnOutHookInsertFoot()
knit_hooks$set(plot = fnOutHookInsertFoot)
@

来源:https://stackoverflow.com/questions/22224773/custom-output-hooks-in-knitr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!