# Missing \begin{document} and auxiliary files

I'm a "noobish" TeX/LaTeX user and I was getting the Missing \begin{document} error. I've tried pdflatex, xelatex, xetex, luatex... nothing worked.

Actually I SOLVED this (turned around at least) by deleting every auxiliary file and leaving only the .tex one, after that it compiled smoothly, so I'm leaving this information to other noobs.

I think the issue was with the .aux file more precisely, but I'm not sure, because I can't quite replicate the problem and try file by file.

Before deleting the files, I've tried making a new .tex file with the same code and it compiled, but the original file with the auxiliary files didn't.

My question is: Why it compiled smoothly by just by deleting the auxiliary files?

The code was more or less like this:

\documentclass[a4paper,landscape,twocolumn,openany]{book}
\usepackage[utf8]{inputenc}
\usepackage[brazilian]{babel}
\usepackage{geometry} % for landscape and some other stuff
% some other innocent packages like amsmath
% i'm pretty sure it wasn't their fault anyway
\title{HUEHUEHUE}
\author{Fulano}
\date{\today}
\usepackage{lipsum} % illustration purposes

\begin{document}
\maketitle
\tableofcontents
\part{bla, bla, bla}
\lipsum
\chapter{pi, pi, pi}
\lipsum
\section{nhe, nhe, nhe}
\lipsum

\end{document}


I was editing in Ubuntu's terminal and vim, no special options like saving folds in vim, or anything.

I also noticed that sometimes with the other "test" file, I had to compile twice to get the contents straight when I put each block on it, otherwise it'd miss chapters, screw the table of contents and things like that.

The answer might be very handy to other lazy tex users, who didn't read the full documentation.

EDIT: I din't change anything in the preamble, it just "suddenly" decided to miss the \begin{document}

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Martin Schröder Nov 11 '14 at 14:27
• What exactly is your question? It's hard to help without the actual code. – Martin Schröder Nov 11 '14 at 14:28

You get that error if latex starts trying to typeset paragraphs before \begin{document} has set things up. During that processing the aux file is read (to resolve cross reference information saved on the previous run). It can happen that the aux file is corrupt and so a command in the file is misinterpreted and characters start being interpreted as text to be typeset, thus triggering the error. If this happens removing the aux file is the the thing to do.
It is not possible to say, looking at a fixed file, how the aux file came to be corrupt. Aborting a previous job, accidental edit, stray cosmic ray, ....
• Happens a lot if switching between languages in babel (I mean, adding/deleting the babel line and rerun) --- for me it's the most common cause of a corrupt .aux file. Or compiling with latex and after with xelatex and back... – Rmano Nov 11 '14 at 18:22