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












2















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









share|improve this question




















  • 1





    I cannot reproduce the error with your example document using rmarkdown version 1.10 and knitr version 1.20

    – Ralf Stubner
    Nov 20 '18 at 10:44











  • Can you try it after updating the installed tex packages. Atleast fancyvrb and babel.

    – Crops
    Nov 20 '18 at 10:54











  • Babel v3.27 and fancyvrb v3.0

    – Crops
    Nov 20 '18 at 11:10


















2















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









share|improve this question




















  • 1





    I cannot reproduce the error with your example document using rmarkdown version 1.10 and knitr version 1.20

    – Ralf Stubner
    Nov 20 '18 at 10:44











  • Can you try it after updating the installed tex packages. Atleast fancyvrb and babel.

    – Crops
    Nov 20 '18 at 10:54











  • Babel v3.27 and fancyvrb v3.0

    – Crops
    Nov 20 '18 at 11:10
















2












2








2








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









share|improve this question
















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






r latex r-markdown knitr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 13:14









Ralf Stubner

13.9k21437




13.9k21437










asked Nov 20 '18 at 9:19









CropsCrops

2,08821942




2,08821942








  • 1





    I cannot reproduce the error with your example document using rmarkdown version 1.10 and knitr version 1.20

    – Ralf Stubner
    Nov 20 '18 at 10:44











  • Can you try it after updating the installed tex packages. Atleast fancyvrb and babel.

    – Crops
    Nov 20 '18 at 10:54











  • Babel v3.27 and fancyvrb v3.0

    – Crops
    Nov 20 '18 at 11:10
















  • 1





    I cannot reproduce the error with your example document using rmarkdown version 1.10 and knitr version 1.20

    – Ralf Stubner
    Nov 20 '18 at 10:44











  • Can you try it after updating the installed tex packages. Atleast fancyvrb and babel.

    – Crops
    Nov 20 '18 at 10:54











  • Babel v3.27 and fancyvrb v3.0

    – Crops
    Nov 20 '18 at 11:10










1




1





I cannot reproduce the error with your example document using rmarkdown version 1.10 and knitr version 1.20

– Ralf Stubner
Nov 20 '18 at 10:44





I cannot reproduce the error with your example document using rmarkdown version 1.10 and knitr version 1.20

– Ralf Stubner
Nov 20 '18 at 10:44













Can you try it after updating the installed tex packages. Atleast fancyvrb and babel.

– Crops
Nov 20 '18 at 10:54





Can you try it after updating the installed tex packages. Atleast fancyvrb and babel.

– Crops
Nov 20 '18 at 10:54













Babel v3.27 and fancyvrb v3.0

– Crops
Nov 20 '18 at 11:10







Babel v3.27 and fancyvrb v3.0

– Crops
Nov 20 '18 at 11:10














1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389743%2fknitr-hook-to-add-code-before-documentclass-line-in-tex-file-to-avoid-options-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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.






    share|improve this answer






























      3














      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.






      share|improve this answer




























        3












        3








        3







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 15:44

























        answered Nov 20 '18 at 13:14









        Ralf StubnerRalf Stubner

        13.9k21437




        13.9k21437






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389743%2fknitr-hook-to-add-code-before-documentclass-line-in-tex-file-to-avoid-options-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules