【问题标题】:Knitr hook to add code before \documentclass line in tex file to avoid options clash with xcolorKnitr 钩子在 tex 文件中的 \documentclass 行之前添加代码,以避免选项与 xcolor 冲突
【发布时间】:2018-11-20 09:19:06
【问题描述】:

我正在尝试使用 rmarkdownknitr 创建一个 pdf 文档。我需要使用带有一些选项的xcolor tex 包(例如:[table][svgnames])。

每当我尝试在 YAML 标头中或在 pdf_document includes in_header: 下提到的序言 tex 文件中使用 - \usepackage[table]{xcolor} 时,我都会收到以下错误:

! LaTeX Error: Option clash for package xcolor

选项冲突是因为knitr 引擎pdf_document 也直接或通过另一个包间接加载xcolor 包。我怀疑是后者,因为最近我更新了几个 tex 包后出现了这个问题。

一个可能的解决方案是在 tex 文件的开头,\documentclass[]{article} 行之前的add \PassOptionsToPackage{table}{xcolor}。当我手动执行此操作时,问题已解决。

在前导 tex 文件或 YAML 标头中添加,仅在 tex 文件中的 \documentclass[] 行之后添加。

如何解决这个问题?

是否有任何knitr hook 函数可以在 tex 文件中将 \PassOptionsToPackage{}{} 行添加到 \documentclass[] 行之前。?

---
title: "xcolor options clash"
author: "xcolor, options clash"
header-includes:
- \usepackage[table]{xcolor}
output:
  pdf_document:
    dev: cairo_pdf
    fig_caption: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Passage

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

```{r cars}
summary(cars)
```

## Plots

```{r pressure, echo=FALSE}
plot(pressure)
```

【问题讨论】:

  • 我无法使用 rmarkdown 版本 1.10 和 knitr 版本 1.20 重现您的示例文档中的错误
  • 更新安装的tex包后能不能试试。至少fancyvrbbabel
  • Babel v3.27 和 fancyvrb v3.0

标签: r latex r-markdown knitr


【解决方案1】:

在将 LaTeX 包 fancyvrb 更新到 v3.0 后,我能够重现此问题。一种解决方案是利用 LaTeX 文档类也将它们的参数传递给所有加载的包这一事实:

---
title: "xcolor options clash"
author: "xcolor, options clash"
classoption: table
output:
  pdf_document:
    dev: cairo_pdf
    fig_caption: no
    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Passage

Lorem ipsum dolor sit amet, consectetur adipiscing elit ...

```{r cars}
summary(cars)
```

## Plots

```{r pressure, echo=FALSE}
plot(pressure)
```

这会生成一个以

开头的 LaTeX 文件
\documentclass[table]{article}
\usepackage{lmodern}
...

编译这个 LaTeX 文件,我得到一个日志文件,其中包含:

(/usr/share/texlive/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty
Package: fancyvrb 2018/11/01

Style option: `fancyvrb' v3.0 <2018/11/01> (tvz)
(/usr/share/texlive/texmf-dist/tex/latex/xcolor/xcolor.sty
Package: xcolor 2016/05/11 v2.12 LaTeX color extensions (UK)

(/usr/share/texlive/texmf-dist/tex/latex/graphics-cfg/color.cfg
File: color.cfg 2016/01/02 v1.6 sample color configuration
)
Package xcolor Info: Driver file: pdftex.def on input line 225.

(/usr/share/texlive/texmf-dist/tex/latex/colortbl/colortbl.sty
Package: colortbl 2018/05/02 v1.0c Color table columns (DPC)

我认为colortbl.sty 被加载的事实表明table 选项确实传递给xcolor 包。在正常的工作流程中,不需要最后这些步骤。

【讨论】:

    猜你喜欢
    • 2019-12-28
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 2014-08-13
    相关资源
    最近更新 更多