Marco De Virgilis
Marco De Virgilis

Reputation: 1089

R Markdown Error compiling

I am trying to write a Latex document with the help of the package R Markdown. I have always been using software such as TaxMaker, but considering that I will be using R to produce several plots I would like to try the R Markdown package.

So far I have a very simple code just to try and understand it it works. Here there is the document

---
title: "Untitled"
output: pdf_document
---
\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}

\title{My Title}
\author{Me}
\date{}

\begin{document}
\maketitle

\end{document}

However this does not compile:

! LaTeX Error: Can be used only in preamble.

Error: Failed to compile 1.tex. See 1.log for more info.
Execution halted

The same code works perfectly in a standard Latex environment.

What am I missing? Thanks

Upvotes: 0

Views: 2856

Answers (2)

jay.sf
jay.sf

Reputation: 73802

In Rmarkdown, the location for custom LaTeX packages is a list in the YAML header header-includes:, as is the argument documentclass:. The most important packages are already included, so you rarely have to configure them explicitly. Especially in your example, \usepackage[latin1]{inputenc} does not seem to work, use \usepackage[utf8]{inputenc} instead.

---
title: "My Title"
author: me
output: pdf_document
documentclass: article
header-includes:
- \usepackage{amsmath}
- \usepackage{graphicx}
- \usepackage{hyperref}
- \usepackage[utf8]{inputenc}
---

# Header

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

enter image description here

Note: An extensive collection of LaTeX options can be found in the Pandoc Manual.

Upvotes: 1

Ralf Stubner
Ralf Stubner

Reputation: 26843

When using rmarkdown, you do not have to write LaTeX code. Instead you write markdown like this:

---
title: "My title"
author: "Me"
date: May 2018
output: pdf_document
---

# Introduction

Some text.

R Markdown: The Definitive Guide will fill you in on all the details.

Upvotes: 0

Related Questions