5

My question is about calculating with the package textpos.

By saving the vertical position of a block of text with zsavepos and calculating the middle of it, I want to position a block of text at the same vertical position.

In the following example, it doesn't seem to work.

Thank you for your answer.

\documentclass{article}
\usepackage{lipsum}
\usepackage{zref-savepos}
\usepackage[absolute,overlay]{textpos}
\usepackage{calc}

\begin{document}

\zsavepos{begin}
\lipsum[20]
\zsavepos{end}

\begin{textblock*}{2.5cm}[0.5,0.5](2cm,0.5\zposy{begin}sp-0.5\zposy{end}sp)
\noindent \textit{Text that should be centered vertically.}
\end{textblock*}

\begin{textblock*}{2.5cm}[0.5,0.5](2cm,11143907sp)
\noindent \textit{Text approximately properly centered.}
\end{textblock*}

\end{document}
  • +1; also because I didn't know before about the zref package – that's an interesting thing. – Norman Gray Jul 17 '14 at 20:38
4

I think there are a couple things going on here.

The first, \zposy returns the vertical position as measured from the bottom of the page. In this next example, I dispense with using the calc package and use \dimexpr from e-TeX.

I believe this will give you the results you want:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage{zref-savepos}
\usepackage[absolute,overlay]{textpos}
\begin{document}

\zsavepos{begin}
\lipsum[20]
\zsavepos{end}

\begin{textblock*}{2.5cm}[0,0.5](2.5cm,{\dimexpr\paperheight-\zposy{begin}sp+(\zposy{begin}sp-\zposy{end}sp)/2})
  \noindent \textit{Text that should be centered vertically.}
\end{textblock*}

\begin{textblock*}{2.5cm}[0,0.5](\dimexpr\paperwidth-3.5cm,11143907sp)
\noindent $\circ$\textit{Text approximately properly centered.}
\end{textblock*}

\end{document}

enter image description here

Incidentally,

0.5\zposy{begin}

doesn't do what you're expecting it to. If \zposy{begin} is 5, then 0.5\zposy{begin} expands to 0.55 not 2.5.

To calculate have the y-pos, you need to do one of the following:

\zposy{begin}/2

or

\zposy{begin}*\real{0.5}

So then to correctly calculate the position using calc package

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage{zref-savepos}
\usepackage{calc}
\usepackage[absolute,overlay]{textpos}
\begin{document}

\zsavepos{begin}
\lipsum[20]
\zsavepos{end}

\begin{textblock*}{2.5cm}[0,0.5](2.5cm,\paperheight-\zposy{begin}sp+\zposy{begin}sp*\real{0.5}-\zposy{end}sp*\real{0.5})
  \noindent \textit{Text that should be centered vertically.}
\end{textblock*}

\end{document}
  • It works perfectly as I wanted. I now need adjustments when this concerns maths equations. Thank you very much. – D.J. Jul 18 '14 at 21:12

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.