在Rnw/LaTeX中,使用针织品钩子的公共输出可能是在装饰一些花哨的环境--来自块的数据。
例如,特定于块的代码可以为表生成核心数据,而钩子代码在提供重复修饰之前和之后提供。
考虑以下片段:
\documentclass{article}
\begin{document}
<<myhooks, include=FALSE>>=
printhook=function(before, options, envir) {
if (before) {
return('\nCommon R \\LaTeX\\ in before-hook')
} else {
return('\nCommon R \\LaTeX\\ in after-hook')
}
}
knit_hooks$set(lprint = printhook)
@
<<test, results='asis', lprint=TRUE, echo=FALSE>>=
cat("R \\LaTeX\\ in current chunk\n")
@
\end{document}问题是,LaTeX输出大约如下所示:
\begin{kframe}
Common R \LaTeX\ in before-hook
\end{kframe}
R \LaTeX\ in current chunk
\begin{kframe}
Common R \LaTeX\ in after-hook
\end{kframe}钩子代码实际上不是asis,但它被包装在kframe环境中,这防止了将这三部分粘合在一起。
我们如何删除封装在kframe中的
发布于 2014-07-20 15:52:43
对于这个问题,我没有一个优雅的解决办法。下面是一个可能的解决方案。其基本思想是替换输出钩子chunk,当results == 'asis'时,不回显源代码,在代码块中不生成任何情节:
\documentclass{article}
\begin{document}
<<myhooks, include=FALSE>>=
local({
hook_chunk = knit_hooks$get('chunk')
knit_hooks$set(chunk = function(x, options) {
x = hook_chunk(x, options)
if (options$results == 'asis' && !options$echo && options$fig.num == 0) {
# remove all kframe's
gsub('\\\\(begin|end)\\{kframe\\}', '', x)
} else x
})
})
printhook=function(before, options, envir) {
if (before) {
return('\nCommon R \\LaTeX\\ in before-hook')
} else {
return('\nCommon R \\LaTeX\\ in after-hook')
}
}
knit_hooks$set(lprint = printhook)
@
<<test, results='asis', lprint=TRUE, echo=FALSE>>=
cat("R \\LaTeX\\ in current chunk\n")
@
\end{document}https://stackoverflow.com/questions/24845731
复制相似问题