2

I'm working on a .tex file which basically generates a totally procedurally generated pdf. The output is a 100 page pdf in which every page's background is different. I made all of those background by hand, and LaTeX calls them out page by page.

Usually, to fasten the exportation while I work on the document, I reduce the number of pages to 1 or a few of them. The weird thing is that, when I then decide to see how the 100 page file comes out, usually I have to export two times, because the first time every "new page" after the one I was working on has the background out of position.

For instance, if I was working on 4 pages, and then I decide to export a 20 pages pdf, the pages from 5 to 20 will have the background like this:

background out of position

On the left you see a normal page, on the right there is a page with the background out of position. Notice that the background always ends up in THAT position, it's not random.

The second time I export the document, usually the background comes in the right position (since it already had that number of pages).

Here's a MWE which gives me this problem. Of course without actual background files it will be hard for you to try it, but still, maybe you can see something that I'm doing wrong:

\documentclass[11pt, a4paper]{article}

\usepackage{background}
\usepackage{lipsum}

\newcommand{\sfondo}[1]{
    \backgroundsetup{angle=0, opacity=0.5, scale=1, contents=\includegraphics{./../../sfondi/sfondo#1.png}}
}

\begin{document}

\foreach \pagenum in {1,...,100}{

    \sfondo{\pagenum}

    \lipsum[1]

    \newpage

}

\end{document}

It wouldn't be an issue to export two times - I always did it this way up to now. The real issue is that the original .tex document is starting to include really a big lot of operations, and this makes the exportation very heavy. It takes around 1 minute on Linux, and several minutes with Windows 10, on the same machine. This heavyness somehow messes up with the background, because now, if I export a 100 page pdf, it comes out with many backgrounds out of position every time. So the double exportation trick doesn't work anymore. In fact, using TeXstudio (which has some built-in feature that exports two times if the resulting pdf needs a second run) compiling that .tex file leads to an eternal loop where the pdf is exported time after time infinitely - and I can't even let it run, since every time I'm asked if I want to see the log, since it weights over 2MBs.

So the question is: is there any actual reason why, with big compilings, the bacgrkound comes out this way, and is there a way to prevent this?

il mietitore
  • 702
  • 1
  • 5
  • 19
  • I've always found background images require compiling twice - but I haven't used them much. Other features of your document can cause pagination to change on every run (we've had some questions about that) such that it's never stable, and automatically compiling until the "page numbering may have changed" message goes away will never complete. – Chris H Mar 25 '17 at 10:39

1 Answers1

0

I found a temporary solution, which is to put away the background package and use tikz instead.

\documentclass[11pt, a4paper]{article}

\usepackage{tikz}
\usepackage{lipsum}

\newcommand{\sfondo}[1]{
    \begin{tikzpicture}[remember picture,overlay,opacity=0.5]
        % draw image
        \node[inner sep=0] at (current page.center)
        {\includegraphics[width=\paperwidth,height=\paperheight]{./sfondi/sfondo#1.png}};
    \end{tikzpicture}
}

\begin{document}

\foreach \pagenum in {1,...,100}{

    \sfondo{\pagenum}

    \lipsum[1]

    \newpage

}

\end{document}

This will still need to be executed a couple of times in case I change the number of pages, but at least the second time it always prints the background in the right position.

il mietitore
  • 702
  • 1
  • 5
  • 19