【问题标题】:Possible knitr bug interaction with MC style可能与 MC 风格的 knitr bug 交互
【发布时间】:2021-12-26 05:22:59
【问题描述】:

我正在与 Morgan & Claypool 一起写一本书。直到今天,他们的书籍模板和样式文件(morgan-defs.sty 和 morgan2.sty)与 knitr 和 ggplot 图表完美配合。今天早上更新我的 LaTeX 包后,我收到了表单错误

包 xcolor 错误:未定义颜色 `fgcolor'。

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

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

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

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: r latex r-markdown knitr


    【解决方案1】:

    .pdf 文件在主目录中正确创建。

    不!出现错误后,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}
    

    【讨论】:

    • 感谢@samcarter_is_at_topanswers.xyz。你的回答解决了问题!我仍然很困惑为什么现在需要\definecolor{fgcolor}{rgb}{255,0,0} 行。就在几天前还没有必要。
    • 出现错误时发现还需要添加\definecolor{errorcolor}{rgb}{255,0,0}这一行。
    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多