Knitr 钩子在 tex 文件中的 \documentclass 行之前添加代码以避免选项与 xcolor 冲突
Knitr hook to add code before \documentclass line in tex file to avoid options clash with xcolor
我正在尝试使用 rmarkdown
和 knitr
创建 pdf 文档。我需要使用带有一些选项的 xcolor
tex 包(例如:[table]
或 [svgnames]
)。
每当我尝试使用 YAML header 中的 - \usepackage[table]{xcolor}
或 pdf_document
includes
in_header:
中提到的序言 tex 文件时,我收到以下错误:
! LaTeX Error: Option clash for package xcolor
选项冲突是因为 knitr
引擎 pdf_document
也直接或通过另一个包间接加载 xcolor
包。我怀疑是后者,因为最近我更新了几个 tex 包后问题突然出现了。
一个可能的解决方案是在tex文件的开头,\documentclass[]{article}
行之前add \PassOptionsToPackage{table}{xcolor}
。当我手动执行此操作时,问题就解决了。
在序言tex文件或YAML中添加它header,仅在tex文件中的\documentclass[]
行之后添加它。
如何解决这个问题?
有没有knitr
hook
函数在tex文件的\documentclass[]
行之前添加\PassOptionsToPackage{}{}
行?
---
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)
```
在将 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
包。在正常的工作流程中,不需要最后这些步骤。
我正在尝试使用 rmarkdown
和 knitr
创建 pdf 文档。我需要使用带有一些选项的 xcolor
tex 包(例如:[table]
或 [svgnames]
)。
每当我尝试使用 YAML header 中的 - \usepackage[table]{xcolor}
或 pdf_document
includes
in_header:
中提到的序言 tex 文件时,我收到以下错误:
! LaTeX Error: Option clash for package xcolor
选项冲突是因为 knitr
引擎 pdf_document
也直接或通过另一个包间接加载 xcolor
包。我怀疑是后者,因为最近我更新了几个 tex 包后问题突然出现了。
一个可能的解决方案是在tex文件的开头,\documentclass[]{article}
行之前add \PassOptionsToPackage{table}{xcolor}
。当我手动执行此操作时,问题就解决了。
在序言tex文件或YAML中添加它header,仅在tex文件中的\documentclass[]
行之后添加它。
如何解决这个问题?
有没有knitr
hook
函数在tex文件的\documentclass[]
行之前添加\PassOptionsToPackage{}{}
行?
---
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)
```
在将 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
包。在正常的工作流程中,不需要最后这些步骤。