# Why doesn't latexmk handle errors in the preamble?

Today I discovered that if I have an error in the preamble of my tex file then Latexmk produce an error only once.

Precisely, for example if my file looks like this

\documentclass{article}
\someWrongCommand{foo}
\begin{document}
bar
\end{document}


then of course both latexmk and pdflatex produce an error in the line \someWrongCommand{foo}. But if I then compile the file with latexmk for the second time (without changing anything in my file) then letexmk just says

Latexmk: All targets (file.pdf) are up-to-date


Whats more, if I have an error in the document (not in the preamble) then latexmk shows an error all the time (not only in the first compile) as I wish.

Is it possible to make latexmk behaves like pdflatex if there is an error in a preamble? I know I can use latexmk with -g option but it is not a solution because then it always recompile everything from scratch.

I just want latexmk to handles errors in the preamble no matter in which compilation.

• From limited testing (TeXworks 0.4.3 r858 on Windows), it looks like if you cancel latexmk on the error, it won't build a PDF, and will act like pdflatex as you want. If you hit Enter to get past the errors, a PDF will be built (in your case, with the words foo and bar in one paragraph). If that happens, there's no way for latexmk to know that it needs to rebuild the PDF until you make a change to the source file. – Mike Renfro Apr 17 '12 at 2:19
• See also the comment under this answer to the same error. You've got a different cause, somewhat, but an identical root cause: "The point being that Latex emits the 'Missing \begin{document}.' if it has something that looks like it should be output, but it has not yet met the '\begin{document}' command." – Mike Renfro Apr 17 '12 at 2:31
• @MikeRenfro: Thanks for your comments. Indeed if latexmk didn't build output file (and there is no old one!) then it always show an error. But if I previously compile my tex file and there was no error (I have an output PDF file) and then produce an error (in a preamble) then latexmk print an error only once. I know it would be enough to make latexmk remove/doesn't save output if it get an error but it behaves like this only for error in document (outside preamble). – xen Apr 17 '12 at 8:23

From latexmk's point of view, errors in the preamble are treated no differently than those in the body. If you are getting a difference, it is because you or pdflatex are reacting differently to the error.
The behavior of latexmk follows from that of pdflatex and is as follows:
• If pdflatex finds an error and you tell it to exit, then no pdf file is created. Any already existing pdf file will be unchanged.
• If you respond to error by telling pdflatex to continue, then a new pdf file will (normally) be produced. (But latexmk will see that there is an error, and not do postprocessing, e.g., forcing a previewer update.)
On a subsequent run, if the pdf file exists and if the source files are unchanged, then latexmk will not rerun pdflatex. This is normally correct behavior, since (normally) rerunning pdflatex won't change the error condition until a source file is changed. If there is no pdf file, a subsequent run of latexmk will notice the non-existence of the pdf file and therefore (pointlessly) rerun pdflatex and reproduce the error. (Perhaps I need latexmk to cache the existence of an error condition, and save the extra run.)
I definitely don't want to change latexmk to remove the pdf file if pdflatex reports an error, since generally the principle of latexmk is that it should respect the behavior of the programs it calls.