Problem with references \here \where

Hi I have a problem with referencing which I cannot reproduce with the MWE. Here is what the problem is. I need to write next to the page number of my reference also if that page comes before and or after (Cf. above P. X - cf. below P. Y).

For that I am using following definition:

\makeatletter%
\newcount\here@undef%
\newcommand{\here}[1]{%
\@namedef{here@#1}{}}%
\newcommand{\where}[1]{%
\@ifundefined{here@#1}{%
unten\space%
\@ifundefined{here@#1@undef}{%
\@namedef{here@#1@undef}{}%
}{}%
}{%
oben%
}%
}%
\AtEndDocument{%
\ifnum\here@undef>0%
\GenericWarning{}{There were undefined above/below labels}%
\fi}%
\makeatother%


and than in the text I put: \here{x}\label{x} and \footnote{Siehe dazu \where{x} S. \pageref{x}.}

Now that works fine for the most parts but there are some entries where it doesn't meaning it tells above when its below or below when its above.

Here is an example:

The page number is ok but it should be oben (above) instead of below (unten). This happens only with few references - not with all, but still thats a problem and it shouldn't happen.

I can't figure out why it does this.In the .aux file it looks like this:

\@writefile{toc}{\defcounter {refsection}{0}\relax }\@writefile{toc}{\contentsline {subsubsection}{\numberline {1.2.2}Homoousianismus}{36}{subsubsection.1.2.2}}
\newlabel{hmsnsnms}{{1.2.2}{36}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{ZGHPAZ8A62}{{1.2.2}{36}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{ZGGRAAN9M3}{{1.2.2}{36}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{ZGF97PS312}{{1.2.2}{37}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{ZENP2OXA6Z}{{1.2.2}{37}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{ZDNOW6A981}{{1.2.2}{38}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{athglbkntns}{{1.2.2}{38}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{ZBT29MH2LW}{{1.2.2}{38}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{ZAPYHEWQ07}{{1.2.2}{39}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{ZA4HNDP59U}{{1.2.2}{39}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{Z546N43KIX}{{1.2.2}{39}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{Z50RQN9QUC}{{1.2.2}{39}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{Z4RSFUMEBP}{{1.2.2}{39}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{Z2ZXAGGBHJ}{{1.2.2}{39}{Homoousianismus}{subsubsection.1.2.2}{}}
\newlabel{doppltexath}{{2}{40}{Homoousianismus}{Item.29}{}}
\newlabel{Z0H2OLHK3K}{{1.2.2}{42}{Homoousianismus}{Item.30}{}}


The problematic reference is:

\newlabel{doppltexath}{{2}{40}{Homoousianismus}{Item.29}{}}


and I get it that the reason for this is that here I have {2}(second chapter) instead of {1.2.2} (first chapter), but I can't figure out why he is doing this with exactly this entry which is just a normal entry like any other (nothing special about it).

Here is a MWE but as I mentioned I can't reproduce this behaviour:

\documentclass[a4paper,11pt]{scrartcl}
\usepackage[latin1]{inputenc}
\usepackage{csquotes}
\usepackage{blindtext}
\usepackage{filecontents}
\usepackage{hyperref}
\hypersetup{
citecolor=black,
filecolor=black,
urlcolor=black
}
\makeatletter%
\newcount\here@undef%
\newcommand{\here}[1]{%
\@namedef{here@#1}{}}%
\newcommand{\where}[1]{%
\@ifundefined{here@#1}{%
unten\space%
\@ifundefined{here@#1@undef}{%
\@namedef{here@#1@undef}{}%
}{}%
}{%
oben%
}%
}%
\AtEndDocument{%
\ifnum\here@undef>0%
\GenericWarning{}{There were undefined above/below labels}%
\fi}%
\makeatother%
\begin{filecontents}{\jobname.bib}
@incollection{athanasius1857,
Author = {{Athanasius Alexandrinus}},
Booktitle = {S. P. N. Athanasii archiepiscopi Alexandrini opera omnia quae exstant},
Editor = {Jacques Paul Migne},
Keywords = {source},
Pages = {14013-1422},
Publisher = {Accurante J.-P. Migne},
Series = {Patrologia Graeca},
Shortauthor = {Ath.},
Shorttitle = {ep. fest. 12.},
Title = {Epistola XII heortastica},
Volume = {26},
Year = {1857}}
@book{athanasius1858,
Author = {{Athanasius Alexandrinus}},
Booktitle = {S. P. N. Athanasii archiepiscopi Alexandrini opera omnia quae exstant},
Editor = {Jacques Paul Migne},
Keywords = {source},
Pages = {1403-1412},
Publisher = {Accurante J.-P. Migne},
Series = {Patrologia Graeca},
Shortauthor = {Ath.},
Shorttitle = {ep. fest. 11.},
Title = {Epistola XI heortastica},
Volume = {26},
Year = {1857}}
\end{filecontents}
\usepackage[ngerman]{babel}
\usepackage[style=historische-zeitschrift, maxnames=2, hyperref=true, backref=true, backrefstyle=none, backend=bibtex,idemtracker=true]{biblatex}
\bibliography{\jobname}
%
\begin{document}
\blindtext\footnote{Siehe dazu \where{x} S. \pageref{x}ff.}
\pagebreak
\blindtext\here{x}\label{x}
\pagebreak
\blindtext\footnote{Siehe dazu \where{x} S. \pageref{x}ff.}
\pagebreak
\printbibliography

\end{document}


Also and I think that is very important this only happens with Cf. below and not with Cf. above references!

I also uploaded my .tex and .aux files here if someone wants to take a look at them:

-
I am having trouble understanding what you are trying to do from your explanation. Could you add a short example where you show your input and what the expected output is? Apologies if I am just being slow... – Andrew Aug 19 '14 at 8:12
Its probably me, I am not a native speaker. I added an example (image) what the problem is. Thanks for trying to help! – eklisiarh Aug 19 '14 at 8:24
see the package varioref – David Carlisle Aug 19 '14 at 8:31
@ David: With that package I would have to do it all manually with \vpageref[above] and \vpageref[below]. That defeats the purpose of this what I already have which is Latex finding automatically if its above and below. – eklisiarh Aug 19 '14 at 8:37
My impression is that you should use \global\@namedef and also \global\advance – egreg Aug 19 '14 at 10:25

I can reproduce the issue if I enclose \here in an environment:

\begin{quotation}
\blindtext\here{x}\label{x}
\end{quotation}


Then the second \where will say “unten” instead of “oben”. The problem is that \@namedef and \advance act locally, so their action is undone at the end of the environment.

Use \global in front of \@namedef and \advance. Also, omit % after constants.

\documentclass[a4paper,11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}
\usepackage{blindtext}
\usepackage{filecontents}
\usepackage{hyperref}
\hypersetup{
citecolor=black,
filecolor=black,
urlcolor=black
}

\makeatletter
\newcount\here@undef
\newcommand{\here}[1]{%
\global\@namedef{here@#1}{}%
}
\newcommand{\where}[1]{%
\@ifundefined{here@#1}{%
unten%
\@ifundefined{here@#1@undef}{%
\global\@namedef{here@#1@undef}{}%
}{}%
}{%
oben%
}%
}
\AtEndDocument{%
\ifnum\here@undef>0
\GenericWarning{}{There were undefined above/below labels}%
\fi}
\makeatother

\begin{filecontents}{\jobname.bib}
@incollection{athanasius1857,
Author = {{Athanasius Alexandrinus}},
Booktitle = {S. P. N. Athanasii archiepiscopi Alexandrini opera omnia quae exstant},
Editor = {Jacques Paul Migne},
Keywords = {source},
Pages = {14013-1422},
Publisher = {Accurante J.-P. Migne},
Series = {Patrologia Graeca},
Shortauthor = {Ath.},
Shorttitle = {ep. fest. 12.},
Title = {Epistola XII heortastica},
Volume = {26},
Year = {1857}}
@book{athanasius1858,
Author = {{Athanasius Alexandrinus}},
Booktitle = {S. P. N. Athanasii archiepiscopi Alexandrini opera omnia quae exstant},
Editor = {Jacques Paul Migne},
Keywords = {source},
Pages = {1403-1412},
Publisher = {Accurante J.-P. Migne},
Series = {Patrologia Graeca},
Shortauthor = {Ath.},
Shorttitle = {ep. fest. 11.},
Title = {Epistola XI heortastica},
Volume = {26},
Year = {1857}}
\end{filecontents}
\usepackage[ngerman]{babel}
\usepackage[style=historische-zeitschrift, maxnames=2, hyperref=true, backref=true, backrefstyle=none, backend=bibtex,idemtracker=true]{biblatex}

\bibliography{\jobname}

\begin{document}
\blindtext\footnote{Siehe dazu \where{x} S. \pageref{x}ff.}
\pagebreak
\begin{quotation}
\blindtext\here{x}\label{x}
\end{quotation}
\pagebreak
\blindtext\footnote{Siehe dazu \where{x} S. \pageref{x}ff.}
\pagebreak
\printbibliography

\end{document}


This will print

Siehe dazu unten S. 2ff.

at page 1 and

Siehe dazu oben S. 2ff.

at page 3.

-
That solved it thank you! I also thought that it has to do something with environments but some of the quotes were really just plain quotes outside environments so thats why I didn't mention that. I am really grateful for your time and your help because I need to submit my PhD in 2 days and I was getting worried!!! – eklisiarh Aug 19 '14 at 11:40