Search code examples
latexmarkdownpandoc

How to solve error with YAML, Preamble (LaTex) and Pandoc conversion


I started using markdown together with pandoc a few weeks ago. I'm especially fond of the markdown editor writemonkey v.3! I have one slight problem with converting my .md to .pdf, and I it's caused by the first line in my document. The first line in the document (see below) is how you give the document a name in writemonkey.

name: seminarium 1

---
author: my name
date: 22 Augusti 2020
bibliography: testlibrary.bib
geometry: 
- top=2.5cm
- bottom=2.5cm
- left=2.5cm
- right=2.5cm
pagesize: a4
font: Times New Roman
fontsize: 12pt
header-includes:
   - \usepackage{setspace}
   - \setstretch{1.0}
   - \pagenumbering{gobble}

---

If I remove this first line, the pandoc conversion works like a charm. Otherwise I get this:

pandoc  sem1.md -o sem1.pdf --bibliography library.bib -V lang=sv                 
Error producing PDF.
! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.78 \usepackage

Now - I realize that I've combined a YAML with LaTex code, I am hoping that my formatting is "bad" and that I might improve it so I don't have to remove the first line every time. Maybe tell Pandoc/LaTex to ignore the first line or make a separate .yaml file would help?


Solution

  • Ok! This what I did. Works great with Writemonkey. Just copy-paste in Notepad and save as a .ps1 file and run in Powershell!

    CD C:\Users\username\Desktop\ppp2020 # Filepath of .md, .yaml and .bib
    
    Get-Content cap.md -Encoding UTF8 | Select-Object -Skip 2 | Set-Content cap_temp.md -Encoding UTF8 # Removes the first line (name:) from markdown document
    
    pandoc  cap_temp.md default.yaml  -o cap.pdf --bibliography library.bib -V lang=sv # converts .md to .pdf 
    
    Remove-Item 'C:\Users\username\Desktop\ppp2020\cap_temp.md' # This removes the tempfile