Knitr hook to add code before \\documentclass line in tex file to avoid options clash with xcolor

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

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]).

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 

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.

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.

Adding it in preamble tex file or in YAML header, adds it only after the \documentclass[] line in tex file.

How to fix this ?

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) ``` 

回答1:

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) ``` 

This produces a LaTeX file beginning with

\documentclass[table]{article} \usepackage{lmodern} ... 

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) 

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!