[autotex] State saving improvements

The state now gets saved after every iteration, so it's easier to cancel
the compilation and jump back later.
The state also gets written to a temporary file and copied over when
writing is finished. This should reduce the probability of state file
corruption.