Using zref-savepos within \newenvironment

Viewed 28

I've defined

\usepackage{zref-savepos}
\newcommand*{\curypos}[1]{%
  \zsaveposy{#1}%
  \zposy{#1}sp = %
  \the\dimexpr\zposy{#1}sp\relax%
}

and that seems to work fine. Saying \curypos{some_label} reports the y position on the page, just as expected.

Now do

\newenvironment{mytest}[2]
  {\write\myfile{\curypos{#1} #2}%
  }
  {% Do nothing to close out the environment. 
  }

\begin{mytest}{first}{second}
pointless blather
\end{mytest}

The contents of \myfile do not show the y-position on the page. Instead they show

\zsaveposy {first}0sp = 0.0pt second 

For some reason, first is not passing through as an argument to \curypos.

I'm guessing that this is some form of fragility, so I tried using \protect in various places and declaring \curypos with \DeclareRobustCommand, but none of them work.

1 Answers

This isn't a complete answer, but I think the problem is due to the fact that the call to \zsaveposy is made from within a \write. My guess is that these write operations are treated as though they take place at the top (or bottom?) of the page so that the position is always (0,0). Breaking up the definition of the environment command as follows works.

\newenvironment{intfig}[2]
  {\zsaveposy{#1}%
   \write\myfile{\the\dimexpr\zposy{#1} #2 }%
  }
  {% nothing
  }
Related