Search code examples
rpdflatexr-markdownxelatex

! LaTeX Error: Missing \begin{document}. Error: LaTeX failed to compile paper_template.tex


I am trying to prepare a paper template using R Markdown and Latex for the upcoming semester. I learn as I go through and the recent problem I am facing is the error message of ! LaTeX Error: Missing \begin{document}. Error: LaTeX failed to compile paper_template.tex when I knitr my Rmd file. Here is the code for the R-Markdown file;

---
title: "Paper Template"
author: "AAA"
abstract: ""
output:
  pdf_document:
    latex_engine: xelatex
    includes:
      in_header: customized_paper_template.tex
date: "2024-06-29"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)```

\newpage

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

## Including Plots

You can also embed plots, for example: This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

You can also embed plots, for example: This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

### Including Extra Plots

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

and here is the code for customized_paper_template.tex file;

\usepackage{fontspec}
\usepackage{geometry}
\geometry{a4paper, margin=1in}
\usepackage{setspace}
\renewcommand{\baselinestretch}{1.25}

% Set body text font
\setmainfont{Kepler Std Light Semicondensed}

% Set title fonts
\newfontfamily\titlefont{Tiempos Headline}

% Set the font size for the document
\AtBeginDocument{\fontsize{12pt}{14pt}\selectfont}

% Define title formatting
\usepackage{titlesec}
 \titleformat{\section}
  {\normalfont\LARGE\titlefont} % Title style
  {\thesection} % Section number
  {1em} % Space between number and title
  {} % No additional formatting before the title text
   [\titlerule[0.5pt]] % Add a line under the title with 0.5pt thickness

\titleformat{\subsection}
  {\normalfont\Large\titlefont} % Title style
  {\thesubsection} % Section number
  {1em} % Space between number and title
  {} % No additional formatting before the title text
  [\titlerule[0.4pt]] % Add a line under the title with 0.4pt thickness

\titleformat{\subsubsection}
  {\normalfont\normalsize\titlefont} % Title style
  {\thesubsubsection} % Section number
  {1em} % Space between number and title
  {} % No additional formatting before the title text
  [\titlerule[0.3pt]] % Add a line under the title with 0.3pt thickness

\titleformat{\paragraph}
  {\normalfont\normalsize\titlefont} % Title style
  {\theparagraph} % Section number
  {1em} % Space between number and title
  {} % No additional formatting before the title text
  [\titlerule[0.2pt]] % Add a line under the title with 0.2pt thickness

\titleformat{\subparagraph}
  {\normalfont\normalsize\titlefont} % Title style
  {\thesubparagraph} % Section number
  {1em} % Space between number and title
  {} % No additional formatting before the title text
  [\titlerule[0.2pt]] % Add a line under the title with 0.2pt thickness

Solution

  • Use [{\titlerule[0.2pt]}] etc. to hide the square brackets from the optional argument:

    % Define title formatting
    \usepackage{titlesec}
     \titleformat{\section}
      {\normalfont\LARGE\titlefont} % Title style
      {\thesection} % Section number
      {1em} % Space between number and title
      {} % No additional formatting before the title text
       [{\titlerule[0.5pt]}] % Add a line under the title with 0.5pt thickness