Possible knitr bug interaction with MC style

Viewed 104

I'm writing a book with Morgan & Claypool. Until today, their book template and style files (morgan-defs.sty and morgan2.sty) worked perfectly with knitr and ggplot graphs. After an update to my LaTeX packages this morning, I'm receiving errors of the form

Package xcolor Error: Undefined color `fgcolor'.

when including ANY R code chunk that creates a ggplot graph. (I do not receive the error when compiling the book on a computer whose LaTeX packages have not been updated.)

A MWE Rstudio project that demonstrates the error can be found at https://github.com/MatthewHeun/Possible-knitr-error. You can switch include=TRUE to include=FALSE on the code chunk to eliminate the error.

Despite the error, when include=TRUE, the graph is created correctly in the figure directory, and the .pdf file is created correctly in the main directory. However, the error prevents the .pdf file from opening automatically.

Any help would be appreciated.

Thanks!

1 Answers

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

NO! After an error, latex only recovers enough to syntax check the rest of the output, not necessarily producing sensible output. Don't use a document if there are still errors during the compilation!

That being said, you can work around the problem by defining the missing colour (change 255,0,0 to whatever colour you like):

% 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}
Related