[Tex/LaTex] Determining the position of a box (e.g.) on a page

boxespositioning

Suppose I have some .tex code like this

...
bla bla \framebox{Hello World} bla bla
...

I want to read out the position (left and top or bottom, in pts) of the framebox on the page, and store in, say, variables \myleft, \myright.

It would also be enough to read out the current position on the page.
(I want to highlight the box later in another program, so I need to know its absolute position in the produced pdf.)

Synctex seems to be able to do that, so it should be possible… but how?

Best Answer

You can use the \zsavepos macro of the zref package. Note you need to LaTeX twice before you have the position accurately.

\documentclass{article}
\usepackage{zref-savepos}
\usepackage{fp}
\begin{document}

\zsavepos{XY}\fbox{Testing }
\medskip

\zposx{XY}, \zposy{XY} 

\def\X{\FPdiv\result{\zposx{XY}}{65536}\result}

\def\Y{\FPdiv\result{\zposy{XY}}{65536}\result}

\X, \Y
\end{document}

Edit: show minimal using fp package for calculations.