Search code examples
rlatexr-markdownmarkdown

convert Latex to RMD


My Rmarkdown return error when I'm trying to load the graphicx or geometry package with the following error :

---
output:
  bookdown::pdf_document2: 
    keep_tex: true
header-includes:
  - \usepackage[demo]{graphicx}
  - \usepackage{hyperref}
  - \usepackage{array}
  - \usepackage{lastpage}
  - \usepackage{lipsum}
  - \usepackage{fancyhdr}
  - \usepackage[hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt]{geometry}
---

sometext


processing file: test.Rmd
output file: test.knit.md

! LaTeX Error: Option clash for package graphicx.

Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info.
Execution halted

I am trying to convert the following latex to rmarkdown with no success :(

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[demo]{graphicx}
\usepackage{hyperref}
\usepackage{array}
\usepackage{lastpage}
\usepackage{lipsum}

\usepackage{fancyhdr}
\usepackage[hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt]{geometry}

\setlength{\parindent}{0.95cm}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[CE,CO,LE,LO,RE,RO]{} %% clear out all headers
\fancyhead[C]{%
          \begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|}
          \hline
          \includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
          \centering
          \Huge{TITLE} &
          \centering
          \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\
          Data: 17/05/2013\\
          Rev. 0}\tabularnewline
          \hline
          \end{tabular}%
}
\fancyfoot[CE,CO,LE,LO,RE,RO]{} %% clear out all footers
\fancyfoot[C]{%
          \begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|}
          \hline
          \includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
          \centering
          \Huge{TITLE} &
          \centering
          \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\
          Data: 17/05/2013\\
          Rev. 0}\tabularnewline
          \hline
          \end{tabular}%
}

\begin{document}
\chapter{Chapter title}
%% add this if you want the fancy style also on the first page of a chapter:
\thispagestyle{fancy}
\section{Section title}
\lipsum[1-10]
\end{document}

I'm on windows 10, using R 4.2.3 and Texlive full distribution is already installed and I am able to compile the .tex example !

if I use the .tex directly in RMD I will get error like :

---
output:
  pdf_document: 
    keep_tex: true
header-includes:
  - \usepackage{array}
  - \usepackage{lastpage}
  - \usepackage{lipsum}
  - \usepackage{fancyhdr}
  - \geometry{hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt}
  - \setlength{\parindent}{0.95cm}

  - \pagestyle{fancy}
  - \renewcommand{\headrulewidth}{0pt}
  - \fancyhead[CE,CO,LE,LO,RE,RO]{} %% clear out all headers
  - \fancyhead[C]{%
            \begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|}
            \hline
            \includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
            \centering
            \Huge{TITLE} &
            \centering
            \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\
            Data: 17/05/2013\\
            Rev. 0}\tabularnewline
            \hline
            \end{tabular}%
}
  - \fancyfoot[CE,CO,LE,LO,RE,RO]{} %% clear out all footers
  - \fancyfoot[C]{%
            \begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|}
            \hline
            \includegraphics[height=1.5cm,width=2.5cm]{logo.png} &
            \centering
            \Huge{TITLE} &
            \centering
            \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\
            Data: 17/05/2013\\
            Rev. 0}\tabularnewline
            \hline
            \end{tabular}%
}
classoption: demo
---


\chapter{Chapter title}
%% add this if you want the fancy style also on the first page of a chapter:
\thispagestyle{fancy}
\section{Section title}
\lipsum[1-10]

Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 23, column 17
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted

Solution

  • The problem is that rmarkdown already loads truckloads of packages before it even inserts your header-includes. You thus are no longer able to load them with the options you want.

    As a workaround, you can

    • just remove the hyperref package, as it is already loaded

    • replace \usepackage[...]{geometry} with \geometry{...} to modify the options of the already loaded geometry package

    • getting the demo option to graphicx is more tricky. Do you really need it? It will prevent all your images from showing up. If you really need it, you could force in on all packages with classoption: demo. Notice that this will pass the option to all loaded packages. If other packages also have this option, the results might change.

    • if you actually want to replicate your latex document, you might also want to change the document class as bookdown::pdf_document2 by default uses the article class which is very different then your book class.


    ---
    output:
      bookdown::pdf_document2: 
        keep_tex: true
    header-includes:
      - \usepackage{array}
      - \usepackage{lastpage}
      - \usepackage{lipsum}
      - \usepackage{fancyhdr}
      - \geometry{hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt}
    classoption: demo
    ---
    
    sometext
    

    The full document:

    ---
    output:
      bookdown::pdf_document2: 
        keep_tex: true
    documentclass: book
    classoption: demo   
    header-includes:
      - \usepackage{array}
      - \usepackage{lastpage}
      - \usepackage{lipsum}
      - \usepackage{fancyhdr}
      - \usepackage{geometry}
      - \geometry{hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt}
      - \setlength{\parindent}{0.95cm}
      - \pagestyle{fancy}
      - \renewcommand{\headrulewidth}{0pt}
      - \fancyhead[CE,CO,LE,LO,RE,RO]{}
      - \fancyhead[C]{\begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|} \hline\includegraphics[height=1.5cm,width=2.5cm]{logo.png} &           \centering \Huge{TITLE} & \centering \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\ Data{:} 17/05/2013\\  Rev. 0}\tabularnewline \hline \end{tabular}}
      - \fancyfoot[CE,CO,LE,LO,RE,RO]{}
      - \fancyfoot[C]{\begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|} \hline \includegraphics[height=1.5cm,width=2.5cm]{logo.png} & \centering \Huge{TITLE} & \centering \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\ Data{:} 17/05/2013\\ Rev. 0}\tabularnewline \hline \end{tabular}}
    ---
    
    
    # Chapter title
    \thispagestyle{fancy}
    ## Section title
    \lipsum[1-10]