# How can I avoid compiling twice?

Sometimes I need to run compile twice (eg when I use lastpage). Is there a way to set things so TeX knows that there is need for second run and do it automatically? I use Texmaker.

-

EditUpdated: I just noticed I slightly misunderstood your question. There are tools that compile the required number of times automatically, e.g. latexmk. I don't use it though, but there are certainly users who can tell you all about it. I've been using latexmk for a little while and it works really well (haven't tried it with bibliographies though), even with biblatex and Biber, and the support is great, the author even answers questions on here.

As for my initial answer below, it might still be of help in that it can eliminate the need of compiling twice.

hyperref is a package that frequently requires compiling twice . You can avoid this by loading the package bookmark after hyperref:

\usepackage{hyperref}
\usepackage{bookmark}


Generally, these two should be the last packages you load in your preamble, unless there's a package that specifically asks to be loaded after hyperref (see Which packages should be loaded after hyperref instead of before?).

bookmark provides some new possibilities, but if you've been using hyperref and you're not really looking for anything new, you can just load bookmark -- according to my experience, it won't change anything (besides the number of necessary compilations).

-
It's not just about the order you load packages in; ANYTHING at all that involves a reference — \cite, \ref, page numbers even, all require double compilation, as the correct references are added in a second pass. – Mikael Vejdemo-Johansson Oct 3 '11 at 20:21

You could set up TeXmaker to use latexmk to compile. latexmk is a Perl script that you can set up to automatically run pdflatex (or other compilers), bibtex and/or makeindex if necessary.

As a very simple, example you could go to Options -> Configure TeXmaker -> Quick Build, and set up a custom Quick Build command such as

latexmk -pdf %.tex

When running Quick Build, this will run pdflatex on the file as many times as necessary. The % is TeXmaker's way of representing the filename of the current file (without extension).

See the latexmk manual (texdoc latexmk in a terminal) for all the possible options.

There are other similar options, such as texify in MikTeX, mentioned by Werner, and Rubber. Paulo Cereda wrote a blog post about Rubber, and there was a question concerning the difference between this and latexmk.

-
Can be latexmk used with -shell-escape option of pdflatex? – Ichibann Nov 21 '11 at 19:59
@Ichibann Yes. The following is copied directly from page seven of the manual: latex -pdf -pdflatex=’pdflatex --shell-escape %O %S’ foo.tex Edit: I suppose the first word should really be latexmk, not just latex. – Torbjørn T. Nov 21 '11 at 20:25
It should indeed be latexmk. If you have a slightly older version of latexmk, you can also use the -e option to execute some Perl code that sets the option: latexmk -pdf -e '\$pdflatex="pdflatex --shell-escape %O %S"' foo.tex. – Elmar Zander Jun 20 '13 at 19:21

With respect to Seamus' recent blog post on overkill being a good thing, here's another approach.

The MiKTeX distribution includes texify (and pdftexify). These are different from the standard counterparts latex (->dvips) and pdflatex routines since they automatically recompile a document if needed.

-
Nice, but I can't see a texify in my TexLive, which is up-to-date with everything 24 hours ago... – Brent.Longborough Oct 3 '11 at 19:48
@Brent.Longborough: texify is only distributed with MiKTeX (see my answer edits). TeX Live should use latexmk, I guess. Also see Replacement for MikTeX's texify in TeX Live. – Werner Oct 4 '11 at 6:20