# How to properly use a counter value in zref command

I am trying to expand on the answer to this question in order to automatize the creation of position markers with zref.

I guess I am missing some important knowledge about how counters are printed. So I have created a counter and two commands, one that steps the counter and prints it, the other that repeats the counter without stepping it.

\newcounter{xcnt}\setcounter{xcnt}{0}
\newcommand{\countxcnt}{\stepcounter{xcnt}A\thexcnt}
\newcommand{\repeatxcnt}{A\thexcnt}


When I test it by putting \countxcnt \repeatxcnt \countxcnt \repeatxcnt into the document body I get exactly what I want: A1A1A2A2 and so on. Now I want to use that output for zref in the following way (see the linked answer for the context in which I need this, I've stripped this down here so as to keep this small):

\documentclass{article}
\usepackage[user,savepos]{zref}
\newcommand{\offset}[2]{%
\dimexpr\zposx{#2}sp-\zposx{#1}sp+1em\relax}
\newcommand{\overmk}[1]{\leavevmode\zsaveposx{#1}}

\newcounter{xcnt}\setcounter{xcnt}{0}
%\newcommand{\countxcnt}{\stepcounter{xcnt}A\thexcnt}
%\newcommand{\repeatxcnt}{A\thexcnt}

\begin{document}
\overmk{A}This is an example \overmk{\countxcnt}sentence.\par
\hspace{\offset{A}{\repeatxcnt}}This is another sentence.\par
This \overmk{\countxcnt}is a third sentence.\par
\hspace{\offset{A}{\repeatxcnt}}This is a fourth sentence.
\end{document}


This fails with a missing \endcsname error.
Just for reference, this is providing the markers manually, which I want to automatize so that :

\begin{document}
\overmk{A}This is an example \overmk{A1}sentence.\par
\hspace{\offset{A}{A1}}This is another sentence.\par
This \overmk{A2}is a third sentence.\par
\hspace{\offset{A}{A2}}This is a fourth sentence.
\end{document}


I assume there are some fundamental things that I don't yet understand about how to use counters, I hope someone will be able to point me in the right direction...

SOLUTION (thanks to egreg's comment below):

\overmk{A}This is an example \stepcounter{xcnt}\overmk{\thexcnt}sentence.\par
\hspace{\offset{A}{\thexcnt}}This is another sentence.\par
This \stepcounter{xcnt}\overmk{\thexcnt}is a third sentence.\par
\hspace{\offset{A}{\thexcnt}}This is a fourth sentence.


(and simply ignoring the outcommented lines above, not needed)

• You are executing \stepcounter inside \zsaveposx, which is not allowed. – egreg Mar 30 '16 at 10:49
• OK, got it. I can simply put the \stepcounter{xcnt} in the text before the thexcnt command. Working now! Thanks for your help! – jan Mar 30 '16 at 12:09

\stepcounter is not expandable, since it is doing an assignment, so zsaveposx with \stepcounter as part of the argument can't work here.

Solution: The \stepcounter macro must be done before \zsaveposx in order to perform the assignment, the value can be retrieved later on.

The real purpose of this code is a little bit unclear, the solution restricts to the basic style of the OP.

\documentclass{article}
\usepackage[user,savepos]{zref}

\newcommand{\offset}[2]{%
\dimexpr\zposx{#2}sp-\zposx{#1}sp+1em\relax%
}

\newcommand{\overmk}[1]{\stepcounter{xcnt}\ifnum1=\value{xcnt}\zsaveposx{#11}\else\zsaveposx{#1}\fi}

\newcounter{xcnt}
\newcommand{\countxcnt}{A\thexcnt}
\newcommand{\repeatxcnt}{A\thexcnt}

\begin{document}
\overmk{A}This is an example \overmk{\countxcnt}sentence.\par
\hspace{\offset{A}{\repeatxcnt}}This is another sentence.\par
This \overmk{\countxcnt}is a third sentence.\par
\hspace{\offset{A}{\repeatxcnt}}This is a fourth sentence.
\end{document}