I wish to mark several pairs of from/to anchor locations in text, and use tikz to draw lines between these. The anchors are not in absolute page locations; the text determines where the endpoints of the line should be. However, the drawn line must not obscure the text. Rather, the line must appear as though it were drawn under the text instead of above.
Failed Attempt #1: Line Obscures Text
The following small example document defines \StrokeFrom and \StrokeTo macros, with the latter completing the line to the former. But the stroked line is above the text, obscuring the text below. That is what I want to change. (Run pdflatex twice for the arrows to move to their final locations.)
\documentclass{article}
\usepackage{tikz}
\tikzset{stroke/.style = {->, yellow, line width = 1ex}}
\newcommand{\StrokeAnchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[overlay, remember picture]{\coordinate (stroke #1) ;}}}}}
\newcommand{\StrokeFrom}[0]{\StrokeAnchor{from}}
\newcommand{\StrokeTo}[0]{\StrokeAnchor{to}\tikz[overlay, remember picture]{\draw [stroke] (stroke from) -- (stroke to) ;}}
\begin{document}
This text is \StrokeFrom before the figure both in the \LaTeX{} source
and in the \StrokeTo rendered document.
\begin{figure}[p]
\StrokeFrom Once upon a time. \StrokeTo
\end{figure}
This text is after the figure in the \LaTeX{} source but before
\StrokeFrom it in the rendered document. \StrokeTo
\end{document}
Failed Attempt #2: Transparent Line Pollutes Text Color
I have also considered simply making the stroked line partially transparent. Unfortunately, a partially-transparent colored line atop black text changes the color of that black text. It also makes fully-saturated colors (such as yellow, above) unusable.
Failed Attempt #3: Line Does Not Stay With Float
This problem was originally inspired by my answer to a question about highlighting text in a code listing while also keeping syntax highlighting. In that case I give the from/to anchors unique-per-page names, then paint all of the required lines from within \AtBeginShipout{\AtBeginShipoutUpperLeft{...}} so that the lines go onto the page before the text does. (Run pdflatex twice for the arrows to move to their final locations.)
\documentclass{article}
\usepackage{atbegshi,ifthen,listings,tikz}
\usetikzlibrary{calc}
\tikzset{stroke/.style = {->, yellow, line width = 1ex}}
\newcounter{stroke}[page]
\newcommand{\StrokeAnchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[remember picture, overlay]{\coordinate (#1 stroke \arabic{stroke});}}}}}
\newcommand{\StrokeFrom}[0]{\stepcounter{stroke}\StrokeAnchor{begin}}
\newcommand{\StrokeTo}[0]{\StrokeAnchor{end}}
\AtBeginShipout{\AtBeginShipoutUpperLeft{\ifthenelse{\value{stroke} > 0}{\tikz[remember picture, overlay]{\foreach \stroke in {1,...,\arabic{stroke}} \draw[stroke] (begin stroke \stroke) -- (end stroke \stroke);}}{}}}
\begin{document}
This text is \StrokeFrom before the figure both in the \LaTeX{} source
and in the \StrokeTo rendered document.
\begin{figure}[p]
\StrokeFrom Once upon a time. \StrokeTo
\end{figure}
This text is after the figure in the \LaTeX{} source but before
\StrokeFrom it in the rendered document. \StrokeTo
\end{document}
Unfortunately, this strategy misbehaves if the stroke anchors are in a float which is processed on one page but actually placed on a subsequent page. The line is drawn on the page which was active when the float was being processed, not on the later page where the float actually appears. A fully-satisfactory solution must be able to handle floats that move to different pages, such as the \begin{figure}[p]...\end{figure} float in the example document above.




\begin{pgfonlayer}{background} ... \end{pgfonlayer}doesn't work as well. Ok, on second thought thats understandable. The layers are only relative to each other in onetikzpicture. – Martin Scharrer May 27 '11 at 20:43current pagenode I can get the yellow line underneath the text and with a correct horizontal position. I am unable to get the correct vertical position. – Frédéric May 27 '11 at 22:58