Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

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.

share|improve this answer
    
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}
share|improve this answer
    
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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.