ekstroem
ekstroem

Reputation: 6171

Custom LaTeX template for bookdown

I've recently upgraded my computer with new OS, new version of R (4.0) and consequently all packages. When trying to compile a bookdown document to pdf that used to work, I now get the error message

LaTeX Error: Option clash for package xcolor.

If I look at the tex file that is generated then I see that the following packages are added by the bookdown package by default to the file.

\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\usepackage{xcolor}

I have a bunch of other LaTeX-packages that are loaded earlier via the _output.yml file and the line in_header: latex/preamble.tex and I'm guessing that the clash is due to one of those. If I manually remove the last line containing \usepackage{xcolor} then everything compiles nicely.

But how do I modify the custom template that is used to add these default packages listed above? I checked the templates listed by pandoc but none of those seem to add these packages.

Following the information in the bookdown book it is possible to add a custom template, but the default LaTeX template from pandoc doesn't include those package either, so where do they come from?

Where can I find the default template that includes these extra packages for me? Or can I perhaps force xcolor to work by including a \PassOptionsToPackage{something}{xcolor} earlier in my LaTeX preamble?

(I am using the bookdown::tufte_book2 format but I haven't found the packages when hunting through the templates in the tufte package)

Upvotes: 3

Views: 340

Answers (1)

sahir
sahir

Reputation: 366

Since bookdown 0.4 you can use a global option bookdown.post.latex to post-process the LaTeX output. So I added the following code in a chunk (with echo=FALSE) to index.Rmd:

options(bookdown.post.latex = function(x) {
  # x is the content of the LaTeX output file
  c('\\PassOptionsToPackage{table}{xcolor}',x)    
})

In my preamble.tex I had

\setlength{\arrayrulewidth}{0.5mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{2.5}

And the LaTeX table I used was:

\rowcolors{3}{green!80!yellow!50}{green!70!yellow!40}
\begin{tabular}{ |p{3cm}|p{3cm}|p{3cm}|  }
\hline
\multicolumn{3}{|c|}{Country List} \\
\hline
Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 \\
\hline
American Samoa & AS & ASM \\
Andorra & AD & AND   \\
Angola & AO & AGO \\
\hline
\end{tabular}

Upvotes: 1

Related Questions