字幕颜色变为 fgcolor (knitr, Rnw)
Color of captions becames fgcolor (knitr, Rnw)
将 knitr 与 Rnw 文件一起使用时,块内创建的字幕字体颜色变为 fgcolor。
这个问题类似于。此解决方案在这种情况下没有用。
有什么办法可以解决这个问题吗?黑客 knit_hooks$set(plot = myfunction)?如何?
谢谢。
% Minimal.Rnw
\documentclass{tufte-book}
\begin{document}
Color of normal font is black.
<<fig.env='marginfigure',fig.cap="The color of the font of the caption is fgcolor">>=
plot(1:10)
@
\begin{marginfigure}
\caption{This color is black.}
\end{marginfigure}
\end{document}
编辑我:
我发布了一个可行的解决方案以供将来参考。 Benoit 指出了关键的勾号 fig.show='hide'
。不幸的是,它没有编写图形环境。所以我创建了一个名为 colorcaption
的新钩子来解决这个问题。使用您想要的颜色调用此选项 (black
、red
、green
、...)。
% Minimal.Rnw
\documentclass{tufte-book}
\begin{document}
<<echo=FALSE>>=
library(knitr)
knit_hooks$set(colorcaption = function(before, options, envir){
## Hacked from hook_plot_custom
if (before) return() # run hook after the chunk
ext = "pdf" # original: options$fig.ext %n% dev2ext(options$dev)
hook = knit_hooks$get('plot')
##
n = options$fig.num
if (n == 0L) n = options$fig.num = 1L # make sure fig.num is at least 1
res = unlist(lapply(seq_len(n), function(i) {
options$fig.cur = i
hook(fig_path(ext, options, i), knitr:::reduce_plot_opts(options))
}), use.names = FALSE)
res <- paste(res, collapse = '')
## My hack. Put the color after the end of kframe
sub("\end{kframe}",paste0("\end{kframe}\color{",options$colorcaption, "}"),res,fixed=TRUE)
})
@
Color of normal font is black.
<<colorcaption='red',fig.show='hide',fig.env='marginfigure',fig.cap="The color of the font of the caption is colorcaption",>>=
1:10
plot(1:10)
10:1
@
Hello
\begin{marginfigure}
\caption{This color is black.}
\end{marginfigure}
\end{document}
如果您的 R 代码块生成的图存储在 figure
文件夹中,这应该可以满足您的要求:
\documentclass{tufte-book}
\begin{document}
Color of normal font is black.
<<test1, fig.show = 'hide'>>=
plot(1:10)
@
\begin{marginfigure}
\includegraphics[width = \textwidth]{figure/test1-1}
\caption{this is now the correct color}
\end{marginfigure}
\begin{marginfigure}
\caption{This color is black.}
\end{marginfigure}
\end{document}
或者您可以在序言中(重新)定义 fgcolor
:
\documentclass{tufte-book}
\definecolor{fgcolor}{RGB}{0,0,0}
\begin{document}
Color of normal font is black.
<<fig.env='marginfigure',fig.cap="The color of the font of the caption is fgcolor">>=
plot(1:10)
@
\begin{marginfigure}
\caption{This color is black.}
\end{marginfigure}
\end{document}
将 knitr 与 Rnw 文件一起使用时,块内创建的字幕字体颜色变为 fgcolor。
这个问题类似于
有什么办法可以解决这个问题吗?黑客 knit_hooks$set(plot = myfunction)?如何? 谢谢。
% Minimal.Rnw
\documentclass{tufte-book}
\begin{document}
Color of normal font is black.
<<fig.env='marginfigure',fig.cap="The color of the font of the caption is fgcolor">>=
plot(1:10)
@
\begin{marginfigure}
\caption{This color is black.}
\end{marginfigure}
\end{document}
编辑我:
我发布了一个可行的解决方案以供将来参考。 Benoit 指出了关键的勾号 fig.show='hide'
。不幸的是,它没有编写图形环境。所以我创建了一个名为 colorcaption
的新钩子来解决这个问题。使用您想要的颜色调用此选项 (black
、red
、green
、...)。
% Minimal.Rnw
\documentclass{tufte-book}
\begin{document}
<<echo=FALSE>>=
library(knitr)
knit_hooks$set(colorcaption = function(before, options, envir){
## Hacked from hook_plot_custom
if (before) return() # run hook after the chunk
ext = "pdf" # original: options$fig.ext %n% dev2ext(options$dev)
hook = knit_hooks$get('plot')
##
n = options$fig.num
if (n == 0L) n = options$fig.num = 1L # make sure fig.num is at least 1
res = unlist(lapply(seq_len(n), function(i) {
options$fig.cur = i
hook(fig_path(ext, options, i), knitr:::reduce_plot_opts(options))
}), use.names = FALSE)
res <- paste(res, collapse = '')
## My hack. Put the color after the end of kframe
sub("\end{kframe}",paste0("\end{kframe}\color{",options$colorcaption, "}"),res,fixed=TRUE)
})
@
Color of normal font is black.
<<colorcaption='red',fig.show='hide',fig.env='marginfigure',fig.cap="The color of the font of the caption is colorcaption",>>=
1:10
plot(1:10)
10:1
@
Hello
\begin{marginfigure}
\caption{This color is black.}
\end{marginfigure}
\end{document}
如果您的 R 代码块生成的图存储在 figure
文件夹中,这应该可以满足您的要求:
\documentclass{tufte-book}
\begin{document}
Color of normal font is black.
<<test1, fig.show = 'hide'>>=
plot(1:10)
@
\begin{marginfigure}
\includegraphics[width = \textwidth]{figure/test1-1}
\caption{this is now the correct color}
\end{marginfigure}
\begin{marginfigure}
\caption{This color is black.}
\end{marginfigure}
\end{document}
或者您可以在序言中(重新)定义 fgcolor
:
\documentclass{tufte-book}
\definecolor{fgcolor}{RGB}{0,0,0}
\begin{document}
Color of normal font is black.
<<fig.env='marginfigure',fig.cap="The color of the font of the caption is fgcolor">>=
plot(1:10)
@
\begin{marginfigure}
\caption{This color is black.}
\end{marginfigure}
\end{document}