pgfmathresult with unexpected result

Please have a look at the following MWE.

\documentclass{scrartcl}
\usepackage{etoolbox}
\usepackage[absolute]{textpos}
\usepackage{tikz}
\begin{document}
\foreach \i in {1,2,3,...,30} {%
\pgfmathparse{int(mod(\i,8))}%\pgfmathresult
\begin{textblock*}{1cm}(5cm,5cm)
\begin{tikzpicture}
\node at (0,0) {test};
\end{tikzpicture}
\end{textblock*}
\ifnumequal{\pgfmathresult}{0}{\newpage}{}
}
\end{document}


When looking at the result there is only a single empty page. Only when activating the \pgfmathresult (in the first line in the loop) I get the desired four pages. But obviously I do not want the resulting digits on my pages ...

What do I have to change?

The problem is, textblock in absolute mode puts something on the page in the output routine, not in typesetting mode. This means, TeX thinks that nothing has been put on the page, and therefore \newpage will do nothing. See section 2.2 of the textpos documentation. Solution from that doc: put \null before \newpage, or anything that produces an invisible output, like ~.

 \ifnumequal{\pgfmathresult}{0}{\null\newpage}{}


By the way, now you are putting 8 copies of "test" on top of each other. Is that what you want?

• Thanks for the Explanation. No, of course is that not the final result. This is a drastically reduced example from a far more complex file. – Jürgen Dec 19 '16 at 11:35

The textblock environment typesets stuff in a box which does not change to horizontal mode. In vertical mode \newpage has no effect, so you have to add \leavevmode before textblock.

The textblock environment typesets stuff on shipout which is why \newpage sees an empty page and has no effect, so you have to add \leavevmode or \null somewhere on the page (depending on whether you want the indentation box or not).

\documentclass{scrartcl}
\usepackage{etoolbox}
\usepackage[absolute]{textpos}
\usepackage{tikz}
\begin{document}
\foreach \i in {1,2,3,...,30} {%
\pgfmathparse{int(mod(\i,8))}
\begin{textblock*}{1cm}(5cm,5cm)
\begin{tikzpicture}
\node at (0,0) {test};
\end{tikzpicture}
\end{textblock*}
\ifnumequal{\pgfmathresult}{0}{\leavevmode\newpage}{}
}
\end{document}

• What a simple solution. Many thanks for the quick help! – Jürgen Dec 19 '16 at 11:22
• Although this solution works, the explanation is wrong. See my answer for a good explanation. \newpage certainly does work in vertical mode. It would be very strange if it wouldn't. However, \leavevmode has a side effect to insert \indent, which causes a empty \hbox to be inserted, almost the same as \null. The only difference is that the \insert box has a width of \parindent, while \null has width 0. – Piet van Oostrum Dec 19 '16 at 11:50
• @PietvanOostrum Yes, you are right of course. I revised my answer. Thank you. – Henri Menke Dec 19 '16 at 12:20