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]
).
I am trying to create a pdf document using rmarkdown
and knitr
. I need to use the xcolor
tex package with some options (eg: [table]
or [svgnames]
).
每当我尝试使用YAML标头中的- \usepackage[table]{xcolor}
或pdf_document
includes
in_header:
下提到的前导tex文件时,都会出现以下错误:
Whenever I try to do so either using - \usepackage[table]{xcolor}
in the YAML header or in a preamble tex file mentioned under the pdf_document
includes
in_header:
, I am getting the following error:
! LaTeX Error: Option clash for package xcolor
选项冲突是因为knitr
引擎pdf_document
还将直接或通过另一个包间接加载xcolor
包.我怀疑是后者,因为在我更新了一些tex软件包后,这个问题最近才出现.
The option clash is because, the knitr
engine pdf_document
is also loading the xcolor
package either directly or indirectly through another package. I suspect the latter, because the problem cropped up recently after I updated a few tex packages.
可能的解决方案是添加\PassOptionsToPackage{table}{xcolor}
在tex文件的开头,在\documentclass[]{article}
行之前.当我手动执行此操作时,问题已解决.
A possible solution is to add \PassOptionsToPackage{table}{xcolor}
at the beginning of the tex file, before the \documentclass[]{article}
line. When I manualy do this, the problem is fixed.
将其添加到前导tex文件或YAML标头中,仅将其添加到tex文件中的\documentclass[]
行之后.
Adding it in preamble tex file or in YAML header, adds it only after the \documentclass[]
line in tex file.
如何解决此问题?
在tex文件中,是否有任何knitr
hook
函数在\documentclass[]
行之前添加\PassOptionsToPackage{}{}
行??
Is there any knitr
hook
function to add the \PassOptionsToPackage{}{}
line before \documentclass[]
line in tex file.?
---
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文档类还将其参数也传递给所有已加载的软件包的事实:
I was able to reproduce this after updating the LaTeX package fancyvrb
to v3.0. One solution is to use the fact that LaTeX document classes pass their arguments also to all loaded packages:
---
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)
```
这将产生一个以
\documentclass[table]{article}
\usepackage{lmodern}
...
编译此LaTeX文件时,我得到一个包含以下内容的日志文件:
Compiling this LaTeX file I get a log file containing:
(/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
包的指示.在正常的工作流程中,不需要最后这些步骤.
I take the fact that colortbl.sty
gets loaded as an indication that the table
option is indeed passed on to the xcolor
package. In the normal workflow these last steps are not needed.
这篇关于Knitr钩子在tex文件中的\ documentclass行之前添加代码,以避免选项与xcolor冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!