# Determining the position of a box (e.g.) on a page

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?

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.

• That works very well, thanks for the very quick answer! – user3362 Feb 5 '11 at 19:44

You could use the functionality of TikZ and its remember picture option. It needs two compile runs.

\documentclass{article}

\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\lipsum[1]
\tikz[remember picture]\node(x){\framebox{Test}};
\lipsum[2-3]

% Later
% There are better ways to do this, I'm sure:
\begin{tikzpicture}
\path let \p0 = (x.north west) in
% \x0, \y0 hold the position from the lower left page corner
[/utils/exec={\message{x = \x0, y = \y0}}];
\end{tikzpicture}
\end{document}

• Thanks a lot! Your code helped me as well, though I'll go with Yiannis' answer. – user3362 Feb 5 '11 at 19:47
• @Thomas: You are welcome, and no problem, zref seems to be the right way to do it. I didn't know it before by myself. – Martin Scharrer Feb 5 '11 at 19:52

It needs two pdflatex runs

\documentclass{book}
\usepackage[savepos]{zref}
\begin{document}

\ldots

bla bla \zsavepos{fbox}\framebox{Hello World} bla bla

\ldots

the fbox starts at (\zposx{fbox}sp;\zposy{fbox}sp), which is the same
as (\the\numexpr \zposx{fbox}/65536\relax pt;
\the\numexpr \zposy{fbox}/65536\relax pt),
always measured from the left and bottom of the page

\end{document}