# LaTeX Error: Can be used only in preamble

Running this shell skript:

#!/bin/bash -e
FILE=$1 pandoc --top-level-division=chapter -V geometry:"margin=0.8in, landscape" --toc -s$FILE -o \${FILE%.*}.pdf


with this minimal LaTeX file:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\end{document}


gives me:

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.64 \documentclass


Any hints on this? I am new to LaTeX and have no clue why this is not working.

• Welcome to TeX.SX! You can help us to help you by providing the code for a small compilable document that shows your problem called MWE. Just edit your question and add missing code. Mar 1, 2018 at 13:39
• It looks like you are trying to use pandoc to convert a LaTeX file to PDF. Instead, use LaTeX to do this: pdflatex file. The script you've written should work for converting a Markdown file to PDF. (Pandoc uses LaTeX to create the PDF but you don't have to write any LaTeX code for basic documents.) Mar 1, 2018 at 13:54
• In other words, if you run Pandoc on a LaTeX file in this way, it will create another LaTeX file with the necessary preamble and then include your LaTeX file inside it, which is why you are getting this error: the document Pandoc is creating includes your preamble commands in the body, not the preamble. Mar 1, 2018 at 13:58
• What did you do in the end, Michael? Mar 15, 2018 at 15:02
• Solution was using pandoc the correct way. Not mixing it with LaTeX code. As you guys told me. Mar 16, 2018 at 13:24

This is a misunderstanding. If you have got a kind of TeX system running (usually texlive or MiKTeX), save your LaTeX-file as (e.g.) foo.tex and run pdflatex foo.tex and you'll probably get foo.pdfas result.
pdflatex --help displays help in a nutshell.