Цвет подписи стал fgcolor (knitr, Rnw) - PullRequest
0 голосов
/ 30 мая 2020

При использовании knitr с файлами Rnw цвет шрифта подписей, созданных внутри блока, становится fgcolor.

Эта проблема похожа на knitr kable: цвет текста в PDF от RNW серый . В данном случае это решение бесполезно.

Есть ли способ решить эту проблему? Взлом 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}

Example of color

EDIT I:

Я публикую sh рабочее решение для использования в будущем. Бенуа указал на ключевую галочку 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}

Example of color in figure environment

1 Ответ

1 голос
/ 30 мая 2020

Это должно делать то, что вы хотите, при условии, что графики, созданные вашими фрагментами кода 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}

enter image description here

Или вы можете (пере) определить 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}
...