可能的 knitr bug 与 MC 风格的交互

Possible knitr bug interaction with MC style

我正在与 Morgan & Claypool 合着一本书。直到今天,他们的书籍模板和样式文件(morgan-defs.stymorgan2.sty)与 knitrggplot 图表完美配合。今天早上更新我的 LaTeX 软件包后,我收到了形式为

的错误

程序包 xcolor 错误:未定义颜色“fgcolor”。

当包含创建 ggplot 图的任何 R 代码块时。 (在未更新 LaTeX 软件包的计算机上编译本书时没有收到错误。)

可以在 https://github.com/MatthewHeun/Possible-knitr-error 找到演示错误的 MWE Rstudio 项目。您可以在代码块上将 include=TRUE 切换为 include=FALSE 以消除错误。

尽管出现错误,但在 include=TRUE 时,图表在 figure 目录中正确创建,并且 .pdf 文件在主目录中正确创建。但是,该错误会阻止 .pdf 文件自动打开。

如有任何帮助,我们将不胜感激。

谢谢!

the .pdf file is created correctly in the main directory.

不!发生错误后,latex 仅恢复到足以对输出的其余部分进行语法检查的程度,不一定会产生合理的输出。如果在编译过程中仍然存在错误,请不要使用文档!

也就是说,您可以通过定义缺失的颜色(将 255,0,0 更改为您喜欢的任何颜色)来解决该问题:

% please use TexLive 2014 or later with the M&C macros freely
% available from tug.org or use any other recent version of LaTeX

\documentclass{book}

%the main style; default LibreCaslon font
\usepackage[raggedsec]{morgan2}
\usepackage{morgan-defs}

\definecolor{fgcolor}{rgb}{255,0,0}

% 
% Begin R and knitr setup
% 

<<setup, echo=FALSE, message=FALSE>>=

# Set default sizes for figures throughout the book.
# We can always override with options on each individual chunk.
knitr::opts_chunk$set(echo=FALSE,
                      fig.width = 6,    # inches
                      fig.height = 3.5, # inches
                      fig.align = "center",
                      fig.pos = "t")

library(ggplot2)     # For graphing functions
library(magrittr)    # For pipe operator, (%>%)
@


\begin{document}

Hello World!


% When include=TRUE, an error is thrown ("Package xcolor Error: Undefined color `fgcolor'.")
% Change to include=FALSE" to avoid the error.
<<causes_an_error, include=TRUE>>=
library(ggplot2)
data.frame(x = 1:10, y = 11:20) |>
  ggplot2::ggplot(mapping = ggplot2::aes_string(x = "x", y = "y")) +
  ggplot2::geom_line()
@


\end{document}