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