1

Is there any command to get current position in text?
What do I mean:

\the\currentposition
text text text text \newline
\the\currentposition
text text text text \newline
\the\currentposition

Output:
0pt
text text text text
12pt
text text text text
24pt

Or even better - how to get actual (x,y) position of some text or enviroment (i.e. top left corner of column or block) in the list?

vovo
  • 13
  • 2
    I'm not sure what your objective is, but with tikz (remember picture and overlay features), you can define a reference point and later create e.g. arrows to it. – JPi Aug 26 '15 at 15:33
  • What is your final goal? – ASdeL Aug 26 '15 at 15:54
  • Welcome to the site. The tabto package can do this with horizontal positioning, using, for example, \tabto*{\TabPrevPos} to move to that location, with \TabPrevPos containing the LaTeX length from left margin to the saved location.. – Steven B. Segletes Nov 03 '15 at 19:24

1 Answers1

2

pdfTeX in PDF mode provide \pdfsavepos, which stores the current position that can be written to the .aux file and used the next TeX run. Also XeTeX and LuaTeX provide this features. There are some limitations:

  • XeTeX's right to left mode is somewhat broken.
  • Graphics state changes, that do not use the pdfTeX interfaces (\pdfsetmatrix, \pdfsave, \pdfrestore) interfere with the position recording.

  • Absolute values are not well defined, thus relative values are preferred.

Package zref-savepos of project zref provides an interface to the positioning feature:

\documentclass{article}
\usepackage{zref-savepos}

\newcommand*{\currpos}[1]{%
  \zsavepos{#1}%
  (\zposx{#1}sp, \zposy{#1}sp) =
  (\the\dimexpr\zposx{#1}sp\relax, \the\dimexpr\zposy{#1}sp\relax)%
}

\begin{document}
\leavevmode
\currpos{posA}
text text text text \newline
\currpos{posB}
text text text text \newline
\currpos{posC}
\end{document}

Result after two compile runs:

Result

Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85
Heiko Oberdiek
  • 271,626