Search code examples
pdfr-markdownrstudioknitr

Rmarkdown: Paragraph ended before \Gin@ii was complete


I am getting the following error

! Paragraph ended before \Gin@ii was complete.
<to be read again> 
\par 
l.398 

when trying to knit into pdf the following Rmarkdown in Rstudio, which involves defining a variant of \includegraphics.

---
output:
  pdf_document: 
    latex_engine: lualatex
header-include:
  - usepackage{graphicx}
---

\newcommand\IG[1]{\includegraphics[width = \textwidth]{#1}}

\IG{test.png}

Weirdly, the tex output does compile correctly when I execute it through lulatex outside of Rstudio (yes, I do have to use lualatex here).

The error seems to be related to https://tex.stackexchange.com/questions/37650/paragraph-ended-before-giniii-was-complete-while-inserting-image-with-inclu and Rmarkdown Error: "! Paragraph ended before \@fileswith@ptions was complete", but none of the solutions there help and I can't figure out what's going wrong in my case.

Edit: It weirdly seems like the error comes and goes. I get it sometimes, but not others (I just re-ran the above code and it went through this time).


Solution

  • Add the \newcommand to the YAML header, i.e.

    ---
    title: "Test"
    output:
      pdf_document: 
        latex_engine: lualatex
        keep_tex: yes
      html_document: default
    date: "2022-12-06"
    header-include:
      - usepackage{graphicx}
      - \newcommand\IG[1]{\includegraphics[width = \textwidth]{#1}}
    ---