2


i want to get the position of the upper left of a text and the position of the bottom right of a text. right now i'm using the following code which works ok for images but not for text because it breaks the textflow (see image below. remark: the border illustrates the rectangle created by the two measured points and is not in the actual output)

image

\begin{tabular}{c c c}
\zlabel{Overlay\arabic{overlay}} \zsavepos{Overlay\arabic{overlay}1} & & \\
& Testquery test test test & \\
& & \zsavepos{Overlay\arabic{overlay}2}\\
\end{tabular} 

How can i improve the code above and avoid that the textflow gets interrupted?

  • \zsavepos uses \pdfsavepos to save the location on the baseline of where it's placed. You're creating a tabular that has 3 rows - you should only use one and adjust vertically the y-coordinate from the baseline. Can you show how you are creating the rectangle? – Werner Feb 24 '15 at 2:00
  • The rectangle is directly created from the the two points saved with \zsavepos by writing them into a file and then reading them in a Java PDF Viewer that displays the created PDF and overlays the rectangle at the given position. Anyway, using just one row wouldnt solve the problem with the textflow i think. – Patrick Bender Feb 24 '15 at 9:09
1

This answer may not be suitable because, in addition to the x-position of the upper left and lower right, it only gives the dz of the text, that is, the relative change in z from the start of the phrase, rather than the absolute coordinates on the page. It uses tabto, rather than zref.

I base this code on my answer at Draw Tikz highlighting on the background of text, and while the result here may seem a bit cluttered, it is to show you in gory detail what is being done.

Here, the command \Ohighlight{} delimits the inline text that we wish to "measure". The red text is it being set relative to the preexisting text (i.e., starting mid-line, with line-wraps, paragraphs, etc.)

At any point before \Ohighlight is called a second time, I can recall the x-coordinate of the beginning and ending of the text as well as the dz used by the text. I create the macro \demo to show this. It laps a rule of the height dz to the left; it then sets the box (in cyan color) that was measured, properly employing its mid-line starting point. Underneath the set box, it indents to the beginning of the start of the text (\tmpx} and creates a rule over to the end of the text (\tmpy), showing that it remembers where the text began and ended.

Below this graphical demonstration, it prints the value of dz, x1, and x2.

\documentclass{minimal}
\usepackage{xcolor}
\newlength\svpi
\newsavebox\tmpbox
\usepackage{tabto}
\newcommand\Ohighlight[2][]{\color{cyan}%
  \tabto*{0in}\edef\tmpx{\TabPrevPos}\tabto*{\tmpx}%
  \setlength\svpi{\the\parindent}%
  \savebox\tmpbox{%
  \begin{minipage}[b]{\textwidth}\parindent\svpi%
    \tabto*{\tmpx}#2\strut%
  \end{minipage}}%
  {#2}%
  \vspace*{\dimexpr-\dp\tmpbox-\ht\tmpbox+\dp\strutbox}%
  \tabto*{\tmpx}\color{red}#2\unskip\color{black}\ %
  \edef\tmpy{\TabPrevPos}%
}
\textwidth=5in

\def\demo{\noindent\llap{%
  \rule[-\dp\strutbox]{3pt}{\dimexpr+\dp\tmpbox+\ht\tmpbox+\ht\strutbox}}%
  \makebox[0pt][l]{\usebox{\tmpbox}}\\
  \tabto{\tmpx}\rule{\dimexpr\tmpy-\tmpx\relax}{3pt}%
  \par dz=\the\dimexpr+\dp\tmpbox+\ht\tmpbox+\ht\strutbox; x1,x2: \tmpx, \tmpy}

\begin{document}
The original highlight is drawn over the text.
The original definition works over... 

Multi paragraphs, so my fix should try to as well.

\bigskip

Now here is my fix. \Ohighlight{The highlight is drawn under the text}
Does it work multiline?

\demo

multiline?
\Ohighlight{The highlight is drawn under the text The highlight is drawn under the text The highlight is drawn under the text The highlight is drawn under the text

A new paragraph}
And more...

\demo

\Ohighlight{short}

\demo
\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.