Compatibility of tikzmark and chappg

I have encountered a strange problem when using tikzmark and chappg in a document:

\documentclass{scrbook}

\usepackage{chappg}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\chapter{Test}

foo\tikzmark{foo}

\vspace{2cm}

bar\tikzmark{bar}

\begin{tikzpicture}[overlay,remember picture]
\draw (pic cs:foo) -- (pic cs:bar);
\end{tikzpicture}

\end{document}


This yields

l.21    \draw (pic cs:foo)
-- (pic cs:bar);

! Missing number, treated as zero.


on the second run. If I delete the aux file and compile it without chappg, everything works fine. Why is this?

As Stefan mentions in his answer, there seems to be a problem with tikzmark; in

execute at end picture={%
\ifpgfrememberpicturepositiononpage%
\edef\pgf@temp{%
\noexpand\write\noexpand\pgfutil@auxout{%
\string\savepicturepage{\pgfpictureid}{\noexpand\thepage}}}%
\pgf@temp
\fi%
},


it uses \thepage and it's safer to use \value{page}. Here's a corrected version (there's also a \thepage in the definition of \pgfmark, but since it's not used here, it's not relevant here, although it should also be changed):

\documentclass{scrbook}

\usepackage{chappg}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\makeatletter
\tikzset{%
remember picture with id/.style={%
remember picture,
overlay,
save picture id=#1,
},
every picture/.append style={%
execute at end picture={%
\ifpgfrememberpicturepositiononpage%
\edef\pgf@temp{%
\noexpand\write\noexpand\pgfutil@auxout{%
\string\savepicturepage{\pgfpictureid}{\the\value{page}}}}%
\pgf@temp
\fi%
},
},
save picture id/.code={%
\immediate\write\pgfutil@auxout{%
\string\savepointas{#1}{\pgfpictureid}}%
},
if picture id/.code args={#1#2#3}{%
\@ifundefined{save@pt@#1}{%
\pgfkeysalso{#3}%
}{
\pgfkeysalso{#2}%
}
},
next page/.is choice,
next page vector/.initial={\pgfqpoint{0pt}{0pt}},
next page/below/.style={%
next page vector={\pgfqpoint{0pt}{-\the\paperheight}}%
},
next page/above/.style={%
next page vector={\pgfqpoint{0pt}{\the\paperheight}}%
},
next page/left/.style={%
next page vector={\pgfqpoint{-\the\paperwidth}{0pt}}%
},
next page/right/.style={%
next page vector={\pgfqpoint{\the\paperwidth}{0pt}}%
},
next page/ignore/.style={%
next page vector={\pgfqpoint{0pt}{0pt}}%
},
}
\makeatother

\begin{document}

\chapter{Test}

foo\tikzmark{foo}

\vspace{2cm}

bar\tikzmark{bar}
\begin{tikzpicture}[overlay,remember picture]
\draw (pic cs:foo) -- (pic cs:bar);
\end{tikzpicture}

\end{document}

• @PhilippImhof Please see my updated answer. In fact, there's a problem in tikzmark library using \thepage. I've provided a fix and will notify the author of the library. – Gonzalo Medina Aug 22 '15 at 17:32
• Great, thank you. Just a question regarding what you wrote in your first answer: Why should one care about the bounding box when drawing just an overlay arrow? (I normally use the order you suggested, but I still wonder.) – Philipp Imhof Aug 22 '15 at 17:42
• The code isn't correct. The value of the page counter is not expanded. The aux contains \savepicturepage{pgfid1}{\c@page }. – Ulrike Fischer Aug 23 '15 at 13:04
• @UlrikeFischer You're right. Thank you for noticing it. I've changed to \the\value{page}. At least this produces the value for the page in the .aux file, but I am not sure if this is the better approach. I've already written an email to Andrew Stacey about this, so surely he'll suggest the best fix here. – Gonzalo Medina Aug 24 '15 at 16:13

The page number is used for sorting and referring to the marked position. However, chappg changes the page number, such as from 1 to 1-1. The error comes, because that's not a number any more.

You can verify it by

\renewcommand*{\chappgsep}{0}


It will work then, because it's number.

Without going further in the depth, I would say it's a bug, because it already fails with roman page numbering:

\documentclass{scrbook}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\pagenumbering{roman}
\begin{document}
foo\tikzmark{foo}

bar\tikzmark{bar}
\begin{tikzpicture}[overlay,remember picture]
\draw (pic cs:foo) -- (pic cs:bar);
\end{tikzpicture}
\end{document}


Also this brings:

! Missing number, treated as zero.

So it's independent of chappg.