Unexpected output using tikz/textpos

The following code (MWE)

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage[absolute]{textpos}

\usetikzlibrary{external}
\tikzexternalize

\begin{document}

\begin{textblock}{4}[0,0](1.3,2.5)
\begin{center}
SOME TITLE TEXT SOME TITLE TEXT\\
SOME TITLE TEXT SOME TITLE TEXT
\end{center}
\end{textblock}
\thispagestyle{empty}

\begin{figure}
\begin{tikzpicture}
\path[draw] (20mm,150mm) -- (20mm,250mm);
\path[draw] (100mm,150mm) -- (100mm,250mm);
\end{tikzpicture}

\end{figure}

\end{document}


is expected to produce two pages;

the first one containing eg some title text and the second one two parallel lines.

Instead in the second page the first one is appended (take a look at the picture)

I am using pdflatex from MikTeX 2.9 (updated) and compiling with -enable-write18.

What's wrong?

-
Are you viewing the pdf-file in "book mode", i.e. showing two facing sides at once? –  mrf Aug 11 '12 at 21:41
yes I did it to get the screenshot (the problem appears even when not viewing two sides at once) –  niels Aug 11 '12 at 21:50
I needed to comment out the \tikzexternalize[prefix=tikz-external/] line to get this to compile, after which the second page only had the vertical lines. –  Peter Grill Aug 11 '12 at 21:50
Yes, when tikz externalize is removed it works fine. But I need the externalization. As for not compiling maybe its because of -enable-write18 (I think that when you are not using miktex its -shell-escape). –  niels Aug 11 '12 at 21:57
Hmmmm. You are correct. With \tikzexternalize I do see the behavior you describe. Needed to remove [prefix=tikz-external/] to get it to compile. –  Peter Grill Aug 12 '12 at 4:44
show 1 more comment

The problem is that both textpos package and externalize library play games with TeX shipout mechanism.

Supposedly, externalize should ignore all text outside of the tikzpicture environments, which includes the text in the textblock environment. However, textpos in absolute mode includes this text inside a box which is shipped at the end of the page, apparently without tikz knowing it, and thus interfering in the "page" that externalize is trying to build.

The hack I've found is to empty this textpos box after using it. In this particular example, the following MWE works, but I don't know if it is general enough. Try it in your actual use case and tell us.

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage[absolute]{textpos}

\makeatletter
\def\cleartextposbox{\global\setbox\TP@holdbox\vbox{}}
\makeatother

\usetikzlibrary{external}

\tikzexternalize
\begin{document}
\begin{textblock}{4}[0,0](1.3,2.5)
\begin{center}
SOME TITLE TEXT SOME TITLE TEXT\\
SOME TITLE TEXT SOME TITLE TEXT
\end{center}
\end{textblock}
\thispagestyle{empty}
\null\newpage\cleartextposbox   % <-----

\begin{figure}
\begin{tikzpicture}
\path[draw] (20mm,150mm) -- (20mm,250mm);
\path[draw] (100mm,150mm) -- (100mm,250mm);
\end{tikzpicture}
\end{figure}

\end{document}


-
awesome, tested in the actual case and the series of corrupted figures is now fixed... Thanks a lot! –  niels Aug 12 '12 at 18:02