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)
\stepcounterinside\zsaveposx, which is not allowed. – egreg Mar 30 '16 at 10:49\stepcounter{xcnt}in the text before thethexcntcommand. Working now! Thanks for your help! – jan Mar 30 '16 at 12:09